给定以下HTML:

< div id = "容器" > <!——这里的其他元素——> < div id =“版权”> 版权所有Foo网页设计 < / div > < / div >

我想把#copyright贴在#container的底部。我能在不使用绝对定位的情况下实现这一点吗?


当前回答

不要在底部使用"position:absolute"作为粘性页脚。那么你可以这样做:

html, body { height: 100%; margin: 0; } .wrapper { min-height: 100%; /* Equal to height of footer */ /* But also accounting for potential margin-bottom of last child */ margin-bottom: -50px; } .footer{ background: #000; text-align: center; color: #fff; } .footer, .push { height: 50px; } <html> <body> <!--HTML Code--> <div class="wrapper"> <div class="content">content</div> <div class="push"></div> </div> <footer class="footer">test</footer> </body> </html>

其他回答

如果你不知道子块的高度:

#{母公司 背景:绿色; 宽度:200 px; 身高:200 px; 显示:表格单元; vertical-align:底部; } .child { 背景:红色; vertical-align:底部; } < div id = "父" > < div class = "孩子" >的孩子 < / div > < / div >

http://jsbin.com/ULUXIFon/3/edit

如果你知道子块的高度,添加子块,然后添加padding-top/margin-top:

#{母公司 背景:绿色; 宽度:200 px; 身高:130 px; padding-top: 70 px; } .child { 背景:红色; vertical-align: 底; 身高:130 px; } < div id = "父" > < div class = "孩子" >的孩子 < / div > < / div >

如果你想让它“粘”在底部,不管容器的高度如何,那么绝对定位是要走的路。当然,如果版权元素是容器中的最后一个,它将始终位于底部。

你能详细说明一下你的问题吗?准确解释你想要做什么(以及为什么你不想使用绝对定位)?

可能不是。

指定位置:相对于#容器,然后position:绝对;底部:0;#版权。


#{容器 位置:相对; } #{版权 位置:绝对的; 底部:0; } < div id = "容器" > <!——这里的其他元素——> < div id =“版权”> 版权所有Foo网页设计 < / div > < / div >

为#copyright上面的元素创建另一个容器div。就在版权上面添加一个新的div: < div风格= "明确:;" > < / div > 它将强制页脚位于其他所有内容的下面,就像使用相对定位(bottom:0px;)的情况一样。

虽然这里提供的答案似乎都不适用于我的特定情况,但我看到了一篇文章,它提供了一个简洁的解决方案:

#container {
  display: table;
}

#copyright {
  display: table-footer-group;
}

我发现它非常有用,适用于移动显示的响应式设计,而不必重新排序网站的所有html代码,将主体本身设置为一个表。

请注意,只有第一个表脚-组或表头-组会被这样呈现:如果有多个表脚-组,其他的将被这样呈现。