24

I have a dynamic footer on my page and I expect the content above it to scale to reach the top of the footer, that's fine and all, but I'm having a problem:

#snippet-container {
  height: 300px;
  width: 200px;
}

#page {
  display: flex;
  flex-flow: column;
  height: 100%;
}

#content {
  flex: 1 1 auto;
  background: blue;
}

#content .test {
  width: 100%;
  height: 100%;
  background: yellow;
  border: 2px solid red;
}

#footer {
  flex: 0 1 10vh;
  background: black;
}
<div id="snippet-container">
  <div id="page">
    <div id="content">
      <div class="test"></div>
    </div>
    <div id="footer"></div>
  </div>
</div>

I would expect the .test element to fill the #content element, hence the 100%/100% width/height however it does not.

You can see this reflected as I gave the .test element a red border in the snippet.

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
Hobbyist
  • 15,888
  • 9
  • 46
  • 98

2 Answers2

25

Problem

The problem is that your #page flex container is not reaching the .test element since .test is not a child, it is a descendant of the flex item.

The contents of a flex container consists of zero or more flex items: each child of a flex container becomes a flex item.

Each flex item is affected by the flex container, but the flex item's descendants are not.

Solution

You need to add display: flex to your #content as well.

JSFiddle

Code snippet

#snippet-container {
  height: 300px;
  width: 200px;
}
#page {
  display: flex;
  flex-flow: column;
  height: 100%;
}
#content {
  display: flex;
  height: 100%;
  background: blue;
}
#content .test {
  width: 100%;
  background: yellow;
  border: 2px solid red;
}
#footer {
  flex: 0 1 10vh;
  background: black;
}
<div id="snippet-container">
  <div id="page">
    <div id="content">
      <div class="test"></div>
    </div>
    <div id="footer"></div>
  </div>
</div>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ricky Ruiz
  • 25,455
  • 6
  • 44
  • 53
  • This solution works only for the direct flex child, but if the item you need to have 100% is wrapped in more div elements then each of them needs height:100%, https://jsfiddle.net/mwx1k3b3/398/ – Hrvoje Golcic Jul 31 '18 at 13:54
9

This does it for you:

https://jsfiddle.net/wjr0wwht/2/

In order to make the 'test' div grow to full height, you need to make content also a display: flex like this:

#content {
  flex: 1 1 auto;
  background: blue;
  display: flex;
  flex-direction: column;
}

And then make the test itself grow:

#content .test {
  background: yellow;
  border: 2px solid red;
  flex-grow: 1;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sifriday
  • 4,342
  • 1
  • 13
  • 24