<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

 <title>Dave Dash</title>
 <link href="http://davedash.com/tag/equal-columns/atom.xml" rel="self"/>
 <link href="http://davedash.com/tag/equal-columns"/>
 <updated>2012-01-17T21:54:19-08:00</updated>
 <id>http://davedash.com/</id>
 <author>
   <name>Dave Dash</name>
   <email>dd+atom1@davedash.com</email>
 </author>

 
 <entry>
   <title>Equal height columns with jQuery</title>
   <link href="http://davedash.com/2007/05/22/equal-height-columns-with-jquery/"/>
   <updated>2007-05-22T00:00:00-07:00</updated>
   <id>http://davedash.com/2007/05/22/equal-height-columns-with-jquery</id>
   <content type="html">&lt;p&gt;[tags]css, jQuery, layout, javascript, equal, columns, equal columns[/tags]&lt;/p&gt;

&lt;p&gt;I've seen a few examples of how to equalize column heights using javascript, and none of them seem appealing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://www.tomdeater.com/jquery/equalize_columns/&quot;&gt;jquery.equalizecols.js&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;This required a few other libraries, and I wanted more flexibility (e.g. where the column should grow in order to equalize)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.projectseven.com/tutorials/css/pvii_columns/index.htm&quot;&gt;Project 7&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;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.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.html.it/articoli/nifty/index.html&quot;&gt;Nifty Corners&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;I had trouble with the syntax, but I liked how it just created a new element out of thin air...&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;So I wrote my own:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$(&quot;#col1, #col2&quot;).equalizeCols();
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;will equalize the columns as expected&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$(&quot;#col1, #col2&quot;).equalizeCols(&quot;p,p&quot;);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;will equalize the columns and add the extra space after the &lt;code&gt;p&lt;/code&gt; tag in &lt;code&gt;#col1&lt;/code&gt; or &lt;code&gt;#col2&lt;/code&gt; (whichever is shorter).&lt;/p&gt;

&lt;p&gt;Here's our function:&lt;/p&gt;

&lt;div&gt;&lt;textarea name=&quot;code&quot; class=&quot;js&quot;&gt;

(function($) {
  $.fn.equalizeCols = function(children){
    var child = Array(0);
    if (children) child = children.split(&quot;,&quot;);
    var maxH = 0;
    this.each(
      function(i) 
      {
        if (this.offsetHeight&gt;maxH) maxH = this.offsetHeight;
      }
    ).css(&quot;height&quot;, &quot;auto&quot;).each(
      function(i)
      {
        var gap = maxH-this.offsetHeight;
        if (gap &gt; 0)
        {
          t = document.createElement('div');
          $(t).attr(&quot;class&quot;,&quot;fill&quot;).css(&quot;height&quot;,gap+&quot;px&quot;);
          if (child.length &gt; i)
          {
            $(this).find(child[i]).children(':last-child').after(t);
          } 
          else 
          {
            $(this).children(':last-child').after(t);
          }
        }
      }  
    );
    
  }
})(jQuery);

&lt;/textarea&gt;&lt;/div&gt;


&lt;p&gt;This requires jQuery of course, and it hasn't been tested much.&lt;/p&gt;
</content>
 </entry>
 

</feed>

