Created
May 25, 2011 22:04
-
-
Save ChrisMBarr/992100 to your computer and use it in GitHub Desktop.
Match the height of any number of elements.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.