Fill remaining vertical space with CSS using display:flex
Asked Answered
D

6

362

In a 3-row layout:

  • the top row should be sized according to its contents
  • the bottom row should have a fixed height in pixels
  • the middle row should expand to fill the container

The problem is that as the main content expands, it squishes the header and footer rows:

Flexing Bad

section {
  display: flex;
  flex-flow: column;
  align-items: stretch;
  height: 300px;
}
header {
  flex: 0 1 auto;
  background: tomato;
}
div {
  flex: 1 1 auto;
  background: gold;
  overflow: auto;
}
footer {
  flex: 0 1 60px;
  background: lightgreen;
  /* fixes the footer: min-height: 60px; */
}
<section>
  <header>
    header: sized to content
    <br>(but is it really?)
  </header>
  <div>
    main content: fills remaining space<br>
    x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
    <!-- uncomment to see it break - ->
    x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
    x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
    x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
    x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
    <!-- -->
  </div>
  <footer>
    footer: fixed height in px
  </footer>
</section>

Fiddle:

I'm in the lucky situation that I can use the latest and greatest in CSS, disregarding legacy browsers. I thought I could use the flex layout to finally get rid of the old table-based layouts. For some reason, it's not doing what I want...

For the record, there are many related questions on SO about "filling the remaining height", but nothing that solves the problem I'm having with flex. Refs:

Dogwatch answered 2/8, 2014 at 18:15 Comment(6)
Seems to be working as expected on the fiddle.Phyllis
Yes, you need to uncomment the rest of the <div>'s content to see how it breaks. Maybe I should have linked the broken version. Sorry.Dogwatch
I have added both versions to the question now.Dogwatch
I see what you mean now.Phyllis
dup of #90678?Barrack
@JonathanStray Yes, technically it probably is closable now. I'd consult with some other CSS gold badgers though before mjolniring it, personally.Frediafredie
H
412

Make it simple : DEMO

section {
  display: flex;
  flex-flow: column;
  height: 300px;
}

header {
  background: tomato;
  /* no flex rules, it will grow */
}

div {
  flex: 1;  /* 1 and it will fill whole space left if no flex value are set to other children*/
  background: gold;
  overflow: auto;
}

footer {
  background: lightgreen;
  min-height: 60px;  /* min-height has its purpose :) , unless you meant height*/
}
<section>
  <header>
    header: sized to content
    <br/>(but is it really?)
  </header>
  <div>
    main content: fills remaining space<br> x
    <br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
    <!-- uncomment to see it break -->
    x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br> x
    <br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br> x
    <br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br> x
    <br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
    <!-- -->
  </div>
  <footer>
    footer: fixed height in px
  </footer>
</section>

Full screen version

section {
  display: flex;
  flex-flow: column;
  height: 100vh;
}

header {
  background: tomato;
  /* no flex rules, it will grow */
}

div {
  flex: 1;
  /* 1 and it will fill whole space left if no flex value are set to other children*/
  background: gold;
  overflow: auto;
}

footer {
  background: lightgreen;
  min-height: 60px;
  /* min-height has its purpose :) , unless you meant height*/
}

body {
  margin: 0;
}
<section>
  <header>
    header: sized to content
    <br/>(but is it really?)
  </header>
  <div>
    main content: fills remaining space<br> x
    <br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
    <!-- uncomment to see it break -->
    x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br> x
    <br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br> x
    <br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br> x
    <br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
    <!-- -->
  </div>
  <footer>
    footer: fixed height in px
  </footer>
</section>
Hertfordshire answered 2/8, 2014 at 19:11 Comment(14)
Thanks again. Now I have another question about your solution, if you're interested. It's too long for a comment, so I opened a new topic: stackoverflow.com/questions/25105765Dogwatch
What if we want section to have a 100% height?Aoudad
@PaulTotzke Then it is morelike another question , you just need to set height to 100%. As usual , parents needs an height set/usable, else we have the classic 100% of 'null' example for code above : html,body,section {height:100%;} where section is direct child of body jsfiddle.net/7yLFL/445 this gives a header and footer fixed.Hertfordshire
This doesn't work if you want a background image in the div. Technically the div goes past the footerVinegar
@Vinegar do you you have an example to show ? (where you can also show/see the div past the footer :) ) edit like this ? jsfiddle.net/7yLFL/868 ?Hertfordshire
how can i remove the vertical scroll in this fiddle jsfiddle.net/link2pk/wofu7n6d/6 . The content should take the remaining space available. Can anyone help me with this?Tragus
@link2pk you need to reset margin on body jsfiddle.net/5bacx1geHertfordshire
@eliya before your edit it was flex:1. Any reason you changed it to 2? Also, you have removed some valuable comments from the css IMOTwain
@PaulTotzke I just replaced the fixed height on section css by the viewport 100%: height: 100vh;. It worked for me on Chrome and SafariRetentive
How is this the accepted answer, it doesn't answer the question and the demo's do not demonstrate hw fill the remaining vertical space.Sumptuary
@JonMiles can you explain what is not working for you ? flex is nowdays widely implemented, so i hardly believe this is not working for you. flex:1 is a shorthand propertie BTW. demos, snippets or jsfiddle linked are working fine. Or maybe you issue is something else and this answer has nothing to do with your own issue. Have fun coding anyway. (shortest fiddle : jsfiddle.net/5bacx1ge )Hertfordshire
This possibly needs a different question but how would you change the 'full-screen' answer if there was a separate banner element of fixed height above the header?Selfinductance
@Selfinductance Hello, Do you mean something like this ? jsfiddle.net/hsuq62t0 or jsfiddle.net/3kac1rtx ?Hertfordshire
@Selfinductance okay, i saw your question :) use flex-grow not height for children ;) jsfiddle.net/d5yb2v9g/1Hertfordshire
T
24

The example below includes scrolling behaviour if the content of the expanded centre component extends past its bounds. Also the centre component takes 100% of remaining space in the viewport.

jsfiddle here

html, body, .r_flex_container{
    height: 100%;
    display: flex;
    flex-direction: column;
    background: red;
    margin: 0;
}
.r_flex_container {
    display:flex;
    flex-flow: column nowrap;
    background-color:blue;
}

.r_flex_fixed_child {
    flex:none;
    background-color:black;
    color:white;

}
.r_flex_expand_child {
    flex:auto;
    background-color:yellow;
    overflow-y:scroll;
}

Example of html that can be used to demonstrate this behaviour

<html>
<body>
    <div class="r_flex_container">
      <div class="r_flex_fixed_child">
        <p> This is the fixed 'header' child of the flex container </p>
      </div>
      <div class="r_flex_expand_child">
            <article>this child container expands to use all of the space given to it -  but could be shared with other expanding childs in which case they would get equal space after the fixed container space is allocated. 
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,
            </article>
      </div>
      <div class="r_flex_fixed_child">
        this is the fixed footer child of the flex container
        asdfadsf
        <p> another line</p>
      </div>

    </div>
</body>
</html>
Tackle answered 10/2, 2016 at 4:51 Comment(2)
This can be slightly simplified by removing html from the selector and appyling height: 100vh to body specifically: jsfiddle.net/pm6nqcqh/1Jump
I did notice a difference between flex:auto and flex:1 on the expandable child. With flex:auto, other children seem to shrink when needed, with flex:1 they don't (in Chrome)Alveolus
I
14

Use the flex-grow property to the main content div and give the display: flex; to its parent;

body {
    height: 100%;
    position: absolute;
    margin: 0;
}
section {
  height: 100%;
  display: flex;
  flex-direction : column;
}
header {
  background: tomato;
}
div {
  flex: 1; /* or flex-grow: 1  */;
  overflow-x: auto;
  background: gold;
}
footer {
  background: lightgreen;
  min-height: 60px;
}
<section>
  <header>
    header: sized to content
    <br>(but is it really?)
  </header>
  <div>
    main content: fills remaining space<br>
    x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
    x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
    x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
    x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
    x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
  </div>
  <footer>
    footer: fixed height in px
  </footer>
</section>
Inartistic answered 26/8, 2019 at 10:27 Comment(2)
Is there any other solution to this problem (where section height isn't fixed?) rather than using position: absolute?Photojournalism
@ben If you know the height of the element. Then you can avoid using position: absolute; . https://jsfiddle.net/xa26brzf/Inartistic
D
12

A more modern approach would be to use the grid property.

section {
  display: grid;
  align-items: stretch;
  height: 300px;
  grid-template-rows: min-content auto 60px;
}
header {
  background: tomato;
}
div {
  background: gold;
  overflow: auto;
}
footer {
  background: lightgreen;
}
<section>
  <header>
    header: sized to content
    <br>(but is it really?)
  </header>
  <div>
    main content: fills remaining space<br>
    x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
    
    x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
    x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
    x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
    x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
    
  </div>
  <footer>
    footer: fixed height in px
  </footer>
</section>
Deodand answered 13/8, 2017 at 14:1 Comment(1)
This is overflowing past the height of the viewportSelfinductance
E
8

This is the simplest example that I can think of. The key is

  1. Parent is display:flex
  2. Child has flex-grow:1
  3. Parent MUST have height specified. If you specify height:100% on a parent div, then remember, the parent exists in a <body> and you'll see the body isn't 100% unless you add height:100% on the body too.

http://jsfiddle.net/Ljbzsmvf/2/

div#parent {
  height: 300px;
  display: flex;
  flex-direction: column;
}

div#child {
  border: thin solid red;
  flex-grow: 1;
}
<div id='parent'>
  Parent
  <div id='child'>
    Child
  </div>
</div>
Ephemeral answered 29/10, 2021 at 9:37 Comment(0)
M
1

Here is the codepen demo showing the solution:

Important highlights:

  • all containers from html, body, ... .container, should have the height set to 100%
  • introducing flex to ANY of the flex items will trigger calculation of the items sizes based on flex distribution:
    • if only one cell is set to flex, for example: flex: 1 then this flex item will occupy the remaining of the space
    • if there are more than one with the flex property, the calculation will be more complicated. For example, if the item 1 is set to flex: 1 and the item 2 is se to flex: 2 then the item 2 will take twice more of the remaining space
  • Main Size Property
Mooney answered 30/7, 2020 at 13:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.