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

I have a simple 2-column layout with a footer that clears both the right and left div in my markup. My problem is that I can't get the footer to stay at the bottom of the page in all browsers. It works if the content pushes the footer down, but that's not always the case.

Update:

It's not working properly in Firefox. I'm seeing a strip of background color below the footer when there's not enough content on the page to push the footer all the way down to the bottom of the browser window. Unfortunately, this is the default state of the page.


To get a sticky footer:

  • Have a <div> with class="wrapper" for your content.

  • Right before the closing </div> of the wrapper place the <div class="push"></div> .

  • Right after the closing </div> of the wrapper place the <div class="footer"></div> .

  • * {
        margin: 0;
    }
    html, body {
        height: 100%;
    }
    .wrapper {
        min-height: 100%;
        height: auto !important;
        height: 100%;
        margin: 0 auto -142px; /* the bottom margin is the negative value of the footer's height */
    }
    .footer, .push {
        height: 142px; /* .push must be the same height as .footer */
    }
    

    Use CSS vh units!

    Probably the most obvious and non-hacky way to go about a sticky footer would be to make use of the new css viewport units.

    Take for example the following simple markup:

    <header>header goes here</header>
    <div class="content">This page has little content</div>
    <footer>This is my footer</footer>
    

    If the header is say 80px high and the footer is 40px high, then we can make our sticky footer with one single rule on the content div:

    .content {
        min-height: calc(100vh - 120px);
        /* 80px header + 40px footer = 120px  */
    }
    

    Which means: let the height of the content div be at least 100% of the viewport height minus the combined heights of the header and footer.

    That's it.

    * {
        margin:0;
        padding:0;
    }
    header {
        background: yellow;
        height: 80px;
    }
    .content {
        min-height: calc(100vh - 120px);
        /* 80px header + 40px footer = 120px  */
        background: pink;
    }
    footer {
        height: 40px;
        background: aqua;
    }
    <header>header goes here</header>
    <div class="content">This page has little content</div>
    <footer>This is my footer</footer>

    你可以使用position: absolute来将页脚放在页面的底部,但是确保你的2列有适当的margin-bottom这样它们就不会被页脚遮挡。

    #footer {
        position: absolute;
        bottom: 0px;
        width: 100%;
    }
    #content, #sidebar { 
        margin-bottom: 5em; 
    }
    
    链接地址: http://www.djcxy.com/p/23726.html

    上一篇: 使HTML页面页脚以最小高度停留在页面底部的CSS

    下一篇: 如何让页脚停留在网页的底部?