以下HTML非常简单并且做到了我想要的效果。绿色背景会向下延伸填满整个窗口。
<body style="margin:0">
<div style="height:100%;display:flex;flex-direction:column">
<div style="background:#d0d0ff">
This is a header
</div>
<div style="background:#d0ffd0;flex-grow:1">
This is the body.
</div>
</div>
</body>
但是,如果我使用一些弹性列替换正文,并给它们设置height:100%
,因为我想让它们延伸到底部,那么newdiv
实际上会获得大于其容器的100%的高度,并导致所有内容滚动。 为什么这里的100%不是100%呢?
<body style="margin:0">
<div style="height:100%;display:flex;flex-direction:column">
<div style="background:#d0d0ff">
This is a header
</div>
<div style="background:#d0ffd0;flex-grow:1">
<!-- The new part -->
<div id='newdiv' style="display:flex;flex-direction:row; height:100%">
<div style="background:#ffd0d0"> Col 1 </div>
<div> Col 2 </div>
</div>
</div>
</div>
</body>