Equal height columns with jQuery

by Dave Dash 22May07

[tags]css, jQuery, layout, javascript, equal, columns, equal columns[/tags]

I've seen a few examples of how to equalize column heights using javascript, and none of them seem appealing:

  • jquery.equalizecols.js
    • This required a few other libraries, and I wanted more flexibility (e.g. where the column should grow in order to equalize)
  • Project 7
    • The Project 7 approach was the most interesting, but the code seemed a bit messy and not so open source friendly (even thought it might have been). It would let you specify which element was to grow inside a column.
  • Nifty Corners
    • I had trouble with the syntax, but I liked how it just created a new element out of thin air...

So I wrote my own:

$("#col1, #col2").equalizeCols();

will equalize the columns as expected

$("#col1, #col2").equalizeCols("p,p");

will equalize the columns and add the extra space after the p tag in #col1 or #col2 (whichever is shorter).

Here's our function:

This requires jQuery of course, and it hasn't been tested much.


Where am I?

This is a single entry in the weblog.

"Equal height columns with jQuery" is filed under css and programming. It was published in May 2007.

May 2007
M T W T F S S
« Apr   Jun »
 123456
78910111213
14151617181920
21222324252627
28293031  

need more help

If you found our tutorials and articles to be useful, but are still looking for more hands on help, consider hiring us. Find out more about how Spindrop can help you.

 

21 Responses to “Equal height columns with jQuery”


  1. 1 Tom Deater Posted May 31st, 2007 - 2:56 pm

    Hi Dave,

    FYI, I’ve updated jquery.equalizecols.js and it no longer requires the dimensions plugin to function.

    Also, you might want to check out Michael Futreal’s vjustify plugin.

    http://michael.futreal.com/jquery/vjustify

    Tom

  2. 2 Dave Dash Posted May 31st, 2007 - 3:39 pm

    Tom,

    Awesome. Thanks for the link and the update. I’ll look at both next time I have to do this. I think I had to go with mine because I had decorated boxes (with nifty cols or something) and needed an inner element to grow rather, than just the height of the box… it was very much a unique situation.

    -d

  3. 3 Rey Bango Posted June 25th, 2007 - 9:23 am

    @Dave: I just found out about your plugin. Have you posted it to the jQuery plugin repository? If not, it would be awesome if you could do that so we can have an updated copy there: http://jquery.com/plugins/

    Rey jQuery Project Team

  4. 4 Dave Dash Posted July 9th, 2007 - 5:41 pm

    Rey

    I posted it here.

    Thanks.

    -d

  5. 5 Barry Ramsay Posted July 17th, 2007 - 7:59 pm

    Just wanted to say thank you for your neat bit of code.

  6. 6 kim Posted July 30th, 2007 - 1:43 am

    hi,

    nice plugin! works good,but have a little problem.

    Using this with IE, I get the fill div become too big when it’s smaller than the font-size (maybe in particular mode ?).

    How about

    $(t).attr(”class”,”fill”).css(”height”,gap “px”); to $(t).attr(”class”,”fill”).css(”height”,gap “px”).css(”overflow”,”hidden”);

    ?

  7. 7 Jens Grochtdreis Posted October 23rd, 2007 - 7:34 am

    There’s a similar solution, mostly interesting for all German-speaking jQuery-users: http://blog.highresolution.info/index.php?/highresolution/comments/equalheightsmit_jquery/

  8. 8 Matthew James Taylor Posted January 27th, 2008 - 10:32 pm

    What if JavaScript is turned off? I’ve managed to do equal height columns without JavaScript and it works in all common browsers. Here are my examples:

    Pixels: 3 column liquid layout – pixel widths

    Ems: 3 column liquid layout – em widths

    Percent: 3 column liquid layout – percentage widths

Who's linking?

  1. 1 Morning Brew #9 Pingback on May 23rd, 2007
    "[...] Equal height columns with JQuery. [...] "
  2. 2 Iarfhlaith Kelly - Code agus Craic » Blog Archive » New Site - HomeHealthClubs.com Pingback on May 23rd, 2008
    "[...] took the chance to use some of jQuery’s more subtle plugins to render the rounded corners and equalise div ... "
  3. 3 老孔的Blog » Blog Archive » List of Useful jQuery Plugins Pingback on May 28th, 2008
    "[...] to display on a webpage. Dimensions Get accurate window and document attributes across browsers. Equalize Columns Make columns the ... "
  4. 4 Blox.Svbasi · List of Useful jQuery Plugins Pingback on Jun 28th, 2008
    "[...] Equalize Columns Make columns the same height as the longest column. [...] "
  5. 5 Digital Path Blog - Web Development Fragments - Website Design and Development, Graphics, Web Content Management System, Auckland, New Zealand Pingback on Aug 21st, 2008
    "[...] to display on a webpage. Dimensions Get accurate window and document attributes across browsers. Equalize Columns Make columns the ... "
  6. 6 Using Javascript to Fix 12 Common Browser Headaches Pingback on Oct 12th, 2008
    "[...] 1.2 Equal height columns with jQuery [...] "
  7. 7 delade | Using Javascript to Fix 12 Common Browser Headaches Pingback on Oct 13th, 2008
    "[...] 1.2 Equal height columns with jQuery [...] "
  8. 8 Using Javascript to Fix 12 Common Browser Headaches | POLPDESIGN Pingback on Oct 13th, 2008
    "[...] 1.2 Equal height columns with jQuery [...] "
  9. 9 RIA Revolution » Using JavaScript to Fix CSS Pingback on Oct 15th, 2008
    "[...] You can also set equal height columns with jQuery. [...] "
  10. 10 用Javascript修正12个常见的浏览器问题 | 前端观察 Pingback on Oct 17th, 2008
    "[...] 1.2 用jQuery匹配栏目高度 [...] "
  11. 11 Using Javascript to Fix 12 Common Browser Headaches | SulVision Pingback on Nov 8th, 2008
    "[...] 1.2 Equal height columns with jQuery [...] "
  12. 12 12种Javascript解决常见浏览器兼容问题的方法 | 丁总 Pingback on Apr 25th, 2009
    "[...] 1.2 用jQuery匹配栏目高度 [...] "
  13. 13 Using Javascript to Fix 12 Common Browser Headaches « Dogfeeds IT Telescope Pingback on Jul 13th, 2009
    "[...] 1.2 Equal height columns with jQuery [...] "

Further Help

If you require more hands on assistance, we do offer affordable hands on support.

Leave a Reply


Comment guidelines: No spamming, no profanity, and no flaming. Inappropriate comments will be deleted outright.