带有Margin-Top的CSS粘性页脚

3

我在 Stack Overflow 上找到了很多关于固定页脚的解决方案,并且它们对我来说都有效。然而,我需要在我的“内容 div”和我的“页脚 div”之间保持60像素的距离。在我所找到的所有解决方案中,如果我为我的“页脚 div”设置 margin-top: 60px,它没有起作用。

我找到的一个解决方案:

<div id="container">
    <div id="body">
        <div id="teste">
        </div>
    </div>
    <div id="footer">
    </div>
</div>

html,
body {
  margin:0;
  padding:0;
  height:100%;
}

#container {
  min-height:100%;
  position:relative;
}

#teste {
  background: red;
  height: 500px;
}

#body {
  padding:10px;
  padding-bottom:60px;   /* Height of the footer */   
}

#footer {
  position:absolute;
  bottom:0;
  width:100%;
  height:60px;   /* Height of the footer */
  background:#6cf;
  margin-top: 60px; <---- this didn't work
}
2个回答

10
解决方案比我想象的要简单。只需增加我的 #body div 的 padding-bottom 值即可。

3
在“content-div”上设置“margin-bottom:60px;”。您的粘性页脚具有绝对定位,这会将其从页面流中移除。您设置的边距不会与其他元素交互。在内容div上设置边距只是使该div保持在页面底部60像素的位置,即您的页脚占用的空间。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接