html - 固定高度的纯 CSS 解决方案作为剩余空间的函数

标签 html css

我有一个页面,其中包含一个标题和其他三个不相关内容的 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/

相关文章:

css - SCSS 自定义函数和字符串额外引号

javascript - 如何使用@keyframes 将动画从当前位置移动到 CSS3 中的固定位置?

javascript - 将列表嵌入为字符串 javascript 的一部分

Jquery 对话框,更改文本字段大小和字体

php - JEA 设施布局添加自定义 css 样式

html - 如何使图像表现得像 <p :selectBooleanCheckbox> in Primefaces?

html - 如何去除空白页眉

jquery - 将 CSS 不透明度应用于下拉菜单

javascript - 如何在执行函数后显示组件?

javascript - 带停止按钮的倒计时