Nov
2010
jQuery equal height columns plugin
Problem:
Recently I have been working on a project and I came across an issue where I have to display ‘Featured products’ in 4 columns with a ‘Read more…’ link at the end of each product details. The number of lines for these 4 columns were not equal and so was not looking clean. To demonstrate, I will take the following example where we have 3 panels/columns with unequal height (live demo):
Solution:
I wrote a small jQuery plugin (only 696 bytes) to achieve 2 main goals:
- Make these panels of equal height by identifying the smallest height among these panels and than reduce the text in other panels to make it of equal height to the smallest.
- and add an option to put three dots at the end of the paragraph if its not ending with a full stop.
It should also gracefully degrade if no Javascript support is available on the browser i.e people with Javascript disabled on their browsers will see the panels with unequal height.
Should be able to start a plugin as simple as:
$('#panelsWrapper').equalheightcols({
itemsToEqualize : 'p',
threeDots : true;
)
Plugin code (source):
/**
* jQuery.equalheightcols - Making an equal height panels.
* @copyright (c) 2010 Aamir Afridi
* Dual licensed under MIT and GPL.
* Date: 25/11/2010
* @author Aamir Afridi - aamirafridi(at)gmail(dot)com | http://www.aamirafridi.com
* @version 1.0
*/
;(function($) {
$.fn.equalheightcols = function(options) {
return this.each(function() {
//Extend the options if any provided
var o = $.extend({}, $.fn.equalheightcols.defaults, options),
$this = $(this);
//Find the shortest height item
var shortestHeight = 10000;
$this.find(o.itemsToEqualize).each(function(){
shortestHeight = $(this).height() < shortestHeight ? $(this).height() : shortestHeight;
});
//Now loop through the each element and keep removing word by word untill its height reaches to the minimal
$this.find(o.itemsToEqualize).each(function(){
//If height is already equal to the minimum than return
if($(this).height()==shortestHeight) return;
//Now loop until the heights is equal to the minimum height by removing word by word
while($(this).height()>shortestHeight)
{
//Regex to remove the last word from the text
$(this).html($.trim($(this).html()).replace(/ \S*$/,''));
}
//Now check if the last char of the last word is not a full stop than remove the last word and put three dots with the last word if the option is set to true
if($.trim($(this).html()).slice(-1)!='.' && o.threeDots)
{
//Remove one more word and append three dots just incase if three dots will increase one more line
$(this).html($.trim($(this).html()).replace(/ \S*$/,'')+' ...');
}
});
});
}//End of Plugin
// Public: plugin defaults options
$.fn.equalheightcols.defaults = {
itemsToEqualize : 'p',
threeDots : true //If the last word is not fullstop than put three dots at the end
};
})(jQuery);
After applying the plugin the above example will look like:
Links:
Note: If you are looking for a plugin which will calculate the heightest column and than assign that height to the rest of the panels than you should visit: Equal Height Columns with jQuery





Can’t believe that you have to jump through all these hoops to get this done. With WordPerfect all you have to do when setting up columns is to select the Balanced Newspaper selection and it is done.
Interesting variation on using jQuery to make uniform column heights; and the first solution that REDUCED the height of columns to be equal. Very nice.
Very interesting ‘equal heights’ approach. I’m certainly bookmarking this one.
Two thoughts though:
1. I think the usage of this plugin is very limited, seems that it can only work on columns with paragraphs on them. What about if you have lists in the columns? You can just cut down the lists, if they are products that’s something I’m sure you don’t want to do.
2. The “three dots” are called Ellipsis, in your code (and plugin usage) you should name the ellipsis by its name, ellipsis, and not by “three dots”, it looks more appropiate. Actually, the dots in the Ellipsis are closer together than if you just type 3 dots. The Ellipsis is a punctuation element itself just like a comma or a period, etc.
thank u for ur comments.
It makes a lot of sense. I will improve it soon :)
Does it work in responsive layout?
Depends on what sort of structure do you have of your columns but I am sure it should work fine. But if you want to show the columns vertically (on top of each other) than there is no need to use this plugin.