Search code examples
jquerycssfootersticky

jQuery sticky footer


James here! I've been trying for about two hours now to get a sticky footer, but I seem keep messing up the CSS. I'm looking for a script that jQuery can handle. I understand how most of the scripts work (which is surprising, since I'm just learning), but I need the script to work no matter what the height of the footer is, because it doesn't have a dynamic height set in the CSS file of my page. Would anyone be able to supply a working script for a sticky footer? I want the footer itself to always be at the bottom of pages, BUT NOT FIXED POSITION. The content element is #posts, and the footer area is a element with the ID of #bottom. Here is a page example: JTB Permalink Page


Solution

  • Ok. HTML:

    <div id="container">
        <div id="wrapper">
            <!-- CONTENT GOES HERE -->
        </div>
        <div id="footer">
            <!-- FOOTER GOES HERE -->
        </div>
    </div>
    

    CSS:

    #container {
        min-height: 100%;
        position: relative;
        width: 100%;
    }
    #wrapper {
        position: relative;
        width: 100%;
        height: 100%;
        padding-bottom: 206px; /* footer height, we will fix that with jquery */
    }
    #footer {
        position: absolute;
        bottom: 0;
        width: 100%;
        left: 0px;
        height: 206px; /* footer height if any */
    }
    

    jQuery:

    $(document).ready(function(){
        var footer_height=$("#footer").height();
        $("#wrapper").css({
            'padding-bottom' : footer_height
        });
    });
    

    I must warn you, jquery .height() function may not work properly so be careful with paddings and margins (just add margin/padding values to 'footer_height' and you should be fine).