html - 使页脚粘在页面底部的干净方法?

标签 html css footer

<分区>

我试图让我的页脚贴在网站的底部,当页面高度小于视口(viewport)高度和视口(viewport)高度时,我还没有看到让页脚贴在底部的好答案小于页面高度。

最佳答案

您可以使用 flexbox 实现粘性页脚。 而如果内容小于屏幕高度,您的 main 使用 flex-grow:1 增长。

鉴于这些是代码片段的重要部分:

html,body {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

main {
  flex-grow: 1;
}


header, footer {
  height: 50px;
}

header {
  background-color: green;
}

.content {
  background-color: grey;
  overflow: hidden;
}

footer {
  background-color: red;
}

html,body {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

main {
  flex-grow: 1;
}
<header>header</header>
<main>
  <div class="content">
    content
  </div>
</main>
<footer>footer</footer>

附注 这主要是因为在 flex-container 中间的 flex-child 上设置了 min-height 和设置 flex-grow: 1

假设您在 flex-container 中有 3 个具有设定高度的元素。 具有 flex-grow: 1 的元素将填充剩余空间以达到父元素的高度。而其他 2 个元素的高度取决于它们的内容。

因此,一旦您的内容达到内容元素 min-height 的大小,就没有空间可以填充了,它的行为通常与 height: auto 的元素一样

关于html - 使页脚粘在页面底部的干净方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59129564/

上一篇:html - 如何阻止固定对象超出其父容器?

下一篇:css - 在 scss 导入上设置 `font-display: swap`

相关文章:

html - Thymeleaf - Chrome 看不到 css 文件

javascript - 使用 jQuery 验证插件验证依赖选择选项

css - 我的列表元素正在形成一个楼梯

HTML 页脚不会停留在页面底部

jquery - Jquery 代码应该放在页眉还是页脚?

html - 如何使网格线可见?

javascript - HTML5 Input datetime-local 跨浏览器获取日期对象

css - CSS 宽度为 :100% gets shorter by increasing the IE zoom level 的 div

html - 更改选择的颜色

html - 页脚:站点地图并排