20

In Bootstrap, is there an easy way to make all the span elements in a row the same height, i.e. the height of the tallest element?

Here's an example of what I mean.

http://jsfiddle.net/DVnZ6/

3 Answers 3

46

You can do it using jQuery:

boxes = $('.well');
maxHeight = Math.max.apply(
  Math, boxes.map(function() {
    return $(this).height();
}).get());
boxes.height(maxHeight);

Here's an example: http://jsfiddle.net/DVnZ6/3/

3
  • 2
    @DaveR Short answer: no. Long answer: you could use a fixed height, or, if you've a know number of boxes into a div, use a percentage height (100/N), but it's not the same thing. Take in mind that bootstrap uses jQuery by itself ;) Commented Sep 8, 2012 at 12:49
  • 1
    @DaveR There's another option, but it works only if you want many divs looks like a table row: jsfiddle.net/DVnZ6/2 Commented Sep 8, 2012 at 13:08
  • Substitution (this works better): .outerHeight() -> .height() Commented Sep 8, 2012 at 17:56
1

The resize event would have to be added to this code since the height of the content in most cases varies on different resolutions and that produces unwanted design problems like overflowed text.

Here's an more complete version of the code that supports resizing as well

jQuery(function () {

    equalMaxWidth = 500; /* Customize viewport width - for resolutions below this number, the height equalizing will not work */
    boxes = jQuery('.well');

    boxes.removeAttr('style');
    if(jQuery(window).width() > equalMaxWidth){
        equalBoxHeights(boxes);
    };

    window.onresize = function () {
        boxes.removeAttr('style');
        console.log( jQuery(window).width());
        if(jQuery(window).width() > equalMaxWidth){
            equalBoxHeights(boxes);
        };
    };
});

function equalBoxHeights(boxes) {
    maxHeight = Math.max.apply(
            Math, boxes.map(function () {
                return jQuery(this).height();
            }).get());
    boxes.height(maxHeight);
}

Check the demo on jsFiddle http://jsfiddle.net/o4w7tjtz/

Tip: try resizing the 3rd vertical frame (noticed equal heights on around 500pw width ?)

-3

If you set the height of the spans to 100% and set the height of the row to a fixed value, they will all match the height of the container.

Of course, you would have to know the expected height of the columns, but it is a js free solution.

1
  • 1
    You can't set 100% height on a block element.
    – T Nguyen
    Commented Sep 25, 2013 at 20:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.