Code Snippet
Equalize Heights of Divs
var maxHeight = 0;
$("div").each(function(){
if ($(this).height() > maxHeight) { maxHeight = $(this).height(); }
});
$("div").height(maxHeight);
var maxHeight = 0;
$("div").each(function(){
if ($(this).height() > maxHeight) { maxHeight = $(this).height(); }
});
$("div").height(maxHeight);
I don’t know why. it work offline but when I put the site to the server it make the height small than the content. And sometimes it change the height with browsers reload in Safari or Firefox.
Interestingly enough, I experienced the same issue. Worked great on the hard drive, but when I uploaded the files to the server, for some reason the script didn’t work in the same manner. It actually set the height for each div to the height of the first.
…at least until I refreshed.
Gotta love the browser cache.
this is sooo good! thank you!
…actually I do have a small question. How do you implement this on multiple divs?
for example I have 3 sets of 2 divs on a page. I only want the sets to equalize, not all divs on the page. I don’t mind the using multiple classes but whats the shortest way to write it.
div.equalize-1, div.equalize-2, div.equalize-3 are my classes.
I assigned these classes to each set of divs I want to be equalized.
Any tips would be great thanks.
// Plugin $.fn.equalize = function() { var maxHeight = 0; return this.each(function(){ var $this = $(this); if ($(this).height() > maxHeight) { maxHeight = $(this).height(); } $this.height(maxHeight); }); }; // Use generic class $('.equalize').equalize(); OR $('div.equalize-1, div.equalize-2, div.equalize-3').equalize();Woops, gotta replace the following line:
if ($this.height() > maxHeight) { maxHeight = $this.height(); }Forgot to use the “$this” var!
Your plugin version doesn’t work quite right I think – it appears to be applying the current maxHeight BEFORE it has looped through all the selectors, so its only correct for things after the tallest one.
The original version is fine, though
DigWP
A book and blog co-authored by Jeff Starr and myself about the World's most popular publishing platform.
Quotes on Design
Design, like Art, can be an elusive word to define and an awfully fun thing to have opinions about.
HTML-Ipsum
One-click copy to clipboard access to Lorem Ipsum text that comes wrapped in a variety of HTML.
Bookshelf
Hey Chris, what books do you recommend? These, young fertile mind, these.