Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

MooTools Equal Heights Plugin: Equalizer

11 Responses »

Keeping equal heights between elements within the same container can be hugely important for the sake of a pretty page. Unfortunately sometimes keeping columns the same height can't be done with CSS -- you need a little help from your JavaScript friends. Well...now you're in luck.

The MooTools JavaScript

var Equalizer = new Class({
	initialize: function(elements) {
		this.elements = $$(elements);
	},
	equalize: function(hw) {
		if(!hw) { hw = 'height'; }
		var max = 0,
			prop = (typeof document.body.style.maxHeight != 'undefined' ? 'min-' : '') + hw; //ie6 ftl
			offset = 'offset' + hw.capitalize();
		this.elements.each(function(element,i) {
			var calc = element[offset];
			if(calc > max) { max = calc; }
		},this);
		this.elements.each(function(element,i) {
			element.setStyle(prop,max - (element[offset] - element.getStyle(hw).toInt()));
		});
		return max;
	}
});

The first step is cycle through each element to find which is the tallest (or widest -- this plugin accommodates for equal widths as well). Once we know the largest number we can set the min-property for the element to that number while subtracting border and padding. Simple!

The Sample Usage

var columnizer = new Equalizer('.sizeMe').equalize('height'); //call .equalize() as often as you want!

Obviously the class is super small. You create one instance, passing elements initially. From that point forward you can simply call the equalize method as many times as you would like. This is especially useful if the contents of a given column changes frequently.

Let me know if you see any room for improvement. I think this plugin will be quite useful for many developers.

Discussion

  1. June 24, 2010 @ 8:26 pm

    Great idea, encapsulating this in a class. I’ve done a similar thing before but just used a loop and had it messily embedded in my code.

    Also, MooTools FTW!

  2. arian
    June 25, 2010 @ 12:54 am

    Would be nice to extend Elements so you can do something like this:

    $$(‘.someClass’).equalize()

    or new Elements([new Element('div')]).equalize();

    Elements.implement({
    equalize: function(stop, prevent){
    new Equalizer(this, stop, prevent).equalize();
    return this;
    }
    });

  3. florian
    June 25, 2010 @ 2:06 am

    line 15:

    element.setStyle(prop,max – (element[offset] – element.getStyle(hw).toInt()));

    …less code :) mootools ftw

  4. June 25, 2010 @ 2:27 am

    Here’s the approach you suggested arian…
    http://www.jsfiddle.net/sixtyseconds/U8EfF/

  5. June 25, 2010 @ 3:13 am

    …and the Forge plugin: http://bit.ly/ds7tXB

  6. alex
    June 25, 2010 @ 8:08 am

    Chris and David, David and Chris, you both saved me some time. Thanks! :D

  7. June 25, 2010 @ 8:45 am

    @Florian, @Chris the Developer: Nice work!

  8. June 25, 2010 @ 10:17 am

    A compliment from Mr Walsh! I feel so important now :D

  9. zoop
    July 13, 2010 @ 9:50 am

    @Chris the Developer says:
    …and the Forge plugin: http://bit.ly/ds7tXB

    http://www.mootools.net/forge/p/elements_equalize

    The “Download” link is broken

  10. July 13, 2010 @ 7:19 pm

    @Chris the Developer: As you should!

    I’ve added my plugin to the Forge: http://mootools.net/plugins/p/equalizer

  11. bruce
    July 14, 2010 @ 6:54 pm

    Its not working properly on

    Chrome 5.0.375.99 beta on Ubuntu

    I have the same version ( minus beta ) on my window box, works fine.

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!