2

Can we put jQuery to use to create columns of equal height? If yes, how?

halfer
  • 19,824
  • 17
  • 99
  • 186
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • All cells of a table row should have the same height. Likewise, all columns of a table should have the same height, which can be defined by table.style.height ? Or am I mis-interpreting? – Warty Mar 01 '10 at 08:07

4 Answers4

14

Update from 2022

JavaScript

jQuery is a bit overkill for something like this; you could use vanilla JavaScript:

// Construct a NodeList of all column elements
const columns = document.querySelectorAll(".column");
// Determine the tallest Node in the list based on its offsetHeight
const tallest = Math.max( ...[ ...columns ].map( c => c.offsetHeight ) );
// Apply that height to all Nodes in the NodeList
columns.forEach( column => column.style.height = `${tallest}px` );

Or (preferably) CSS

In reality, JavaScript probably shouldn't be used at all. Modern browsers have supported Flexbox for quite some time, which is capable of keeping laterally-placed elements equal in height:

Assuming the following (taken from original demo below):

<div class="container">
    <div class="column">Foo</div>
    <div class="column">Foo<br/>Foo<br/>Foo</div>
    <div class="column">Foo</div>    
</div>

Flexbox creates columns of equal-height (based on the tallest) with the following:

.container {
    display: flex;
}

Original Answer

Cycle through each column, and find the tallest. Then set all to that height.

var maxHeight = 0;
$(".column").each(function(){
  maxHeight = $(this).height() > maxHeight ? $(this).height() : maxHeight;
}).height(maxHeight);​

Online Demo: http://jsbin.com/afupe/2/edit

Sampson
  • 265,109
  • 74
  • 539
  • 565
  • I wonder if something has changed since this answer was posted - I get an error in IE9 (syntax error) on the last line, and nothing at all seems to happen in Firefox or Chrome. I am in no way a jQuery guru so maybe I'm just implementing wrong...? – Michelle Jan 03 '12 at 21:16
  • Figured it out; for some reason the last semi-colon was throwing an error. I removed it and it works perfectly! Thanks! – Michelle Jan 03 '12 at 21:52
3

Highly recommend a chainable plugin approach. As this screams reusability.. You can make the equalHeights specific to the collection...

$.fn.equalHeights = function () {
  var max = 0;
  return this
    .each(function(){
      var height = $(this).height();
      max = height > max ? height : max;
    })
    .height(max);
};
// don't combine collections unless you want everything same height..
$('.top-menu a').equalHeight();
$('footer a').equalHeight();

You could take a step further and make height a property you take in so you could use for height or width like so...

$.fn.equalProp = function (prop) {
  var max = 0;
  return this
    .each(function(){
      var oProp = $(this)[prop]();
      max = oProp > max ? oProp : max;
    })
    [prop](max);
};

$('.top-menu a, .top-menu div').equalProp('height').equalProp('width');
PDA
  • 766
  • 6
  • 10
1

I posted a similar question a few days ago and here's the piece of code that worked for me.

*** #column_left, #column_center, #column_right: are the three column divs that are supposed to have the same height. I suppose it can work with more or less number of columns.

<script type='text/javascript'
 src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'></script>

<script type='text/javascript'>
$(document).ready(function() {

    // get the heights
    l = $('#column_left').height();
    r = $('#column_right').height();
    c = $('#column_center').height();

    // get maximum heights of all columns
    h = Math.max(c, Math.max(l, r));

    // apply it
    $('#column_left').height(h);
    $('#column_right').height(h);
    $('#column_center').height(h);
    });
</script>

It was posted by user "widyakumara". I hope it helps you too.

zekia
  • 4,527
  • 6
  • 38
  • 47
1

@ktsixit you should give a class to all your columns instead of using unique IDs in this case. You can then calculate and apply the tallest height to all items at once. Even if you don't or can't use a class, you should at least use variables to store the div ID references. With the code above, you're parsing the DOM 6 times. You could at least reduce that to 3 parses by using variables.

Wigley
  • 11
  • 1