【问题标题】:Why height 100% doesn't work for the content?为什么高度 100% 对内容不起作用?
【发布时间】:2015-05-24 21:24:02
【问题描述】:

在这段代码中,我有一个置顶页脚和上面的内容部分,但为什么 height:100% 不适用于内容部分?

html {
  position: relative;
  min-height: 100%;
}
body {
  margin-bottom: 60px;
  height: 100%;
}
.content {
  background-color: #116655;
  height: 100%;
}
.footer {
  position: absolute;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 60px;
  background-color: #ffcc44;
}
<div class="content">
  <div>content</div>
</div>

<footer class="footer">
  My footer
</footer>

【问题讨论】:

    标签: html css


    【解决方案1】:

    您需要在html上设置一个实际高度

    html {
     position: relative;
      height: 100%; /* not min-height */
    }
    

    JsFiddle Demo

    【讨论】:

    • 这不起作用,因为我的粘性页脚与内容重叠。现在页脚停留在内容下方(如果我们有很多内容)
    • 那么也许position:absolute 不是您的最佳选择……但这不是您的问题。尝试另一个“坚持页脚”选项,也许是css-tricks.com/snippets/css/sticky-footer
    【解决方案2】:

    在内容上也使用position: absolute,然后使用top: 0 和bottom: 60px 这将使内容占用剩余空间。然后使用overflow: auto; 允许滚动内容;

    (Demo)

    .content {
        position: absolute;
        top: 0;
        bottom: 60px;
        background-color: red;
        overflow: auto;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-01-28
      • 1970-01-01
      • 1970-01-01
      • 2013-06-11
      • 2015-05-04
      • 2011-12-14
      • 2017-09-13
      • 2016-06-17
      • 2016-07-15
      相关资源
      最近更新 更多