Affordable, effective marketing solutions for your business.
402.570.6482 | contact us
945 S. 36th Street, Suite 101
Lincoln, NE 68510

Wittmania Blog

jQuery Sticky Footer – Simple and Easy Solution

I needed a quick and easy solution that would use jQuery (already in use on the site) and expand the main content wrapper to fill the viewport, but only on pages where it wasn’t tall enough already.  A quick Google search returned a bunch of solutions from 2005-2007, or that included poorly formed code (and lots of it).  So, I just wrote my own instead.

It’s pretty simple and easy, and I’ve commented the only lines you should need to worry about configuring.  In my case, I wanted to expand the #sidebar element in order to push the footer down.  You can set the target variable to whichever element you want to expand by using the standard jQuery selector syntax.  The wrapper variable should be the element that contains all of your content, i.e. the element that should be compared to the viewport to see if it needs to be expanded or not.  Finally, the adjustment variable allows you to customize the height of the adjustment.  In my case, I needed to knock off 10px due to padding applied to elements being manipulated.  I thought about using jQuery’s .outerHeight() function, but this just seemed a lot easier.  This variable can be a positive or negative number, based on your needs.

As always, your mileage may vary.  It worked fine for me in the situation I tested it in.  Let me know if you have any questions about implementing this code, or leave a comment below to link to where you’re using this code so we can all see it in action.

Here’s the code:

jQuery.noConflict();
jQuery(document).ready(function($) {

    var target = $('#sidebar'); //What element should the additional height be added to?
    var wrapper = $('#wrapper'); //What element should be compared to the viewport height?
    var adjustment = 0; //Do you need to adjust the height due to padding, etc.?

//You shouldn't need to configure anything else down here.
    var viewportHeight = $(window).height();
    var wrapperHeight = wrapper.height();

    if(viewportHeight > wrapperHeight) {
        var addHeight =     (viewportHeight-wrapperHeight);
        var targetHeight = target.height();
        var newHeight = addHeight + targetHeight;
        
        target.height(newHeight+adjustment);
    }

});

5 Comments

  1. Posted January 22, 2013 at 11:45 am | Permalink

    What’s up to all, since I am genuinely keen of reading this webpage’s post to be updated
    on a regular basis. It contains nice information.

  2. Posted March 29, 2013 at 4:42 pm | Permalink

    Incredible quest there. What occurred after? Thanks!
    http://www.liveleak.com tinnitus symptoms treatment Ear ringing medication side
    effects

  3. Posted April 12, 2013 at 12:57 am | Permalink

    I’m extremely impressed with your writing skills as well as with the layout on your web-site. Is this a paid theme or did you modify it yourself? Either way keep up the nice quality writing, it’s rare to see a great blog like this one
    nowadays.

  4. Posted April 19, 2013 at 10:47 pm | Permalink

    I have been surfing online more than 4 hours today, yet I never found any interesting article like yours.

    It is pretty worth enough for me. Personally, if all site
    owners and bloggers made good content as you did, the internet will be a
    lot more useful than ever before.

  5. Posted April 24, 2013 at 12:29 pm | Permalink

    Do you have a spam issue on this blog; I also am a blogger,
    and I was curious about your situation; many of us have
    developed some nice practices and we are looking to swap strategies with others, please shoot me an email if interested.

Post a Comment

Your email is never shared. Required fields are marked *

*
*

Testimonial:

I had a person call this week who was the victim in a serious car accident. After we met and took care of his immediate needs, I asked him how he identified us to help him with his case. He replied, "I did an internet search and you were the first firm to come up. Once I reviewed your website, I could tell you were the firm that had the experience that I needed."

Many thanks to Wittmania Marketing for providing us with an excellent website and promoting us to a high profile on the internet.

Gary Young
Keating O'Gara law Firm