new jQuery Plugin – Equal Height Columns

Have you ever been in the situation where you need to use floated columns on your web page and want them all to be the same height as the tallest column? If you have, then you will probably know that you can achieve it with CSS. However, I don’t like this approach, and quite frankly have never understood it fully anyway. As I used jQuery in practically every project I write I thought it would be neater to just write a jQuery plugin which would do all of the hard work for me.

Here is the plugin code:

/* 
 /* * @Copyright (c) 2013 James Stoddern - info@jamesstoddern.net  
 * web:jamesstoddern.net  
 *  
 * Permission is hereby granted, free of charge, to any person  
 * obtaining a copy of this software and associated documentation  
 * files (the "Software"), to deal in the Software without  
 * restriction, including without limitation the rights to use,  
 * copy, modify, merge, publish, distribute, sublicense, and/or sell  
 * copies of the Software, and to permit persons to whom the  
 * Software is furnished to do so, subject to the following  
 * conditions:  
 * The above copyright notice and this permission notice shall be  
 * included in all copies or substantial portions of the Software.  
 *  
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,  
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES  
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND  
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT  
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,  
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING  
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR  
 * OTHER DEALINGS IN THE SOFTWARE.  
 *  
 * How to use it:  
 *  
 * If you have a series of floated columns, which you wish to make the same height, give them all the same  
 * class, and then run the plugin. It will determine the tallest div, and equalise the height of the rest  
 *  
 * $('.selector').equalHeights();  
 *  
 *  
 */

(function( $ ) {
    $.fn.equalHeights = function() {
        var tallestElement = 0;
        var startRow = 0;
        var elements = new Array();
        var $currentElement;
        var topPosition = 0;

        this.each(function() {

            $currentElement = $(this);
            topPostion = $currentElement.position().top;

            if (startRow != topPostion) {
                for (currentDiv = 0 ; currentDiv < elements.length ; currentDiv++) {
                    elements[currentDiv].height(tallestElement);
                }

                elements.length = 0;
                startRow = topPostion;
                tallestElement = $currentElement.height();
                elements.push($currentElement);

            } else {
                elements.push($currentElement);
                tallestElement = (tallestElement < $currentElement.height()) ? ($currentElement.height()) : (tallestElement);

            }

            for (currentDiv = 0 ; currentDiv < elements.length ; currentDiv++) {
                elements[currentDiv].height(tallestElement);
            }

        });

    };
}) ( jQuery );

So how do you use it?

Well, firstly copy the plugin from this page, and save it to a folder where you normally keep your jquery scripts. I usually call it jquery_equalheight.js. After you have included jQuery in your page, simply inlcude the plugin as well.

Next, all you need to do is run the plugin against a selector. In the case of your columns, make sure they all have the same class and then add this neat bit of code to run the plugin.

jQuery('.column').equalHeights();

Enjoy!

3 thoughts on “new jQuery Plugin – Equal Height Columns

  1. Hi,

    I read your article on equal columns widths and I’ve applied everything as you’ve shown, but when I view my page, all of the columns have changed height but are still not equal heights. Also, the row below is now sat on top of the content row with the columns in that I’m trying to equalize. Your video is so simple and straightforward and I’m 99.9% I’ve done it right and so I’m lost as to what could be wrong with it.

    I’m assuming this works with bootstrap 3 yes and in Chrome?

    Any other thoughts as to why it might not be working?

    Thanks
    Andy

    1. Ok so I managed to work out what I was doing wrong. I was simply applying the selector to the wrong element.

      I do have another challenge for you though if you’re up for it.

      Whilst your code (which is great btw!) equals the heights of my columns, there are a couple of issues using it with multiple rows of columns that need to be different heights. So, how would I make it selective so that I can apply equal heights to one set of columns nested in a row and then apply equal (but different) heights to another set of columns nested in a different row?

      Lastly, when I view the results in mobile format, the heights of the columns remain equal, whereas logic suggests that once columns are stacked in mobile layout, the columns should revert to content based height, yes? In which case, how would your code need to be amended to achieve the above issues?

      Thanks

      Andy.

  2. Hi James

    Thank you for this code, after a bit of messing about I have got this to work on my Bootstrap 3 project, I have a database displaying a catalogue with a photo and text underneath in a series of boxes 6 across the page then breaking and doing it again up to 40 items, I have resized all the photos so they are the same it is just the amount of text that varies, I have applied equal height to the well
    jQuery(‘.well’).equalHeights();
    and it works well, I have also applied equal height to a series of columns in the footer and since then occasionally in Chrome the equal height on the well shrinks them and makes a mess, one or two refreshes and it is all working again.
    I was wondering if I should crate another instance of .equalHeights() and if so how would you suggest? It only seems to happen in Chrome and not IE or Firefox, I can live with it as so far nobody else seems to have noticed this.
    Regards
    James

Leave a Reply

Your email address will not be published. Required fields are marked *