By H Bellamy


2012-01-11 18:43:22 8 Comments

CSS Push Div to bottom of page

Please look at that link, I want the opposite: When the content overflows to the scrollbars, I want my footer to be always at the complete bottom of the page, like Stack Overflow.

I have a div with id="footer" and this CSS:

#footer {
    position: absolute;
    bottom: 30px;
    width: 100%;
}

But all it does is go to the bottom of the viewport, and stays there even if you scroll down, so it is no longer at the bottom.

Image: Examlple

Sorry if not clarified, I don't want it to be fixed, only for it to be at the actual bottom of all the content.

12 comments

@Praveen Kundu 2019-11-08 06:30:03

position: fixed;
bottom: 0;
(if needs element in whole display and left align)
left:0;
width: 100%;

@FZs 2019-11-08 21:31:02

Please describe in your answer, what was the problem, and how will this snippet solve it, to help others understand this answer

@Gowtham Sooryaraj 2019-10-05 14:13:44

use fixed-bottom bootstrap class

<div id="footer" class="fixed-bottom w-100">

@TylerH 2019-10-10 13:17:11

There's no indication OP is using Bootstrap.

@Gowtham Sooryaraj 2019-10-11 05:20:06

hapirides.com check this page in mobile view

@Anonymous 2013-08-11 05:26:43

Just worked out for another solution as above example have bug( somewhere error ) for me. Variation from the selected answer.

html,body {
    height: 100%
}

#nonFooter {
    min-height: 100%;
    position:relative;
    /* Firefox */
    min-height: -moz-calc(100% - 30px);
    /* WebKit */
    min-height: -webkit-calc(100% - 30px);
    /* Opera */
    min-height: -o-calc(100% - 30px);
    /* Standard */
    min-height: calc(100% - 30px);
}

#footer {
    height:30px;
    margin: 0;
    clear: both;
    width:100%;
    position: relative;
}

for html layout

<body>
    <div id="nonFooter">header,middle,left,right,etc</div>
    <div id="footer"></div>
</body>

Well this way don't support old browser however its acceptable for old browser to scrolldown 30px to view the footer

@Tim L 2016-10-18 14:11:45

I just want to add - most of the other answers worked fine for me; however, it took a long time to get them working!

This is because setting height: 100% only picks up parent div's height!

So if your entire html (inside of the body) looks like the following:

<div id="holder">
    <header>.....</header>
    <div id="body">....</div>
    <footer>....</footer>
</div>

Then the following will be fine:

html,body{
    height: 100%
}

#holder{
    min-height: 100%;
    position:relative;
}

#body{
    padding-bottom: 100px;    /* height of footer */
}

footer{
    height: 100px; 
    width:100%;
    position: absolute;
    left: 0;
    bottom: 0; 
}

...as "holder" will pick up it's height directly from "body".

Kudos to My Head Hurts, whose answer was the one I ended up getting to work!

However. If your html is more nested (because it's only an element of the full page, or it's within a certain column, etc) then you need to make sure every containing element also has height: 100% set on the div. Otherwise, the information on height will be lost between "body" and "holder".

E.g. the following, where I've added the "full height" class to every div to make sure the height gets all the way down to our header/body/footer elements:

<div class="full-height">
    <div class="container full-height">
        <div id="holder">
            <header>.....</header>
            <div id="body">....</div>
            <footer>....</footer>
        </div>
    </div>
</div>

And remember to set height on full-height class in the css:

#full-height{
    height: 100%;
}

That fixed my issues!

@AlexHighHigh 2016-06-19 20:41:40

You didn't close your ; after position: absolute. Otherwise your above code would have worked perfectly!

#footer {
   position:absolute;
   bottom:30px;
   width:100%;
}

@David B 2015-10-07 00:29:05

I've solved a similar issue by putting all of my main content within an extra div tag (id="outer"). I've then moved the div tag with id="footer" outside of this last "outer" div tag. I've used CSS to specify the height of "outer" and specified the width and height of "footer". I've also used CSS to specify the margin-left and margin-right of "footer" as auto. The result is that the footer sits firmly at the bottom of my page and scrolls with the page too (although, it's still appears inside the "outer" div, but happily outside of the main "content" div. which seems strange, but it's where I want it).

@George Carlin 2015-08-10 13:48:35

if you have a fixed height footer (for example 712px) you can do this with js like so:

var bgTop = 0;
window.addEventListener("resize",theResize);
function theResize(){
    bgTop = winHeight - 712;
    document.getElementById("bg").style.marginTop = bgTop+"px";
}

@Obromios 2015-08-03 05:39:25

This is an intuitive solution using the viewport command that just sets the minimum height to the viewport height minus the footer height.

html,body{
height: 100%
}
#nonFooter{
min-height: calc(100vh - 30px)
}

#footer {
height:30px;
margin: 0;
clear: both;
width:100%;
}

@Tarciso Junior 2015-07-27 18:31:37

I would comment if i could , but i have no permissions yet, so i will post a hint as an answer, for unexpected behavior on some android devices:

Position: Fixed only works in Android 2.1 thru 2.3 by using the following meta tag:

<meta name="viewport" content="width=device-width, user-scalable=no">.

see http://caniuse.com/#search=position

@Ian 2015-05-12 09:32:15

I realise it says not to use this for 'responding to other answers' but unfortunately I don't have enough rep to add a comment onto the appropriate answer (!) but ...

If you are having problems in asp.net with the answer from 'My Head Hurts' - you need to add 'height : 100%' to the main generated FORM tag as well as HTML and BODY tags in order for this to work.

@My Head Hurts 2012-01-11 19:51:29

Unfortunately you can't do this with out adding a little extra HTML and having one piece of CSS rely on another.

HTML

First you need to wrap your header,footer and #body into a #holder div:

<div id="holder">
    <header>.....</header>
    <div id="body">....</div>
    <footer>....</footer>
</div>

CSS

Then set height: 100% to html and body (actual body, not your #body div) to ensure you can set minimum height as a percentage on child elements.

Now set min-height: 100% on the #holder div so it fills the content of the screen and use position: absolute to sit the footer at the bottom of the #holder div.

Unfortunately, you have to apply padding-bottom to the #body div that is the same height as the footer to ensure that the footer does not sit above any content:

html,body{
    height: 100%
}

#holder{
    min-height: 100%;
    position:relative;
}

#body{
    padding-bottom: 100px;    /* height of footer */
}

footer{
    height: 100px; 
    width:100%;
    position: absolute;
    left: 0;
    bottom: 0; 
}

Working example, short body: http://jsfiddle.net/ELUGc/

Working example, long body: http://jsfiddle.net/ELUGc/1/

@Somnath Kharat 2013-10-16 13:37:09

i can't get why width:100%; is required

@My Head Hurts 2013-10-16 18:21:06

@sTACKoVERFLOW - If you do not specify a width or both left and right then the values are set to auto and the element will wrap to fit its content. Specifying width: 100% or left: 0 and right:0 ensures that the absolutely positioned element takes the full width of the container element

@Jatin 2014-02-17 14:33:49

I was looking for similar behavior, but in my case the #body div is floated, by say "float: left". The short body example works, but the long body example does not. Any suggestions how I can get the desired behavior using float on the #body div.

@msouth 2015-05-28 14:38:12

@Ian said, in an answer because he didn't have enough rep to comment:If you are having problems in asp.net with the answer from 'My Head Hurts' - you need to add 'height : 100%' to the main generated FORM tag as well as HTML and BODY tags in order for this to work.

@Ben Caine 2016-02-04 19:05:48

If you're trying to get this to work inside a div rather than inside the whole body, make sure you specify height=100% on all of your containing elements, including <form> elements.

@AlexHighHigh 2016-06-19 20:42:52

the main point you did that he didnt was close your position: fixed with a ;

@vctls 2017-07-25 08:26:34

I still don't get why all this boilerplate is needed, but it was indeed the only way that prevented my footer from moving out of place or covering other content for any window size, when combined with this other answer: stackoverflow.com/a/12408519

@Joseph Silber 2012-01-11 18:45:37

This is precisely what position: fixed was designed for:

#footer {
    position: fixed;
    bottom: 0;
    width: 100%;
}

Here's the fiddle: http://jsfiddle.net/uw8f9/

@vsync 2012-10-28 16:53:13

That solution is not good for 90% of cases. if you re-size your window, your footer will overlay other content and won't stop right at the end of your content

@My Head Hurts 2012-11-26 07:35:37

@aroth that is because they are two totally different solutions. This solution will keep the footer at the bottom of the visual area of the screen at all times. The alternative solution keeps the footer at either the bottom of the screen or the bottom of the page, depending on which is larger - which was what the asker requested.

@Joseph Silber 2012-11-26 14:12:40

@MyHeadHurts - You're absolutely right. After answering, I realized that this was not what the OP wanted, but I left it here for people looking for a simple fixed footer. From the comments (and votes) it seems many people are struggling with this elementary CSS feature.

@aroth 2012-11-26 23:23:09

@MyHeadHurts - Fair enough, though for my money they're both still basically the same. Content scrolls underneath the fixed footer, when there is enough content to do so. That's good enough for me. Of course, I can see how very large footers like what they have on stackoverflow would not work well with the fixed approach, but for a basic one-line footer this approach works well in my opinion.

@My Head Hurts 2012-11-27 07:35:08

@aroth I agree, this is a good solution and it all depends what you want. I was just pointing out why one solution was more complicated than the other -> they don't do the same thing.

@Robert Christian 2013-11-03 20:42:05

This is a solution for a "fixed" footer, not a "sticky" footer.

@Joseph Silber 2013-11-04 02:03:37

@rob - Correct. Read the comments above yours.

Related Questions

Sponsored Content

27 Answered Questions

[SOLVED] How do you get the footer to stay at the bottom of a Web page?

22 Answered Questions

12 Answered Questions

[SOLVED] Keep overflow div scrolled to bottom unless user scrolls up

38 Answered Questions

[SOLVED] How can I make a div not larger than its contents?

  • 2009-01-16 16:03:07
  • George Snider
  • 1351336 View
  • 1972 Score
  • 38 Answer
  • Tags:   html css width

13 Answered Questions

[SOLVED] HTML table with 100% width, with vertical scroll inside tbody

31 Answered Questions

[SOLVED] Make a div fill the height of the remaining screen space

  • 2008-09-18 05:06:17
  • Vincent McNabb
  • 918973 View
  • 1756 Score
  • 31 Answer
  • Tags:   html css html-table

25 Answered Questions

[SOLVED] How to align content of a div to the bottom

18 Answered Questions

[SOLVED] How do I force a DIV block to extend to the bottom of a page even if it has no content?

  • 2008-09-29 04:40:24
  • Vince Panuccio
  • 331784 View
  • 182 Score
  • 18 Answer
  • Tags:   css html border

4 Answered Questions

[SOLVED] Position absolute but relative to parent

  • 2012-05-07 18:41:54
  • BlaShadow
  • 305364 View
  • 407 Score
  • 4 Answer
  • Tags:   html css

Sponsored Content