I want to set equal height for divs with jQuery.
All the divs may have different amount of content and different default height. Here is a sample of my html layout:
<div class="container">
<div class="column">This is<br />the highest<br />column</div>
<div class="column">One line</div>
<div class="column">Two<br />lines</div>
</div>
<div class="container">
<div class="column">One line</div>
<div class="column">Two<br>lines</div>
<div class="column">One line</div>
</div>
I’m setting the height with the next jQuery function:
$(document).ready(function(){
var highestBox = 0;
$('.container .column').each(function(){
if($(this).height() > highestBox){
highestBox = $(this).height();
}
});
$('.container .column').height(highestBox);
});
This works fine but not in my case because I want all the ‘columns’ equal only inside one ‘container’. This means that in the first container all the boxes must be as high as the first div, but in the second one they must be equal to second column.
So question is — how should I modify my jQuery to reach this?
Thank you!
Answer to your specific question
Your code was checking all columns within any container, what you need to do is:
Quick (Rough) Example
Addressing @Mem’s question
Equalising heights after image load
If you are loading images, you will find that this solution doesn’t always work. This is because
document.readyis fired at the earliest possible time, which means it does not wait for external resources to be loaded in (such as images).When your images finally load, they may distort the heights of their containers after the equalisation script has run, causing it too appear not too work.
Solving equalising heights with images
This is the best solution (at the time of writing) and involves binding to the
img.loadevent. All you have to do is get a count of how many img’s there are$('img').lengthand then for eachload,-1from that count until you hit zero, then fire the equaliser.The caveat here is
loadmay not fire in all browsers if the image is in the cache. Look around google/github, there should be a solution script out there. At the time of writing i found waitForImages from Alexander Dickson (untested, this is not an endorsement).window.loadevent. This should generally always work. However, if you check the docs out, you’ll notice this event fires after ALL external resources are loaded. This means if your images are loaded but there’s some javascript waiting to download it won’t fire until that completes.If you load the majority of your externals asyncronously and are clever with minimising, compressing and spreading the load you probably wont have any issues with this method, however a suddenly slow CDN (happens all the time) could cause issues.
In both cases, you could take a dumb approach and apply fallback timers. Have the equalise script run automatically after a set few seconds just in case the event doesn’t fire or something is slow out of your control. It’s not fool proof, but in reality you’ll satisfy 99% of your visitors with this fallback if you tune the timer correctly.
Years later this is still getting upvotes, with flexbox widely supported these days you may want to consider just plain CSS for this, unless there’s a specific reason you are using JS for it