enkryptor enkryptor - 9 days ago 6
CSS Question

Can element's height be changed, providing it is inside a flexbox?

In the following snippet I want the red area to have the same height as the yellow one:



html,
body {
height: 100%;
margin: 0
}

.box {
display: flex;
flex-flow: column;
height: 100%;
}

.box .row {
border: 1px dotted grey;
}

.box .row.header {
flex: 0 1 auto;
/* The above is shorthand for:
flex-grow: 0,
flex-shrink: 1,
flex-basis: auto
*/
}

.box .row.content {
flex: 1 1 auto;
background-color:yellow;
}

.box .row.footer {
flex: 0 1 40px;
}

<div class="box">
<div class="row header">
<p><b>header</b>
<br />
<br />(sized to content)</p>
</div>
<div class="row content">
<div style="height:100%; width:90%; background-color:red;">
Should fill the remaining space
</div>
</div>
<div class="row footer">
<p><b>footer</b> (fixed height)</p>
</div>
</div>





It has
style="height:100%
however it acts like it has
height:auto
, presumably because of the flexbox. Can I adjust its height somehow?

Answer

You have flex-basis on content as 100%

html,
body {
  height: 100%;
  margin: 0
}
.box {
  display: flex;
  flex-flow: column;
  height: 100%;
}
.box .row {
  border: 1px dotted grey;
}
.box .row.header {
  flex: 0 1 auto;
  /* The above is shorthand for:
  flex-grow: 0,
  flex-shrink: 1,
  flex-basis: auto
  */
}
.box .row.content {
  flex: 1 1 100%;
  background-color: yellow;
}
.box .row.footer {
  flex: 0 1 40px;
}
<div class="box">
  <div class="row header">
    <p><b>header</b>
      <br />
      <br />(sized to content)</p>
  </div>
  <div class="row content">
    <div style="height:100%; width:90%; background-color:red;">
      Should fill the remaining space
    </div>
  </div>
  <div class="row footer">
    <p><b>footer</b> (fixed height)</p>
  </div>
</div>

Hope it helps