css - 粘性页脚 : Why is :after needed?

标签 css sticky-footer

在下面的代码中,为什么需要 .page-wrap:after

理论上,如果没有这个,粘性页脚不应该工作吗?为什么不呢?

* {
  margin: 0;
}
html, body {
  height: 100%;
}
.page-wrap {
  min-height: 100%;
  /* equal to footer height */
  margin-bottom: -142px; 
}
.page-wrap:after {
  content: "";
  display: block;
}
.site-footer, .page-wrap:after {
  height: 142px; 
}
.site-footer {
  background: orange;
}
<div class="page-wrap">
  
  Content!
      
</div>

<footer class="site-footer">
  I'm the Sticky Footer.
</footer>

最佳答案

这是为您的页脚预留空间。由于 .page-wrap 的负边距导致页脚向上拉,因此 .page-wrap 的底部需要有一个空白的“预留”空间,所以.page-wrap 中的内容不会在 .site-footer 下结束。

.page-wrap:after 就是这样做的,它在 .page-wrap 的底部添加了一个空白的“保留”空间供页脚填充。

关于css - 粘性页脚 : Why is :after needed?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28229238/

相关文章:

html - HTML5中div元素的重叠

css - 链接区域太小或遮挡了隐藏 div 中的图像

html - Bootstrap 页脚覆盖内容

html - CSS 粘滞栏重叠

CSS 粘性页脚 - 带边距

html - 如何使用 CSS 在表格中设置 cellpadding 和 cellspacing?

javascript - 减少 CSS 中两个 block 之间的间隙

html - 文本对齐不起作用

css - 如何让页脚贴在 asp.net 页面上?

不在 View 中时的 jQuery 粘性页脚