Skip to content

Instantly share code, notes, and snippets.

@ChrisMBarr
Created May 25, 2011 22:04
Show Gist options
  • Save ChrisMBarr/992100 to your computer and use it in GitHub Desktop.
Save ChrisMBarr/992100 to your computer and use it in GitHub Desktop.
Match the height of any number of elements.
(function($){
$.fn.equalHeight = function(ignorePadding) {
//Matches the height of all the passed in items
if(this.length){
var tallest = 0;
var tallestPadding = 0;
var paddings=[];
//loop through the items, save the paddings for each and get the tallest height
this.each(function(i) {
var $thisItem = $(this);
paddings[i]=(ignorePadding)?0:parseInt($thisItem.css("padding-top"))+parseInt($thisItem.css("padding-bottom"));
tallest = Math.max(tallest,$thisItem.height());
tallestPadding=Math.max(tallestPadding,paddings[i]);
});
//Loop through everything again and set the height on everything shorter than the tallest
this.each(function(i){
$(this).height(tallest-(paddings[i]-tallestPadding));
});
}
};
})( jQuery );
@ChrisMBarr
Copy link
Author

Call it with $(".elements").equalHeight() and it will make sure all the elements passed in are the same height, even if they have different amounts of padding.

Pass in an optional parameter of true ``$(".elements").equalHeight(true)` to have it ignore padding on the elements and just match the height property.

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