简体   繁体   English

为什么高度100%对内容不起作用?

[英]Why height 100% doesn't work for the content?

In this code I have a sticky footer and a content section above, but why height:100% doesn't work for content section? 在这段代码中,我在页脚和内容部分上面有一个粘性内容,但是为什么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> 

You need to set an actual height on the html 您需要在html上设置实际高度

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

JsFiddle Demo JsFiddle演示

Use position: absolute on the content as well, then use top: 0 and bottom: 60px this will make the content take up the remaining space. 在内容上也使用position: absolute ,然后使用top: 0bottom: 60px这将使内容占用剩余空间。 then use overflow: auto; 然后使用overflow: auto; to allow scrolling the content; 允许滚动内容;

( Demo ) 演示

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM