88

Can anyone explain how to align a footer div to the bottom of the page. From the examples I've seen, they all show how to make the div stay visible at the bottom, no matter where you've scrolled the page. Although I don't want it like that. I want it fixed at the bottom of the page, so it doesn't move. Appreciate the help!

Joey Morani
  • 25,431
  • 32
  • 84
  • 131
  • 2
    Do you want it to appear fixed to the bottom of the browser window, or to be always at the bottom of the page, where you can't see it unless you scroll? – hookedonwinter Aug 19 '10 at 19:40
  • Yeah, I want people to have to scroll to see it. Not having it stuck at the bottom of the browser window. :) – Joey Morani Aug 19 '10 at 19:59

7 Answers7

102

UPDATE

My original answer is from a long time ago, and the links are broken; updating it so that it continues to be useful.

I'm including updated solutions inline, as well as a working examples on JSFiddle. Note: I'm relying on a CSS reset, though I'm not including those styles inline. Refer to normalize.css

Solution 1 - margin offset

https://jsfiddle.net/UnsungHero97/ur20fndv/2/

HTML

<div id="wrapper">
  <div id="content">
    <h1>Hello, World!</h1>
  </div>
</div>
<footer id="footer">
  <div id="footer-content">Sticky Footer</div>
</footer>

CSS

html, body {
  margin: 0px;
  padding: 0px;
  min-height: 100%;
  height: 100%;
}

#wrapper {
  background-color: #e3f2fd;
  min-height: 100%;
  height: auto !important;
  margin-bottom: -50px; /* the bottom margin is the negative value of the footer's total height */
}

#wrapper:after {
  content: "";
  display: block;
  height: 50px; /* the footer's total height */
}

#content {
  height: 100%;
}

#footer {
  height: 50px; /* the footer's total height */
}

#footer-content {
  background-color: #f3e5f5;
  border: 1px solid #ab47bc;
  height: 32px; /* height + top/bottom paddding + top/bottom border must add up to footer height */
  padding: 8px;
}

Solution 2 - flexbox

https://jsfiddle.net/UnsungHero97/oqom5e5m/3/

HTML

<div id="content">
  <h1>Hello, World!</h1>
</div>
<footer id="footer">Sticky Footer</footer>

CSS

html {
  height: 100%;
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100%;
}

#content {
  background-color: #e3f2fd;
  flex: 1;
  padding: 20px;
}

#footer {
  background-color: #f3e5f5;
  padding: 20px;
}

Here's some links with more detailed explanations and different approaches:


ORIGINAL ANSWER

Is this what you mean?

http://ryanfait.com/sticky-footer/

This method uses only 15 lines of CSS and hardly any HTML markup. Even better, it's completely valid CSS, and it works in all major browsers. Internet Explorer 5 and up, Firefox, Safari, Opera and more.

This footer will stay at the bottom of the page permanently. This means that if the content is more than the height of the browser window, you will need to scroll down to see the footer... but if the content is less than the height of the browser window, the footer will stick to the bottom of the browser window instead of floating up in the middle of the page.

Let me know if you need help with the implementation.

starball
  • 20,030
  • 7
  • 43
  • 238
Hristo
  • 45,559
  • 65
  • 163
  • 230
  • It doesn't work in IE7 and IE8 – Diego ZoracKy Feb 15 '13 at 12:50
  • it does work in IE7 and IE8. I use these myself, the second one in particular, and it definitely works. Perhaps you have some styles missing or other styles overriding? – Hristo Feb 15 '13 at 16:59
  • I'm curious why you're marked this as the correct answer when you specifically stated that you weren't looking for this solution? – dmc Jun 20 '13 at 10:56
  • unfortunately the main solution here is 404 and the second one is old as hell... – igorsantos07 Mar 22 '17 at 20:31
  • @igorsantos07 Why did you vote down my answer? The question is from 2010, and my answer is from 2010. Granted, I should probably update this for the more modern use case, but it's still an answer to the question, and obviously was helpful to the person who asked the question. – Hristo Mar 22 '17 at 20:35
  • But it's not helpful anymore to those who come here from Google (like myself). Besides that, your first suggestion is broken - SO even suggests avoiding plain links without explanation of the solving method, so... That's why :) helping to update such important question. – igorsantos07 Mar 22 '17 at 22:12
  • I understand, but I think a better approach would be to make a suggestion over voting down a good answer. A down vote sends the wrong message. On the other hand, a comment like "Hey, your answer is outdated, and the first link is broken. Please update!" sends a totally different message, and I'd be happy to address the feedback. And the link is broken because the guy who originally owned the site passed away. – Hristo Mar 22 '17 at 22:18
  • @Hristo The link is dead – lu5er Apr 18 '17 at 03:33
  • @lU5er updated my answer with the solution inline – Hristo Apr 18 '17 at 21:22
  • @Hristo, Thanks! Now it's perfect! :) – lu5er Apr 18 '17 at 21:24
  • @lU5er no problem. if you down-voted my answer, can you please undo that? – Hristo Apr 18 '17 at 21:24
  • 2
    @Hristo. +1 already! Thanks to your efforts. – lu5er Apr 18 '17 at 21:26
  • 1
    @igorsantos07 updated my answer with the solution inline, as well as with (currently working) links that show other approaches – Hristo Apr 18 '17 at 21:30
  • I see the downvote as a way to make space for other, better voted answer. in such cases it's very easy to understand what's wrong with the so-current answer :) but it's updated now downvote taken! – igorsantos07 Apr 24 '17 at 14:36
  • @Hristo I am adding positioned element inside the content, when element's height increase dynamically, it is overlapping my footer. – YogendraR Apr 20 '18 at 13:13
  • 1
    The only answer across all the forums that work. – Jithin Pavithran Dec 07 '19 at 23:20
  • solution 2 seems more powerful since it doesn't require us to fix the height of the footer (which is never fixed due to responsive requirements) – Vincent Jul 16 '21 at 10:59
47

This will make the div fixed at the bottom of the page but in case the page is long it will only be visible when you scroll down.

<style type="text/css">
  #footer {
    position : absolute;
    bottom : 0;
    height : 40px;
    margin-top : 40px;
  }
</style>
<div id="footer">I am footer</div>

The height and margin-top should be the same so that the footer doesnt show over the content.

ovais.tariq
  • 2,627
  • 17
  • 12
22

Your title and comments imply that you weren't looking for a sticky footer (stuck to the bottom of the window as content scrolls below it). I assume you were looking for a footer that would be forced to the bottom of the window if the content does not fill the window, and push down to the bottom of the content if the content exceeds the window boundary.

You can accomplish this with the following.

&ltstyle>
html,
body {
    margin:0;
    padding:0;
    height:100%;
}
#container {
    min-height:100%;
    position:relative;
}
#header {
    background:#ff0;
    padding:10px;
}
#body {
    padding:10px;
    padding-bottom:60px;   /* Height of the footer */
}
#footer {
    position:absolute;
    bottom:0;
    width:100%;
    height:60px;   /* Height of the footer */
    background:#6cf;
}
&lt/style>

&ltdiv id="container">
    &ltdiv id="header">header&lt/div>
    &ltdiv id="body">body&lt/div>
    &ltdiv id="footer">footer&lt/div>
&lt/div>

Source: How to keep footers at the bottom of the page

Jason George
  • 6,992
  • 8
  • 44
  • 60
  • Thank you! This is the best answer. And highlights why it's so important to take care of this first before you start coding the web page. – Diana Jun 22 '21 at 16:00
10

Use

<div style="position:fixed; bottom:0; height:auto; margin-top:40px;
            width:100%; text-align:center">
  I am footer
</div>

Footer will not go upwards

mwfearnley
  • 3,303
  • 2
  • 34
  • 35
8

check this out, works on firefox and IE

<style>
    html, body
    {
        height: 100%;
    }
    .content
    {
        min-height: 100%;
    }
    .footer
    {
        position: relative;
        clear: both;
    }
</style>

<body>
<div class="content">Page content
</div>
<div class="footer">this is my footer
</div>
</body>
Vinay B R
  • 8,089
  • 2
  • 30
  • 45
6

A simple solution that i use, works from IE8+

Give min-height:100% on html so that if content is less then still page takes full view-port height and footer sticks at bottom of page. When content increases the footer shifts down with content and keep sticking to bottom.

JS fiddle working Demo: http://jsfiddle.net/3L3h64qo/2/

Css

html{
  position:relative; 
  min-height: 100%;
}
/*Normalize html and body elements,this style is just good to have*/
html,body{
  margin:0;
  padding:0;
}
.pageContentWrapper{
  margin-bottom:100px;/* Height of footer*/
} 
.footer{
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height:100px;
    background:#ccc;
}

Html

   <html>
    <body>
        <div class="pageContentWrapper">
            <!-- All the page content goes here-->
        </div>
        <div class="footer">
        </div>
    </body>
    </html>
Sanjeev
  • 2,607
  • 22
  • 16
1

I am a newbie and these methods are not working for me. However, I tried a margin-top property in css and simply added the value of content pixels +5.

Example: my content layout had a height of 1000px so I put a margin-top value of 1005px in the footer css which gave me a 5px border and a footer that sits delightfully at the bottom of my site.

Probably an amateur way of doing it, but EFFECTIVE!!!

Jamie
  • 11
  • 1