Question

I've got a few DIV tags with different amounts of text content.

HTML:

<div id="boxes">
    <div id="boxone">
        <p>...</p>
    </div>
    <div id="boxtwo">
        <p>...</p>
    </div>
    <div id="boxthree">
        <p>...</p>
    </div>
    <div id="boxfour">
        <p>...</p>
    </div>
</div>

They're in a two-by-two layout and their width is fluid:

CSS:

div#boxes {
    width: 100%;
}

div#boxes div {
    width: 49.9%;
    float: left;
}

I want them all the same height.

So, I loop through them and find the height of the tallest one. Then I loop again and set them all to that height.

jQuery:

$(function() {
    var maxHeight = 0;
    $('div#boxes div').each(function(){
        if (maxHeight < $(this).height()) {maxHeight = $(this).height()}
    });
    $('div#boxes div').each(function(){
        $(this).height(maxHeight);
    });
});

This works well if the height of the div doesn't need to change again.

But, it fails if I resize the browser window:

  1. If I (a) make the browser wider, then (b) my DIVs get wider, then (c) their text content wraps fewer times, and then (d) my DIVs are too tall.

  2. If I (b) make the browser more narrow, then (b) my DIVs get narrower, then (c) their text content wraps more, and then (d) my DIVs are too short.

How do I both (1) automatically size DIVs to the height of their content like normal, but also (2) keep those multiple DIVs the same height?

Was it helpful?

Solution

Update... completely rewriting this answer after experimenting and finding another, apparently workable way to do this:

function sortNumber(a,b)    {
    return a - b;
}

function maxHeight() {
    var heights = new Array();
    $('div#boxes div').each(function(){
        $(this).css('height', 'auto');
        heights.push($(this).height());
        heights = heights.sort(sortNumber).reverse();
        $(this).css('height', heights[0]);
    });        
}

$(document).ready(function() {
    maxHeight();
})

$(window).resize(maxHeight);

One thing I noticed is that IE really has rounding troubles with 50% wide floated divs... the rendering was much better if I changed those to 49%.

This jQuery works...

// global variables

doAdjust = true;
previousWidth = 0;

// raise doAdjust flag every time the window width changes

$(window).resize(function() {
    var currentWidth = $(window).width();
    if (previousWidth != currentWidth) {
        doAdjust = true;
    }
    previousWidth = currentWidth;
})

// every half second

$(function() {
    setInterval('maybeAdjust()', 500);
});

// check the doAdjust flag

function maybeAdjust() {
    if (doAdjust) {
        adjustBoxHeights();
        doAdjust = false;
    }
}

// loop through the DIVs and find the height of the tallest one
// then loop again and set them all to that height

function adjustBoxHeights() {
    var maxHeight = 0;
    $('div#boxes div').each(function(){
        $(this).height('auto');
        if (maxHeight < $(this).height()) {maxHeight = $(this).height()}
    });
    $('div#boxes div').each(function(){
        $(this).height(maxHeight);
    });
}

OTHER TIPS

For the others who, like me, came here by searching Google for a solution: The fist part of the accepted answer only works when the first div is the highest. I have made some changes to it and now it seems to work in all cases.

var highest = 0;
function sortNumber(a,b)    {
    return a - b;
}

function maxHeight() {
    var heights = new Array();
    $('#wrapper2>div').each(function(){
        $(this).css('height', 'auto');
        heights.push($(this).height());
        heights = heights.sort(sortNumber).reverse();
    });        
        highest = heights[0]; 
    $('#wrapper2>div').each(function(){
        $(this).css('height', highest);
    });

}

$(document).ready(function() {
    maxHeight();
})

$(window).resize(maxHeight);

Check out this jQuery Plugin which allows you to monitor a CSS property such as the height.

If you want it to work when resizing etc... Do it like this:

function sortNumber(a,b) {
    return a - b;
}

var highest = 0;

function maxHeight() {
    var heights = new Array();
    $('.equalheight div').css('height', 'auto');
    $('.equalheight div').each(function(){
        heights.push($(this).height());
    });        
    heights = heights.sort(sortNumber).reverse();
    highest = heights[0]; 
    $('.equalheight div').css('height', highest);
}

$(document).ready(maxHeight);
$(window).resize(maxHeight);

I found this solution to be a bit more tidy for my situation. It works regardless of which div is the highest. Based it on this old Chris Coyier post at CSS Tricks.

function maxHeight() {
    var maxHeight = 0;
    $(".container > .post-box").each(function(){
       if ($(this).height() > maxHeight) { maxHeight = $(this).height(); }
    });
    $(".container > .post-box").height(maxHeight);
}

$(document).ready(function() {
    maxHeight();
}

$(window).bind('resize', function () {
    $(".container > .post-box").css('height', 'auto');
    maxHeight();
}).trigger('resize');

However, I did need to set the height to auto on resize before running the function again. I didn't need to set it in the original function because it was set in the css.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top