Flexbox:如何在列上组合百分比、像素和剩余高度?
Flexbox: How to Combine percent, pixels and remaining height on columns?
我正在构建基本的网格系统,需要组合以不同大小单位描述的弹性项目的高度,这些单位是 %、px 和剩余 space。
html:
<div class="grid">
<div class="block" style="flex-basis: 50px;">50px</div>
<div class="block" style="flex-basis: 25%;">25%</div>
<div class="block">stretch to remaining space?</div>
<div class="block" style="flex-basis: 50px;">50px</div>
</div>
css:
html,body,.grid {
height: 100%;
padding: 0;
margin: 0;
}
.grid {
width: 300px;
background: #aaa;
display: flex;
flex-direction: column;
justify-content: flex-start;
}
.block {
outline: solid 1px black;
flex-grow: 0; // remove growing proportion
flex-shrink: 0; // remove shrinking proportion
}
使用flex-grow
参数(flex
shorthand属性中的第一个):
HTML :
<div class="grid">
<div class="block a">block height 50px</div>
<div class="block b">block height 25%</div>
<div class="block c">block height *</div>
<div class="block d">block height 50px</div>
</div>
CSS :
.block {
outline: solid 1px black;
}
.a, .d {
flex: 0 0 50px;
}
.b {
flex: 0 0 25%;
}
.c {
flex: 1 0 0;
}
我正在构建基本的网格系统,需要组合以不同大小单位描述的弹性项目的高度,这些单位是 %、px 和剩余 space。
html:
<div class="grid">
<div class="block" style="flex-basis: 50px;">50px</div>
<div class="block" style="flex-basis: 25%;">25%</div>
<div class="block">stretch to remaining space?</div>
<div class="block" style="flex-basis: 50px;">50px</div>
</div>
css:
html,body,.grid {
height: 100%;
padding: 0;
margin: 0;
}
.grid {
width: 300px;
background: #aaa;
display: flex;
flex-direction: column;
justify-content: flex-start;
}
.block {
outline: solid 1px black;
flex-grow: 0; // remove growing proportion
flex-shrink: 0; // remove shrinking proportion
}
使用flex-grow
参数(flex
shorthand属性中的第一个):
HTML :
<div class="grid">
<div class="block a">block height 50px</div>
<div class="block b">block height 25%</div>
<div class="block c">block height *</div>
<div class="block d">block height 50px</div>
</div>
CSS :
.block {
outline: solid 1px black;
}
.a, .d {
flex: 0 0 50px;
}
.b {
flex: 0 0 25%;
}
.c {
flex: 1 0 0;
}