Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a variable-height sticky footer be defined in pure CSS?

Tags:

css

The key to note here is the height of the footer is not going to be fixed, but will vary with its content.

When I say “sticky footer,” I use it in what I understand to be the common definition of “a footer that is never higher than the bottom of the viewport, but if there is enough content, it will be hidden until the user scrolls down far enough to see it.”

Note also I don’t need to support legacy browsers. If CSS display: table & related properties help here, they are fair game.

like image 364
Alan H. Avatar asked May 31 '12 00:05

Alan H.


People also ask

What is a sticky footer?

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.

How do I keep the footer at the bottom of the page with dynamic height?

Just add data-position="fixed" to the div tag if you are using jQuery. Hope this helps.


2 Answers

All other solutions here are out of date and either use JavaScript, or table hacks.

With the advent of the CSS flex model, solving the variable-height sticky footer problem becomes very, very easy: while mostly known for laying out content in the horizontal direction, Flexbox actually works just as well for vertical layout problems. All you have to do is wrap the vertical sections in a flex container and choose which ones you want to expand. They'll automatically take up all the available space in their container.

Note how simple the markup and the CSS are. No table hacks or anything.

The flex model is supported by 96%+ of the browsers in use today.

html, body {    height: 100%;    margin: 0; padding: 0;  /* to avoid scrollbars */  }    #wrapper {    display: flex;  /* use the flex model */    min-height: 100%;    flex-direction: column;  /* learn more: http://philipwalton.github.io/solved-by-flexbox/demos/sticky-footer/ */  }    #header {    background: yellow;    height: 100px;  /* can be variable as well */  }    #body {    flex: 1;    border: 1px solid red;  }    #footer{    background: lime;  }
<div id="wrapper">    <div id="header">Title</div>    <div id="body">Body</div>    <div id="footer">      Footer<br/>      of<br/>      variable<br/>      height<br/>    </div>  </div>
like image 50
Dan Dascalescu Avatar answered Oct 05 '22 04:10

Dan Dascalescu


You can absolutely do this in pure CSS. Clicky the linky.

This concept uses display: table-cell organize your page sections rather than the normal display: block.

HTML

<body class="Frame">     <header class="Row"><h1>Catchy header</h1></header>     <section class="Row Expand"><h2>Awesome content</h2></section>     <footer class="Row"><h3>Sticky footer</h3></footer> </body> 

CSS

.Frame {     display: table;     table-layout: fixed;     height: 100%;     width: 100%; } .Row {     display: table-row;     height: 1px; } .Row.Expand {     height: auto; } 
like image 26
cereallarceny Avatar answered Oct 05 '22 03:10

cereallarceny