Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Sticky Footers with Unknown Height

Is there any way to stick a footer to the bottom of the browser screen or right after the content (depending on which is longer) using CSS without knowing the size of the footer in advance?

Right now I am using the absolute positioning in a container that holds the footer and the content with container's min-height as 100%, but if I change the footer I find I must change the padding at the bottom of the container to match its height.

like image 378
Loyal Tingley Avatar asked Dec 03 '10 15:12

Loyal Tingley


People also ask

How do I make my footer sticky CSS?

Method 2: (fixed height footer) Apply display:flex and flex-direction:column to the body . Apply margin-top:auto the footer . You're done, because auto margins inside flex containers absorb all available free space, making the footer stick to the bottom.

How do I make my footer sticky to the bottom in CSS?

Using FlexboxSet the body display to flex display: flex; and flex-direction to column flex-direction: column; . Select footer element (of whatever you want to stick to bottom) and set top margin to auto margin-top: auto; .

How do I stick footer to the bottom when page content is less?

Quick answer: Add “display:flex; flex-direction:column; min-height:100vh;” to body or appropriate layout body element, then add “flex:1;” to content wrapper element/section. I will explain few ways to keep or stick the footer to the bottom of the page if there is not enough content.


2 Answers

http://pixelsvsbytes.com/blog/2011/09/sticky-css-footers-the-flexible-way/

Summary:

For a site with a header, content area, and footer:

  1. Set html, body {height: 100%;}

  2. Set your body (or a wrapper div) to display: table; width: 100%; height: 100%;

  3. Set your header, footer, and content area to display: table-row;. Give your header and footer height: 1px;, and give your content area height: auto;

    The header and footer will both expand to fit their content. The content area will expand to fit the larger of its content, or the available space.

https://jsfiddle.net/0cx30dqf/

like image 185
Igor Alekseev Avatar answered Oct 24 '22 19:10

Igor Alekseev


If you're willing to jump into the HTML5 future, you can use flexbox...

body {     display: flex;     min-height: 100vh;     flex-direction: column; }  main {    flex: 1; } 
  • My more detailed answer to the same question: How to make a fluid sticky footer (full example: http://jsfiddle.net/n5BaR/)

  • Solved by flexbox: http://philipwalton.github.io/solved-by-flexbox/demos/sticky-footer/

  • What is Flexbox from MDN: https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Flexible_boxes

like image 30
Luke Avatar answered Oct 24 '22 18:10

Luke