【问题标题】:DIV with 100% height is too tall even without content即使没有内容,高度为 100% 的 DIV 也太高了
【发布时间】:2011-11-19 07:14:42
【问题描述】:

我正在尝试实现一个 sticky footer,它正在工作,除了主包装 div 的 100% 高度延伸得太高(#body-wrap)并且它在内容和页脚之间造成了巨大的差距。因此,我必须将页面向下滚动到巨大的空白处才能查看它。

我的 HTML 是这样的:

<div id="body-wrap">

    <div id="content">

             [about 100px of content here]

    </div><!-- end #content -->

<div class="push"></div>

</div><!-- end #body-wrap -->

<div id="footer-wrap">

    <div id="footer-content">

              [about 300px of content here]

    </div> <!-- end #footer-content -->

</div> <!-- end #footer-wrap -->

还有我的 CSS:

html, body {
height: 100%;
}

#body-wrap {
min-height: 100%;
height: auto !important;
height: 100%;
margin: 0 auto -300px; /* the bottom margin is the negative value of the footer's   height */
 }

.footer-main-wrap, .push {
height: 300px; /* .push must be the same height as .footer */
}

有人知道为什么 100% 的高度会比内容延伸得更远吗?

【问题讨论】:

  • 您是否也忘记在原始来源中添加引号"?当我准备我的答案时,右引号的缺失导致了不同的布局(FF 3.6.22)。

标签: css height footer sticky-footer


【解决方案1】:

添加 height 100% 到你的 html 和 height auto 到您的 body 将使其在页面不够长时正确调整

【讨论】:

    【解决方案2】:

    当您将高度指定为百分比时(例如,height: 100%),这与 容器有关,而不是元素的 内容。如果您不需要支持 IE6,您可能会发现使用 position: fixed 作为页脚更容易实现。


    编辑:我刚刚注意到另一件事 - 在您的标记中,您有一个 ID 为 footer-wrap 的元素,但在您的 CSS 中,您使用的是选择器 .footer-main-wrap。尝试将 CSS 中的 .footer-main-wrap 更改为 #footer-wrap

    【讨论】:

    • 你是对的,它采​​用了父 div 的高度。这似乎解决了 100% 高度过长的问题。但是现在我的页脚下方有一个很大的间隙。嗯...
    • 差距有多大?如果它在 10-15px 左右,它可能是 body 元素的默认边距。 @Rob 的回答会有帮助,即将body { margin: 0; } 放入您的样式表中。
    • 底部间隙约为180px。添加正文 { 边距:0; } 似乎没有任何区别。
    • 刚刚注意到您的 CSS 和标记之间存在一些不匹配 - 查看更新后的答案。
    • 这里只是一个错字。这是我网站上的正确选择器。
    猜你喜欢
    • 2013-10-21
    • 2013-04-18
    • 1970-01-01
    • 2015-02-14
    • 1970-01-01
    • 1970-01-01
    • 2015-04-07
    • 2015-05-04
    • 2013-08-03
    相关资源
    最近更新 更多