问题描述
最近用 flex 做界面的时候发现个奇怪的问题,纵向布局的时候顶部固定高度的工具栏,下方一个撑满的内容容器(height:100%,并且允许缩放),一开始布局很正常。
但是在内容容器里放一个height:100% 的 div 之后就出问题了,主容器的高度不会被flex缩小,造成内容高度撑开。
相关代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<style>
#main
{
width:220px;
height:300px;
padding:1px;
box-sizing:border-box;
border:1px solid black;
display:flex;
flex-direction:column;
justify-content:space_between;
align-items:stretch;
}
div
{
box-sizing:border-box;
}
.d1{
width:100%;
height:40px;
margin-bottom:20px;
background-color:coral;
flex-shrink:0;
}
.d2{
height:100%;
padding:5px;
background-color:lightblue;
opacity:0.8
}
.dd2{
height:100%;
background-color:#e1e1e1;
}
</style>
</head>
<body>
<div id="main">
<div class="d1">顶部</div>
<div class="d2">
<div class="dd2"></div>
</div>
</div>
</body>
</html>
因为实际使用中应该是不确定置顶置底的内容是否存在,不能置顶内容容器的高度。求教如何修正这个问题呢?让他高度不要撑开。
div.d1的margin-bottom:20px干嘛的?
div.d2的height:100%肯定肯定超出了啊,用height:calc(100% - 40px)。