23

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.

misterManSam
  • 23,481
  • 10
  • 68
  • 87
Hobbyist
  • 15,138
  • 9
  • 42
  • 93

2 Answers2

24

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>
Ricky
  • 22,575
  • 5
  • 39
  • 49
  • 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
10

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 test itself grow:

#content .test {
  background: yellow;
  border: 2px solid red;
  flex-grow: 1;
} 
sifriday
  • 4,234
  • 1
  • 10
  • 23