【问题标题】:Pure CSS solution for fixed height as function of remaining space作为剩余空间函数的固定高度的纯 CSS 解决方案
【发布时间】:2014-10-10 07:42:34
【问题描述】:

我有一个包含标题和其他三个不相关内容的 div 的页面,就像这样。

+-----------+
|   header  |
+-----------+
|  1  |  2  |
+-----+-----+
|     3     |
+-----------+

我需要的是表格占据页面上的剩余空间,但不能超出视口(页面上应该没有垂直滚动条)。

换句话说,1、2 和 3 的高度应该正好是剩余空间的 50%(在标题之后)。在没有 JS 并且不固定标题高度的情况下,我如何才能做到这一点?

【问题讨论】:

    标签: html css


    【解决方案1】:

    可以使用 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>

    【讨论】:

    • 不错!经过进一步研究,box 和 flex 模型也是可选的,但 table 解决方案具有最好的浏览器支持。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-03
    • 2020-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-02
    相关资源
    最近更新 更多