【问题标题】:CSS - Height: 100% vs min-height: 100%;CSS - 高度:100% 与最小高度:100%;
【发布时间】:2012-12-23 05:56:10
【问题描述】:

所以在我的代码中我有一个粘性页脚。粘性页脚具有带有min-height100% 的#wrap 容器。但是对于 min-height,您不能在 wrap div 内的对象上使用 height:100%

所以我添加了height:100%,但是当窗口高度太小时,它会使页脚滚动到 wrap div 中的内容,从而使布局混乱。

有人对此有解决办法吗?

<div id="wrap">
    <div id="main">
        <div id="content">

        </div>
    </div>
    <div class="clearfooter"></div>
</div>
<div id="footer">

</div>

CSS:

*{
    padding: 0px;
    margin: 0px;
}
html, body {
    height: 100%;
}
body{
    color: #FFF;
    background-image:url('../images/black_denim.png');
}
#wrap {
    min-height: 100%;
    margin-bottom: -200px;
    position: relative;
}
#topBanner{
    width: 200px;
    margin: 0px auto;
}
.clearfooter {
    height: 200px;
    clear: both;
}
/* footer */
#footer { 
    position: relative;
    height: 200px;
    width: 100%;
    min-width: 960px;
}

【问题讨论】:

  • 我也知道您可以使用position:absolute;top:0;bottom:0; 来获得效果,但是,由于它是绝对的,如果页脚太小,页脚将表现得像内容仍然不存在。

标签: height css


【解决方案1】:

如果您只需要一个不覆盖任何正文内容的粘性页脚,那么只需给页脚一个固定的位置,并让正文底部的内边距等于页脚高度。

body{ 
    padding-bottom:200px;
}

#footer { 
    position: fixed;
    bottom:0;
    height: 200px;
    width: 100%;
}

编辑:

如果您担心在非常小的屏幕上,固定页脚会覆盖大部分屏幕,那么除了可能使用 css 媒体查询或 javascript 动态隐藏页脚外,没有其他解决方法。

许多移动浏览器不支持固定位置,因为它们会覆盖大部分屏幕。

【讨论】:

  • 这是行不通的,因为您仍然可以将窗口做得足够小到足以覆盖它的位置。
  • 即使编辑 fixed 也不起作用。你没有理解我的问题。我发布的代码可以很好地使页脚在它遍历任何内容之前停止。但是,由于min-height:100%;,代码不允许在包装内包含height:100%;。但是如果没有min-height,代码就无法工作,这是我的问题。
  • 你说得对,我一定不明白这个问题。如果您可以发布一个包含内容的更完整示例的链接,那么我可能会更好地使用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多