Skip to content

Instantly share code, notes, and snippets.

@cezarignat
Last active April 12, 2021 12:30
Show Gist options
  • Save cezarignat/33a32ae4424e43e9faf0253ded367520 to your computer and use it in GitHub Desktop.
Save cezarignat/33a32ae4424e43e9faf0253ded367520 to your computer and use it in GitHub Desktop.
Make all the elements the same height (using the biggest value)
/*
* Dependencies: jQuery 1.x / 2.x / 3.x
*
* Make all the elements the height of the biggest one
* elementsSelector is the general selector used to get all the elements
* that you want to equalise. e.g '#foo li'
*
* Use the extra argument to add extra pixels to the final height
*
*
*/
function equalElements(elementsSelector, extra) {
if(typeof(extra) == 'undefined') {
extra = 0;
}
// Get an array of all element heights
var elementHeights = jQuery(elementsSelector).map(function() {
return jQuery(this).height();
}).get();
// Math.max takes a variable number of arguments
// `apply` is equivalent to passing each height as an argument
// Set each height to the max height
jQuery(elementsSelector).height(Math.max.apply(null, elementHeights) + extra);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment