Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a sticky footer using flexbox in IE11?

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?

like image 475
ssougnez Avatar asked Jun 22 '17 12:06

ssougnez


People also ask

How do I make my footer stick to the bottom of my flex?

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.

Does Flexbox work in ie11?

Note also that Internet Explorer 11 supports the modern display: flex specification however it has a number of bugs in the implementation.

What are sticky footers?

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.


2 Answers

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>
like image 78
Asons Avatar answered Sep 30 '22 20:09

Asons


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:

  • flex property not working in IE
like image 28
Michael Benjamin Avatar answered Sep 30 '22 20:09

Michael Benjamin