底部100%高度 - 动态高度页脚

问题描述 投票:2回答:3

我有以下网页。

<body>
<div id="everything">
<div id="top_header">TOP BAR WITH INFO. FIXED HEIGHT</div>
<div id="content">
<div id="header"><h1> MENU</h1></div>
<div id="body">CONTENT</div>
</div><!--end content-->
<div id="footer">DYNAMIC HEIGHT DEPENDING ON THE LINKS IN THE FOOTER</div>
</div><!--end everything-->
</body>

和风格:

    <style type="text/css">
body {
    background-color: #FFFFFF;
    font: 13px arial;
    margin: 0;
    padding: 0;
    width: 100%;
}
#everything {
    background-color: #FFFFFF;
    min-height: 100%;
    margin: auto;
    width: 100%;
}
#content {
    display: block;
    margin: auto;
    width: 1000px;

}
#header {
    padding-bottom: 25px;
    background-color:#666699;
    height:60px;
}
#body {
    background: none repeat scroll 0 0 #FFFFFF;
    margin: 0 auto;
    padding-bottom: 23px;
    background-color:#CC3333;
    height:1500px;
}
#footer {
    display: inline-block;
    position: absolute;
    bottom:0px;
    width: 100%;
    background-color:#FF6600;
    height:70px;
}
#top_header {
    width:100%;
    background-color:#0066CC;
    height:30px;
}
h1{
    margin:0;
}
</style>

我的页脚的高度是动态的,我不能改变HTML结构,只能改变CSS。当我在较小的屏幕上看到网页并且“body”的高度大于视口时没有问题但是如果我在更大的屏幕上查看网页并且“body”的大小小于视口,则页脚不会不要坚持到底。此外,如果“主体”的大小很大(如本例所示),则不会将页脚推到底部。你能帮助我让页脚始终粘在视口的底部而不知道页脚的高度以及我所有内容所在的“身体”的高度吗?

谢谢,Mihai

css sticky-footer
3个回答
0
投票

您可以定义页脚位置相对并删除绝对

#footer{
   position:relative;
}

0
投票

这在CSS2中是不完全可能的,特别是通过扩展内容正确地向下推动灵活的高度页脚。您可以将其粘贴在屏幕底部,高度可变,但由于您之前不知道特定高度,因此无法相应地设置内容边距/填充底部。

我建议您在页面加载后通过JavaScript测量页脚高度,并将其应用为内容填充/ margin-bottom,这样可以解决您面临的唯一真正问题。

另一种选择是通过使用包装器和display:table-row / table-cell来利用表。我在这里回答了类似的问题:https://stackoverflow.com/a/9946474/759144


-1
投票

Pl z检查下面的演示。我认为这个解决方案可以帮助您解决问题。

body {
    background-color: #FFFFFF;
    font: 13px arial;
    margin: 0;
    padding: 0;
    width: 100%;
}
#everything {
    background-color: #FFFFFF;
    min-height: 100%;
    margin: auto;
    width: 100%;
}
#content {
    display: block;
    margin: auto;
    width: 1000px;

}
#header {
    padding-bottom: 25px;
    background-color:#666699;
    height:60px;
}
#body {
    background: none repeat scroll 0 0 #FFFFFF;
    margin: 0 auto;
    padding-bottom: 23px;
    background-color:#CC3333;
    height:1500px;
}
#footer {
    display: inline-block;
    position: sticky;
    bottom:0px;
    width: 100%;
    background-color:#FF6600;
    /* height:70px; */
    bottom: 0;
}
#top_header {
    width:100%;
    background-color:#0066CC;
    height:30px;
}
h1{
    margin:0;
}
<html>


<body>
<div id="everything">
<div id="top_header">TOP BAR WITH INFO. FIXED HEIGHT</div>
<div id="content">
<div id="header"><h1> MENU</h1></div>
<div id="body">CONTENT</div>
</div><!--end content-->
<div id="footer">DYNAMIC HEIGHT DEPENDING ON THE LINKS IN THE FOOTER</div>
</div><!--end everything-->
</body>



</html>
© www.soinside.com 2019 - 2024. All rights reserved.