html - 如何在 CSS 中将页脚放在页面底部?

标签 html css

我尝试为每个页面创建页脚。目标是使页脚居中并将其放置在页面底部。你可以查看我的JSFiddle ,或者直接看代码如下。

HTML

<div id="page1" class="page">
  <div class="footer">
    <p>1</p>
  </div>
</div>

CSS

div.page {
    height: 300px;
    width: 180px;
    border: 1px solid #000;
    margin: auto;
    margin-bottom: 5px;
    text-align: center;
}
div.footer {
    background-color: #DDD;
    width: 100%;
    bottom: 0; /* doesn't work */
}
p {
    width: 15px;
    color: white;
    background-color: red;
    text-align: center;
    margin: auto;
    bottom: 0;
}

我看到了关于 How to position div at the bottom of a page ? 的类似问题.但是,当我应用它的建议时,bottom + position 设置,每个页面中的页脚都合并在一起,放置在导航器窗口的底部。这是相关的JSFiddle

有人可以帮忙吗?非常感谢。

最佳答案

您缺少应用于 class="page"position: relative;。 这样,应用了绝对位置的元素就知道它需要是 bottom:0 相对于父元素 .page。

div.page {
    height: 300px;
    width: 180px;
    border: 1px solid #000;
    margin: auto;
    margin-bottom: 5px;
    text-align: center;
    position:relative;
}
div.footer {
    background-color: #DDD;
    width: 100%;
    bottom: 0; /* it works now */
    position: absolute;
}
p {
    width: 15px;
    color: white;
    background-color: red;
    text-align: center;
    margin: auto;
    bottom: 0;
}

演示 http://jsfiddle.net/9xzb9m48/3/

关于html - 如何在 CSS 中将页脚放在页面底部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32780897/

相关文章:

javascript - jQuery淡出问题

html - 将不同的文本标题应用于具有相同类的 html <hr> 元素

jQuery - 修改 HTML 字符串

javascript - 检测 Div 的可见宽度

html - 更改 Chrome 中的元素符号点颜色?

C#:如何将 HTML5/CSS3 转换为 PDF 文档?

html - CSS 网格环绕行为

HTML/CSS 定位图像/描述布局

css - "NetworkError: 404 Not Found"/样式.css

jquery - Bootstrap 抛出 Uncaught Error : Bootstrap's JavaScript requires jQuery