Search code examples
htmlcssfooteralignment

How to align footer (div) to the bottom of the page?


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!


Solution

  • 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.