I'm trying to make a simple design with flexbox but I'm having trouble with IE11. Basically, I want a footer that sticks to the bottom only if the content is not high enough. I have no issue doing that with Chrome like this:
*,
*:after,
*:before {
box-sizing: border-box;
}
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
body {
display: flex;
flex-direction: column;
}
main {
flex: 1;
}
<header>
Header
</header>
<main>
<p>Main</p>
<p>Main</p>
<p>Main</p>
<p>Main</p>
<p>Main</p>
</main>
<footer>
Footer
</footer>
Just play with the number of <p>main</p>
to see the wrong behaviour with IE11.
Is there a way to achieve this without JavaScript?
Set margin on the footer Because you set the Body — the footer's parent element — to Flex, you can set the top margin on the footer element to Auto. This makes the footer push away from the content above, causing the footer to stick to the bottom of the page.
Note also that Internet Explorer 11 supports the modern display: flex specification however it has a number of bugs in the implementation.
A sticky footer pattern is one where the footer of your page "sticks" to the bottom of the viewport in cases where the content is shorter than the viewport height.
IE has a min-height
bug and needs display: flex
on the flex column containers parent, in this case the html
Fiddle demo
Update your CSS like this
*,
*:after,
*:before {
box-sizing: border-box;
}
html, body {
margin: 0;
display: flex;
}
body {
flex-direction: column;
min-height: 100vh;
}
main {
flex-grow: 1;
}
<header>
Header
</header>
<main>
<p>Main</p>
<p>Main</p>
<p>Main</p>
<p>Main</p>
<p>Main</p>
</main>
<footer>
Footer
</footer>
On main
, instead of flex: 1
use flex: auto
. That should be all you need.
The flex: 1
shorthand rule breaks down to:
flex-grow: 1
flex-shrink: 1
flex-basis: 0
The flex: auto
shorthand rule breaks down to:
flex-grow: 1
flex-shrink: 1
flex-basis: auto
IE has trouble parsing flex-basis: 0
.
More information:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With