如何指定多行flexbox布局的行高并使其余行拉伸

时间:2015-07-13 19:54:47

标签: css flexbox

我有一个多行的flexbox布局,我想指定其中一行的高度,并将剩余的行拉伸以填充。如果我设置下面蓝色div的高度,我会得到一堆空的空间。

enter image description here



#container {
  display: flex;
  flex-wrap: wrap;
  height: 500px;
  width: 500px;
}
#one,
#two,
#four,
#five {
  flex: 1 1 49%;
}
#three {
  flex: 1 1 99%;
  background-color: blue;
  height: 15px;
  max-height: 15px;
}
#one {
  background-color: green;
}
#two {
  background-color: yellow;
}
#four {
  background-color: orange;
}
#five {
  background-color: violet;
}

<div id="container">
  <div id="one"></div>
  <div id="two"></div>
  <div id="three"></div>
  <div id="four"></div>
  <div id="five"></div>
</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

据我所知,这将需要行。

#container {
  display: flex;
  flex-wrap: nowrap;
  height: 500px;
  width: 500px;
  flex-direction: column;
}
.row {
  flex: 1 0 auto;
  display: flex;
  flex-direction: row;
}
.row:nth-child(2) {
  height: 15px;
  max-height: 15px;
}
.row div {
  flex: 1 0 auto;
}
#one {
  background-color: green;
}
#two {
  background-color: yellow;
}
#three {
  background-color: blue;
}
#four {
  background-color: orange;
}
#five {
  background-color: violet;
}
<div id="container">
  <div class="row">
    <div id="one"></div>
    <div id="two"></div>
  </div>
  <div class="row">
    <div id="three"></div>
  </div>
  <div class="row">
    <div id="four"></div>
    <div id="five"></div>
  </div>
</div>

JSfiddle Demo

答案 1 :(得分:1)

如果您知道将有三行,则可以使用calc

#three {
  height: 15px;
}
#one, #two, #four, #five {
  height: calc((100% - 15px) / 2);
}

&#13;
&#13;
#container {
  display: flex;
  flex-wrap: wrap;
  height: 500px;
  width: 500px;
}
#one, #two, #four, #five {
  flex: 1 1 49%;
  height: calc((100% - 15px) / 2);
}
#three {
  flex: 1 1 99%;
  background-color: blue;
  height: 15px;
}
#one { background-color: green; }
#two { background-color: yellow; }
#four { background-color: orange; }
#five { background-color: violet; }
&#13;
<div id="container">
  <div id="one"></div>
  <div id="two"></div>
  <div id="three"></div>
  <div id="four"></div>
  <div id="five"></div>
</div>
&#13;
&#13;
&#13;