How to specify row height of multi-line flexbox layout and have remaining rows stretch

只愿长相守 提交于 2021-02-08 04:38:32

问题


I have a multi-line flexbox layout where I would like to specify the height of one of the rows and have the remaining rows stretch to fill. If I set the height of the blue div below, I get a bunch of empty space.

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>

回答1:


As far as i know, this will require rows.

#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




回答2:


If you know there will be exactly three rows, you can use calc:

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

#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; }
<div id="container">
  <div id="one"></div>
  <div id="two"></div>
  <div id="three"></div>
  <div id="four"></div>
  <div id="five"></div>
</div>


来源:https://stackoverflow.com/questions/31392254/how-to-specify-row-height-of-multi-line-flexbox-layout-and-have-remaining-rows-s

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!