Skip to content

Instantly share code, notes, and snippets.

@KruegerDesigns
Created January 11, 2013 16:53
Show Gist options
  • Save KruegerDesigns/4512221 to your computer and use it in GitHub Desktop.
Save KruegerDesigns/4512221 to your computer and use it in GitHub Desktop.
Elements that need to match height can use this jQuery. Based on Chris Coyer's same height code, but modified for layouts using "box-sizing: border-box;".
// jQuery same height HTML elements, per a row.
$(window).load(function() {
var currentTallest = 0,
currentRowStart = 0,
rowDivs = new Array(),
$el,
topPosition = 0;
$('.blocks').each(function() {
$el = $(this);
topPostion = $el.position().top;
if (currentRowStart != topPostion) {
// we just came to a new row. Set all the heights on the completed row
for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) {
rowDivs[currentDiv].height(currentTallest);
}
// set the variables for the new row
rowDivs.length = 0; // empty the array
currentRowStart = topPostion;
currentTallest = $el.outerHeight();
rowDivs.push($el);
} else {
// another div on the current row. Add it to the list and check if it's taller
rowDivs.push($el);
currentTallest = (currentTallest < $el.outerHeight()) ? ($el.outerHeight()) : (currentTallest);
}
// do the last row
for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) {
rowDivs[currentDiv].height(currentTallest);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment