html - 粘性页脚不起作用

标签 html css

您好,我正在尝试制作带有粘性页脚的简单页面。页脚贴在底部,但是当有很多内容时,它会放在粘性页脚后面。为什么?

html, body{
height: 100%;
min-height: 100%;
}
#wrapper{
height: 100%;
min-height: 100%;
margin-bottom: -50px;
}
#footer{
height: 50px;
background-color: blue;
}
<html>
<body>
<div id="wrapper">
</div>
<div id="footer"></div>
</body>
</html>

最佳答案

不要使用 position:absolute 创建粘性页脚。我推荐你使用 flexbox。请查看我的fiddle

代码片段:

body {
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  min-height: 100vh;
}

main {
  flex: 1 0 auto;
}

.footer {
  background: crimson;
}
<main>
  <h2>Your Content goes here...</h2>
</main>

<footer class="footer">
  This is sticky footer
</footer>

关于html - 粘性页脚不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42369781/

相关文章:

javascript - div放置在适当的位置

html图片在大型菜单上

javascript - 如何在 Javascript 中按值(而不是引用)保存所选元素的 Node ?

html - 图片库使用 flexbox : fill blank space with other images

javascript - 使用 JavaScript 倒数

user-interface - 将很多东西放在一页上的最佳方法

html - 如何让四个div引用同一张图片

javascript - 我应该在我的网站中包含 "prefix-free"插件吗

html - 包含 div 和部分的 CSS3 多列

javascript - 如何使用 CSS 和 Javascript 制作居中画廊布局?