【发布时间】:2012-06-27 10:01:23
【问题描述】:
我有一个“主要部分” div,它设置为从其父 div 继承它的高度,即“包装器”div。包装器 div 设置为从其父 div 继承它的高度,它是文档的主体。 html 和 body 标签设置为 height: 100%。
所以,为了使用 CSS “sticky footer”(位于http://www.cssstickyfooter.com/),我必须在“main-section” div 中设置padding-bottom 等于“footer” div 的高度(必须是在包装 div 之外)。然后,必须为页脚 div 赋予一个 负 margin-top 值,该值也等于页脚的高度。
所有这些都是为了将页脚保持在页面底部,但我正在尝试将主部分的高度 100% 扩展到页脚,以便主部分的 background-color 在下方可见整个页面。
我很接近这样做,除了主要部分现在延伸到页脚之外,并将窗口拉伸超过 100% 高度(当没有足够的内容超过页面高度时),然后背景颜色可见页脚,超出页面高度(这是不可取的)。
即使页脚设置为clear: both 和position: relative(确实将页脚保持在页面底部,但主要部分 div 仍然在页脚下方延伸了很多)。或者包装器的min-height: 100% 属性可能会导致冲突?
这是相关的html:
<div id="wrap">
<div id="header">
...
</div> <!-- end of header -->
<div id="main-section">
...
</div> <!-- end of main section -->
</div> <!-- end of wrapper -->
<div id="footer">
...
</div> <!-- end of footer -->
...这是相关的CSS:
*
{
margin: 0px;
padding: 0px;
}
html, body
{
height: 100%;
}
body
{
background-color: #bbb;
}
#wrapper
{
/* wrapper 100% of screen */
min-height: 100%;
height: inherit;
width: 950px;
margin-left: auto;
margin-right: auto;
}
#header
{
background-color: #C97;
line-height: auto;
text-align: center;
font-family: "Lucida Console";
font-weight: bold;
font-size: 2.5em;
}
#main-section
{
background-color: #ddd;
height: inherit;
/* for a "sticky" footer */
padding-bottom: 50px; /* equal to the height of the footer */
}
#footer
{
clear: both;
position: relative;
height: 50px;
line-height: 50px;
margin-top: -50px; /* equal to the height of the footer, for a "sticky footer" */
width: 950px; /* equal to width of wrapper */
margin-left: auto;
margin-right: auto;
text-align: center;
background-color: #C97;
}
编辑:值得一提的是,我正在 Firefox 中进行测试。
【问题讨论】:
标签: css height percentage sticky-footer html