html - 如何让我的 flexbox 布局占用 100% 的垂直空间?

标签 html css flexbox

我如何知道 flexbox 布局行占用了浏览器窗口中剩余的垂直空间?

我有一个 3 行的 flexbox 布局。前两行是固定高度,但第三行是动态的,我希望它增长到浏览器的全高。

enter image description here

我在第 3 行有另一个 flexbox,它创建了一组列。为了正确调整这些列中的元素,我需要它们了解浏览器的整个高度——例如背景颜色和底部的元素对齐方式。主要布局最终会像这样:

enter image description here

.vwrapper {
    display: flex;
    flex-direction: column;
    flex-wrap: nowrap;
    justify-content: flex-start;
    align-items: stretch;
    align-content: stretch;
    //height: 1000px;
}
.vwrapper #row1 {
    background-color: red;
}
.vwrapper #row2 {
    background-color: blue;
}
.vwrapper #row3 {
    background-color: green;
    flex 1 1 auto;
    display: flex;
}
.vwrapper #row3 #col1 {
    background-color: yellow;
    flex 0 0 240px;
}
.vwrapper #row3 #col2 {
    background-color: orange;
    flex 1 1;
}
.vwrapper #row3 #col3 {
    background-color: purple;
    flex 0 0 240px;
}
<body>
    <div class="vwrapper">
        <div id="row1">
            this is the header
        </div>
        <div id="row2">
            this is the second line
        </div>
        <div id="row3">
            <div id="col1">
                col1
            </div>
            <div id="col2">
                col2
            </div>
            <div id="col3">
                col3
            </div>
        </div>
    </div>
</body>

我已经尝试添加一个 height 属性,当我将它设置为一个硬数字时它会起作用,但当我将它设置为 100% 时它不起作用。我知道 height: 100% 不起作用,因为内容没有填满浏览器窗口,但我可以使用 flexbox 布局复制这个想法吗?

最佳答案

你应该将 html, body, .wrapperheight 设置为 100% (为了继承全高)然后设置flex 值大于 1.row3 而不是其他值。

.wrapper, html, body {
    height: 100%;
    margin: 0;
}
.wrapper {
    display: flex;
    flex-direction: column;
}
#row1 {
    background-color: red;
}
#row2 {
    background-color: blue;
}
#row3 {
    background-color: green;
    flex:2;
    display: flex;
}
#col1 {
    background-color: yellow;
    flex: 0 0 240px;
    min-height: 100%;/* chrome needed it a question time , not anymore */
}
#col2 {
    background-color: orange;
    flex: 1 1;
    min-height: 100%;/* chrome needed it a question time , not anymore */
}
#col3 {
    background-color: purple;
    flex: 0 0 240px;
    min-height: 100%;/* chrome needed it a question time , not anymore */
}
<div class="wrapper">
    <div id="row1">this is the header</div>
    <div id="row2">this is the second line</div>
    <div id="row3">
        <div id="col1">col1</div>
        <div id="col2">col2</div>
        <div id="col3">col3</div>
    </div>
</div>

DEMO


编辑,如@Basj 所述,代码可以缩短。我们现在也可以广泛使用网格:下面是一个供访问者使用的网格示例:

body {
    height: 100vh;
    display: grid;
    grid-template-rows: auto auto 1fr;
    margin: 0;
    background-color: orange;
    grid-template-columns: 240px 1fr 240px;
}

[id^=row]{ grid-column: 1/-1 }

#row1 { background-color: red; }
#row2 { background-color: blue; }
#row3 { background-color: green; }
#col1 { background-color: yellow; }
#col3 { background-color: purple; }
<div id="row1">this is the header</div>
<div id="row2">this is the second line</div>
<div id="col1">col1</div>
<div id="col2">col2</div>
<div id="col3">col3</div>

关于html - 如何让我的 flexbox 布局占用 100% 的垂直空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56307650/

相关文章:

javascript - 如何将 `google map`到 `div`直接加载到容器中?

html - 向空的 div 添加背景 - 指定不可能的高度

html - 将 div 边框放在包含文本的前面(固定显示)

html - 在 HTML 5 进度元素中获取差距

html - 如何在居中的 flexbox div 容器中换行?

html - 在 flexbox 元素之间添加空间

html - 具有 100% 视口(viewport)宽度的 flex-wrap 元素不会在 chrome 移动模拟器上出现 100%

ide - 任何好的、可视化的 HTML5 编辑器或 IDE?

css - 在浏览器中更改 css 媒体类型

css - Bootstrap 表单文本在 Google Apps Script htmlservice 中的移动设备上没有响应