我希望除 last 之外的所有 flex 项目只占据它们的正常高度,最后一个 flex 项目占据 flexbox 的剩余高度(拉伸)。但我无法做到这一点。
以下是我一直在尝试的:
.parent {
display: flex;
flex-wrap: wrap;
/*align-content: start;*/
border: 1px solid green;
height: 50vh
}
.child-height-auto {
align-self: start;
border: 1px solid red;
width: 100%;
}
.child-height-strech {
align-self: stretch;
border: 1px solid blue;
width: 100%;
}Run Code Online (Sandbox Code Playgroud)
<div class="parent">
<div class="child-height-auto">I should occupy my normal height</div>
<div class="child-height-auto">I should occupy my normal height</div>
<div class="child-height-strech">I should occupy remaining height of the flexbox</div>
</div>Run Code Online (Sandbox Code Playgroud)
将 flex 的方向设置为column并将 flex-grow 属性添加到最后一个元素。
.parent {
display: flex;
flex-wrap: wrap;
/*align-content: start;*/
border: 1px solid green;
height: 50vh;
flex-direction: column;
}
.child-height-auto {
align-self: start;
border: 1px solid red;
width: 100%;
}
.child-height-strech {
align-self: stretch;
border: 1px solid blue;
width: 100%;
flex-grow: 1;
}Run Code Online (Sandbox Code Playgroud)
<div class="parent">
<div class="child-height-auto">I should occupy my normal height</div>
<div class="child-height-auto">I should occupy my normal height</div>
<div class="child-height-strech">I should occupy remaining height of the flexbox</div>
</div>Run Code Online (Sandbox Code Playgroud)