事情描述:希望有一个页面布局,头部高度不动,内容区域可以随着外围区域变化自动填充剩余的空间,在内容区域内有一个滚动的div可以自适应内容区域的高度。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.flex {
width: 300px;
height: 500px;
display: flex;
flex-direction: column;
}
.header {
flex: 0 0 auto;
height: 50px;
background-color: red;
}
.content {
flex: 1 1 auto;
}
.wrap {
overflow: auto;
height: 100%;
background-color: green;
}
</style>
</head>
<body>
<div class="flex">
<div class="header"></div>
<div class="content">
<div class="wrap">
<div style="height: 1000px; background-color: gray;"></div>
</div>
</div>
</div>
</body>
</html>
后来发现.wrap
中滚动条没有出现,父容器被撑开了,如图所示
原因是.wrap
没有继承到.content
的高度,只要给.content
设置height: 0
就可以了。
.flex {
width: 300px;
height: 500px;
display: flex;
flex-direction: column;
}
.header {
flex: 0 0 auto;
height: 50px;
background-color: red;
}
.content {
flex: 1 1 auto;
height: 0;
}
.wrap {
overflow: auto;
height: 100%;
background-color: green;
}
结果如下