我有一个页面,其中包含一个标题和其他三个不相关内容的 div,如下所示。
+-----------+
| header |
+-----------+
| 1 | 2 |
+-----+-----+
| 3 |
+-----------+
我需要的是表格占据页面上的剩余空间,但不超出视口(viewport)(页面上不应有垂直滚动条)。
换句话说,1、2、3 的高度应该恰好是剩余空间(在标题之后)的 50%。没有 JS 并且没有固定标题的高度,我怎么能做到这一点?
最佳答案
可以使用 CSS 表格和相当多的嵌套 div
元素来完成。
body, html {
margin: 0;
height: 100%;
}
.wrapper {
height: 100%;
background-color: tan;
display: table;
width: 100%; /* optional, depends on layout */
}
.header-row {
display: table-row;
}
.header-row img {
display: block;
width: 100%;
}
.content-row {
height: 100%;
display: table-row;
}
.content-row {
border: 1px dotted blue;
box-sizing: border-box;
height: 100%;
display: table-cell;
}
.content {
display: table;
width: 100%;
height: 100%;
}
.row {
display: table-row;
}
.row .cell {
display: table-cell;
height: 50%;
width: 100%;
border: 1px dotted blue;
}
.row .split {
width: 50%;
height: 100%;
float: left;
border: 1px dotted blue;
box-sizing: border-box;
}
<div class="wrapper">
<div class="header-row">
<div class="header">
<img src="http://placehold.it/1000x200">
</div>
</div>
<div class="content-row">
<div class="content">
<div class="row">
<div class="cell">
<div class="split">split</div>
<div class="split">split</div>
</div>
</div>
<div class="row">
<div class="cell">stuff</div>
</div>
</div>
</div>
</div>
关于html - 固定高度的纯 CSS 解决方案作为剩余空间的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26288917/