Search code examples
htmlcssflexboxsticky-footer

Flexbox Sticky Footer - 'Flex: 1' Not Working to Fill Height


I'm trying to test out the flexbox sticky footer method at the moment for a dynamic-height footer, something I've had problems with for a while, but I'm having trouble with the main section not expanding to fill the full height of the window using 'flex: 1', after applying 'min-height: 100vh' and 'flex-direction: column' to the containing body.

This is leaving the footer still half way up the page and I can't figure out after reading up on it why this shouldn't be working.

HTML:

    <body>

    <div id="wrapper">

        <main>

            <div id="pageContent">

                <h1>Page Content</h1>

                <h2>Page Content</h2>

                <h3>Page Content</h3>

                <h4>Page Content</h4>

            </div>

        </main>

        <footer>

            <row>

                <p>Footer Content</p>

                <p>Footer Content</p>

                <p>Footer Content</p>

                <p>Footer Content</p>

            </row>

        </footer>

    </div>

</body>

CSS:

html, body
{
margin: 0;
padding: 0;
max-width: 100%;
background: #1a1a1a;
}

body
{
display: flex;
min-height: 100vh;
flex-direction: column;
}

main
{
width: 100%;
margin: 0;
padding: 50px 10%;
box-sizing: border-box;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
flex: 1;
}

main > #pageContent
{
height: 100%;
width: 100%;
margin: 0;
}

footer
{
height: auto;
background: #0d0d0d;
padding: 0 10%;
margin: 0;
}

footer > row
{
display: flex;
}

footer > row > p
{
flex-grow: 1;
flex-basis: 0;
text-align: center;
padding: 25px 0;
margin: 0;
}

Solution

  • You haven't applied display:flex to the #wrapper div.

    Flexbox is not inherited.

    html,
    body {
      margin: 0;
      padding: 0;
      max-width: 100%;
    }
    body {
      display: flex;
      min-height: 100vh;
      flex-direction: column;
    }
    #wrapper {
      flex: 1;
      display: flex;
      flex-direction: column;
    }
    main {
      width: 100%;
      margin: 0;
      padding: 50px 10%;
      box-sizing: border-box;
      -webkit-box-sizing: border-box;
      -moz-box-sizing: border-box;
      flex: 1;
    }
    main > #pageContent {
      height: 100%;
      width: 100%;
      margin: 0;
    }
    footer {
      height: auto;
      background: lightgreen;
      padding: 0 10%;
      margin-top: auto;
    }
    footer > row {
      display: flex;
    }
    footer > row > p {
      flex-grow: 1;
      flex-basis: 0;
      text-align: center;
      padding: 25px 0;
      margin: 0;
    }
    <div id="wrapper">
    
      <main>
    
        <div id="pageContent">
    
          <h1>Page Content</h1>
    
          <h2>Page Content</h2>
    
          <h3>Page Content</h3>
    
          <h4>Page Content</h4>
    
        </div>
    
      </main>
    
      <footer>
    
        <row>
    
          <p>Footer Content</p>
    
          <p>Footer Content</p>
    
          <p>Footer Content</p>
    
          <p>Footer Content</p>
    
        </row>
    
      </footer>
    
    </div>

    Codepen Demo