2

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?

Sunny Patel
  • 7,830
  • 2
  • 31
  • 46
enkryptor
  • 1,574
  • 1
  • 17
  • 27

3 Answers3

1

Set display:flex on the .row.content div to make it into a flex container

Since the default value of align-items is stretch - the items will take up full height.

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;
  display: flex; /* <----- added */
}

.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="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>
Danield
  • 121,619
  • 37
  • 226
  • 255
1

You can also set the child element to use position: absolute (and use position: relative on the parent).

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;
  position: relative;
}

.box .row.content div {
  height: 100%;
  width: 90%;
  background-color:red;
  position: absolute;
}

.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>
      Should fill the remaining space
    </div>
  </div>
  <div class="row footer">
    <p><b>footer</b> (fixed height)</p>
  </div>
</div>
Sunny Patel
  • 7,830
  • 2
  • 31
  • 46
1

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

Geeky
  • 7,420
  • 2
  • 24
  • 50