Skip to content

Instantly share code, notes, and snippets.

@jimmynotjim
Created May 25, 2012 00:47
Show Gist options
  • Save jimmynotjim/2785129 to your computer and use it in GitHub Desktop.
Save jimmynotjim/2785129 to your computer and use it in GitHub Desktop.
jQuery script to equalize the sidebar and content of a page
Simple jQuery script to set a sidebar to always span the height of a page. Required when the sidebar has a background, borders, or shadows. #main is the container for both the sidebar and content and can be replaced w/ any other element. It's important to use min-height, in case your sidebar has moving/changing elements that could resize it larger than the main content (or just happens to be larger than the main content).
// Set #sidebar's min-height equal to height of #main element
//--------------------------------------------------------
function equalHeight() {
$('#sidebar').css( 'min-height', $('#main').height() );
}
// Runs the script when the page loads
//--------------------------------------------------------
equalHeight();
// Added a resize event to #main that fires the script anytime the container is resized
//--------------------------------------------------------
$('#main').resize(function() {
equalHeight();
});
@jimmynotjim
Copy link
Author

Set the script as a function so that I could fire it again when the #main element is resized (using https://github.com/cowboy/jquery-resize). Required when using iframes like w/ Facebook widgets, Wufoo forms, or other external data that loads after the height has been calculated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment