Flexbox行:如何拉伸儿童填充横轴?

Deb*_*ole 112 css flexbox

我有一个左右的flexbox:

.wrapper {
  display: flex;
  flex-direction: row;
  align-items: stretch;
  width: 100%;
  height: 70vh;
  min-height: 325px; 
  max-height:570px; 
}
Run Code Online (Sandbox Code Playgroud)
<div class="wrapper">
  <div class="left">Left</div>
  <div class="right">Right</div>
</div>
Run Code Online (Sandbox Code Playgroud)

问题是,正确的孩子没有表现出反应.具体来说,我希望它填充包装器的高度.怎么做到这一点?

Ale*_*rov 141

  • row-flexbox容器的子项自动填充容器的垂直空间.

  • flex: 1;如果您希望子项填充剩余的水平空间,请为其指定:

.wrapper {
  display: flex;
  flex-direction: row;
  align-items: stretch;
  width: 100%;
  height: 5em;
  background: #ccc;
}
.wrapper > .left
{
  background: #fcc;
}
.wrapper > .right
{
  background: #ccf;
  flex: 1; 
}
Run Code Online (Sandbox Code Playgroud)
<div class="wrapper">
  <div class="left">Left</div>
  <div class="right">Right</div>
</div>
Run Code Online (Sandbox Code Playgroud)

  • flex: 1;如果您希望它们填充等量的水平空间,请为两个子项指定:

.wrapper {
  display: flex;
  flex-direction: row;
  align-items: stretch;
  width: 100%;
  height: 5em;
  background: #ccc;
}
.wrapper > div 
{
  flex: 1; 
}
.wrapper > .left
{
  background: #fcc;
}
.wrapper > .right
{
  background: #ccf;
}
Run Code Online (Sandbox Code Playgroud)
<div class="wrapper">
  <div class="left">Left</div>
  <div class="right">Right</div>
</div>
Run Code Online (Sandbox Code Playgroud)

  • [`flex`](https://developer.mozilla.org/en-US/docs/Web/CSS/flex) 是 [`flex-grow`](https://developer.mozilla.org) 的简写属性/en-US/docs/Web/CSS/flex-grow)、[`flex-shrink`](https://developer.mozilla.org/en-US/docs/Web/CSS/flex-shrink),以及[`flex-basis`](https://developer.mozilla.org/en-US/docs/Web/CSS/flex-basis)。`flex: 1;` 等价于 `flex-grow: 1;`。 (33认同)
  • 似乎不需要“align-items:stretch;” (4认同)
  • @BarryCap 感谢您的更正。事实上,“flex: 1;”实际上相当于“flex: 1 1 0;”或“flex-grow: 1;”。弯曲收缩:1;flex-basis: 0;`,根据 [`flex` 文档](https://developer.mozilla.org/en-US/docs/Web/CSS/flex)。 (3认同)

Ali*_*ali 6

简短的回答:只使用flex:1;for child

.wrapper {
    display: flex;
    flex-direction: row;
    .
    .
    .
}
.wrapper > * {
    flex: 1;
}
Run Code Online (Sandbox Code Playgroud)

请记住,通常不能将width: ... flex:1;一起用于单个子项。