html - 粘在页面底部的页脚

标签 html css footer

您好,我有一个 css 问题,因为我试图将页脚粘贴到页面底部,但它只将其粘贴到视口(viewport)的最下方,而不是文档的底部。

有人能帮我理解这是为什么吗?

我的 css 和 html 非常简单,尽管在阅读了大量示例并尝试之后我仍然无法让它工作。我不希望我的页脚在包装内,而是在包装外,我也不想在包装上设置高度:100%。

我的 html 如下所示:

<div class="wrapper">
... some content
</div>
<div class="footer">

</div>

还有我的CSS:

html {
   height: 100%;
   margin: 0;
   padding: 0;
   position:relative;
}

body {
    position:relative;
    height: 100%;
    background-color: #D8D8D8;
    margin: 0;
    padding: 0;
    font-family: "Trebuchet MS", Verdana, tahoma, arial, serif;
    font-size: 12pt;
}

.wrapper {
  position:relative;           
  margin-left: auto;
  margin-right: auto;
  width: 1024px;
  padding: 6px;
  margin-bottom: 30px;

}

.footer {
    position: absolute;
    bottom:0;
    left: 50%;
    margin-left: -512px;
    height: 25px;        
    background-color: #E6E6E6;
    width: 1024px;
    padding: 6px;
    clear:both;

}

是否可以用 wrapper 外的页脚来做到这一点?

我认为在页脚上设置 position absolute 意味着它将基于 body 或 html 定位,因为它们是下一个带有 position:relative 的元素,但 bottom:0 似乎只是视口(viewport)的底部尽管包装器 div 的内容远远超出了这个范围。

这样做的结果是,当包装器中有很多内容时,页脚实际上会粘在页面中间,因为底部被计算为视口(viewport)的底部。

谢谢

最佳答案

定位不需要一直用。该属性仅在某些情况下用于专门“定位”内容。

我想删除

position: absolute;

来自

  .footer

应该可以解决问题。

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

相关文章:

html - 为什么这个 flexbox 页脚不能在移动设备上工作?

html - 多个图像 CSS 之间的淡入淡出

javascript - 使用 angularJS Controller 生成下拉菜单

javascript - Reactjs创建多个对象数组声明

javascript - 如何获得文本节点的感知样式?

javascript - 单击时创建变暗的背景

html - 容器内的全宽部分

wordpress - 在 Wordpress 主题中添加 3 列动态小部件页脚

html - 将鼠标悬停在第二个元素上时,如何更改上一个元素的背景?

html - 让图片正确并排显示