How to stretch children to fill cross-axis?

CssFlexbox

Css Problem Overview


I have a left-right flexbox:

<div class="wrapper">
  <div class="left">Left</div>
  <div class="right">Right</div>
</div>

.wrapper {
  display: flex;
  flex-direction: row;
  align-items: stretch;
  width: 100%;
  height: 70vh;
  min-height: 325px; 
  max-height:570px; 
}

The problem is that the right child is not behaving responsively. To be specific, I want it to fill the height of the wrapper.

How to accomplish this?

Css Solutions


Solution 1 - Css

  • The children of a row-flexbox container automatically fill the container's vertical space.

  • Specify flex: 1; for a child if you want it to fill the remaining horizontal space:

.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;
}

<div class="wrapper">
  <div class="left">Left</div>
  <div class="right">Right</div>
</div>

  • Specify flex: 1; for both children if you want them to fill equal amounts of the horizontal space:

.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;
}

<div class="wrapper">
  <div class="left">Left</div>
  <div class="right">Right</div>
</div>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDeborah ColeView Question on Stackoverflow
Solution 1 - CssAlex ShesterovView Answer on Stackoverflow