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
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.
Incredible quest there. What occurred after? Thanks!
http://www.liveleak.com tinnitus symptoms treatment Ear ringing medication side
effects
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.
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.
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.