46

I have a page with only a couple of lines of content. I want the footer to be pushed to the bottom.

<div id="footer"></div>

I don't want to use

#footer
{
    position:fixed;
    bottom:0;
}

AKA Sticky Footer

Is this possible without jQuery?

any suggestions?

Blu
  • 4,036
  • 6
  • 38
  • 65
pom4ik
  • 463
  • 1
  • 4
  • 5

9 Answers9

53

This Flexbox solution is neater and far easier to implement:

HTML

<body>
  <div class="content">
    content
  </div>
  <footer class="footer"></footer>
</body>

CSS

html, body {
  height: 100%;
}
body {
  display: flex;
  flex-direction: column;
}
.content {
  flex: 1 0 auto;
}
.footer {
  flex-shrink: 0;
}

Just ensure you wrap the necessary divs inside the body.

Chidozie Nnachor
  • 872
  • 10
  • 21
  • 6
    This should have been the accepted answer. Flexbox is the right approach to be taken to solving this footer problem. It still allows you to keep your footer inside wrapper div (which is a standard design practice) and absolutely prevents you from making absurd solutions such as setting the footer's position to absolute. – Michael Jun 29 '20 at 15:03
  • 3
    This should have been the accepted answer. Easy to implement, simple, few changes to the existing code, works well with Bootstrap. – Zeron Mar 02 '21 at 21:14
  • While it _should_ be the accepted answer, I'm not sure it could have been at the time the accepted answer was posted. The question only dates to the year after the current Flexbox specification. Not sure the browser support would have been there. – Auspex May 06 '21 at 11:30
38

Update 2021 - CSS GRID

Here is a solution using CSS Grid, this is by far the best way to do it on 2021.

html, body {
    margin: 0;
    height: 100%;
}
body {
    display: grid;
    grid-gap: 10px;
    grid-template-columns: 1fr;
    grid-template-areas: "main" "footer";
    grid-template-rows: 1fr 80px;
}
main {
    background-color: #F8BBD0;
    grid-area: main;
}
footer {
    background-color: #7E57C2;
    grid-area: footer;
}
<body>
    <main>The content</main>
    <footer>Footer</footer>
</body>

Old Answer

There is another sticky footer by Ryan Fait that doesn't use position fixed:

* {
    margin: 0;
}
html, body {
    height: 100%;
}
.wrapper {
    min-height: 100%;
    height: auto !important; /* This line and the next line are not necessary unless you need IE6 support */
    height: 100%;
    margin: 0 auto -155px; /* the bottom margin is the negative value of the footer's height */
}
.footer, .push {
    height: 155px; /* .push must be the same height as .footer */
}
Vinícius Moraes
  • 3,506
  • 2
  • 22
  • 24
  • 2
    This is an awesome pure CSS solution. Footer stays below the content if the content height is more than the browser window, but stays at the bottom of the browser window if there is less content. – Deborah May 21 '13 at 20:51
  • perfect! I guess this wont work on IE8 as it is using negative margin. – pom4ik May 22 '13 at 02:12
  • 1
    It works great in ie8. Try it. I'm not sure why IE8 has negative margin issues sometimes and not others, but it seems to be influenced by poorly constructed html/css. – Deborah May 22 '13 at 04:28
  • 1
    This method can be improved by using an :after selector on a wrapper inside the main wrapper and then deleting the push element. Supported for ie8+ – Mike Oram May 16 '14 at 13:56
  • 2
    What about variable height footers? – Dustin Graham Jan 09 '17 at 19:32
  • 15
    This is not a complete example. I have no idea where to put `.push` and whether `.wrapper` contains the `.footer` or not. I'll remove my downvote if you fix this. – Adam Arold Jun 11 '17 at 16:32
  • You can get the full example in the link he provided https://css-tricks.com/snippets/css/sticky-footer – Pnar Sbi Wer Jan 15 '20 at 01:53
  • Isn't Flexbox the most elegant and versatile solution nowadays? The one above assumes one knows the footer size in advance. – zendka Feb 04 '20 at 17:17
  • 1
    It is not advised to put a link to an example in SO, as the resource is likely to return a 404 in a year or two. I am sure we all have experienced this in the past. – Leogout Nov 30 '20 at 10:07
15

Here is a solution that does not require that the footer be placed outside of the main wrapper element, which is how most people structure their pages.

html,
body {
  margin: 0;
  height: 100%;
}

.wrapper {
  box-sizing: border-box;
  position: relative;
  padding-bottom: 1em; /* Height of footer */
  min-height: 100%;
}

header {
  background-color: #cff;
}

footer {
  position: absolute;
  bottom: 0;
  width: 100%;
  color: #fff;
  background-color: #000;
}
<div class="wrapper">
  <header>I am the header.</header>
  <article>I am content that doesn't fill the page. The footer will appear at the bottom of the browser window. However, when I do fill the page, you will need to scroll down to see the footer.</article>
  <footer>I am the footer.</footer>
</div>

Explanation

The wrapper element will fill 100% of the viewport height. (You could also use 100vh for the wrapper if you don't want to set the height of the html and body elements.) The wrapper also has a bottom padding to create a placeholder for the footer to sit.

The footer is absolutely positioned to the bottom of the wrapper and sits in the placeholder created by the wrapper's bottom padding.

This means that when the page does not have scrollbars, the footer will be positioned at the very bottom. However, when there is enough content for scrollbars to appear, the footer will be pushed down below the content.

(The color and background-color CSS properties in the example are for decoration only, obviously. They are included so that when you run the code, you can clearly see the separated sections.)

Thomas Higginbotham
  • 1,662
  • 20
  • 25
5

Try Sticky Footer Solution by Steve Hatcher

/*  
Sticky Footer Solution
by Steve Hatcher 
http://stever.ca
http://www.cssstickyfooter.com
*/

* {
    margin: 0;
    padding: 0;
}

/* must declare 0 margins on everything, also for main layout components use padding, not 
vertical margins (top and bottom) to add spacing, else those margins get added to the total height 
and your footer gets pushed down a bit more, creating vertical scroll bars in the browser */

html, body {
    height: 100%;
}

#wrap {
    min-height: 100%;
}

#main {
    overflow: auto;
    padding-bottom: 180px;
}

/* must be same height as the footer */

#footer {
    position: relative;
    margin-top: -180px; /* negative value of footer height */
    height: 180px;
    clear: both;
}

/*Opera Fix*/
body:before {
    /* thanks to Maleika (Kohoutec)*/
    content: "";
    height: 100%;
    float: left;
    width: 0;
    margin-top: -32767px; /* thank you Erik J - negate effect of float*/
}

/* IMPORTANT

You also need to include this conditional style in the <head> of your HTML file to feed this style to IE 6 and lower and 8 and higher.

<!--[if !IE 7]>
    <style type="text/css">
        #wrap {display:table;height:100%}
    </style>
<![endif]-->

*/
KB.
  • 3,549
  • 4
  • 23
  • 29
Konstantin Tarkus
  • 37,618
  • 14
  • 135
  • 121
  • 1
    The link is broken. – Adam Arold Jun 11 '17 at 16:34
  • Avoid that link. I've put in an edit for review. As of 11/2017 it was redirecting to those scam pages claiming you computer has a virus etc. – KB. Nov 20 '17 at 09:22
  • Why do we always reference "steve hatcher" for this snippet as if it was somebody famous or something? I didn't know we can add our signature to a snippet like this forever – lapin Jul 01 '19 at 09:04
2

Another way to do this if you don't know the footer size is to use javascript and css

html, body{
    height:100%;
    height:100%;
}

#footer{
    background-color: #292c2f !important;
    position:absolute;bottom:0px;
}

and Javascript part

$(document).ready(function(){
        if ($(document).height() > $(window).height()) {
            $('#footer').css('position', 'relative');
        }
});

You can do this with another approach just easily by setting min-height on the tag before your footer tag.

.the-tag-before-footer{
     min-height:30%;
 } 
Afshin Izadi
  • 527
  • 1
  • 6
  • 13
2

I tried a lot of approaches, but results were different when page was totally fill or not. The simplest and efficient solution is to use flex.

html, body {height: 100%;}
body {display: flex; flex-direction: column;}
.content {flex: 1 0 auto; padding: 20px;}
.footer {flex-shrink: 0; padding: 20px;}
<div class="content">
  <h1>The GOAT Footer with Flexbox</h1>
  <p>You can add content to test with a full page</p>
</div>
<footer class="footer">
  The GOAT Footer 
</footer>

Credits to CSS Trick

0

First wrap all of your main content in a div element and give it a class of “wrapper” (or call it whatever you want).

HTML:

<body>
    <div class="wrapper">
        <h1>Main Content</h1>
    </div>
    <footer>
        <p>Footer Content</p>
    </footer>
</body>

Now, make sure you give your footer a height.

Then use the calc() function to set the height of your wrapper equal to the height of the viewport (display), minus the height of the footer.

.wrapper {
    min-height: calc(100vh - 50px);
}
footer {
    height: 50px;
}

Now, if you have extra margins on your wrapper content you will have to increase the amount of pixels you subtract from the viewport height to reflect that. Other than that, this is a super easy and quick fix. No javascript needed, and only two CSS rules.

Elly Ambet
  • 478
  • 4
  • 13
0

The problem is simple to solve for anyone using Bootstrap 4 or higher, just include this snippet on your website:

<script>
     $(document).ready(function(){
         if ($('body').height() < $(window).height()) {
            $('footer').addClass('position-absolute bottom-0');
         } else {
            $('footer').addClass('position-static');
         }
     });
</script>

Here we check if the height of the BODY tag is less than the height of the browser window, if positive we place the footer at the bottom of the page and if negative we make the footer static and it will remain where it is. You don't need to change your current code, you just need to include this javascript in your page or package, remembering that to work the <body> tag must have position: relative, if you haven't changed the tag's "position" property in CSS <body>, you don't need to do anything as it is the default value.

  • Make sure to include the code after jquery, without jquery it won't work.
  • If you are not using the <footer> tag, you should change the $('footer') selector as appropriate.
0

for me it didn't work until I removed the position: absolute

.footer {
    // position: absolute; // removed 
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
}

html, body {height: 100%;}

Note that the <footer /> element is inside the <body>

Kilvny
  • 43
  • 7