title: Make Two Columns an Almost Equal Height Without JQuery
body: <p>I have an HTML page with content organized into two columns, like this:</p>
  
  <pre><code>&lt;div class="col" style="width:50%;float:left;"&gt;
     &lt;div class="section" name="introduction"&gt;
        bla bla bla
     &lt;/div&gt;
     &lt;div class="section" name="games"&gt;
        bla bla bla bla bla bla
     &lt;/div&gt;
     &lt;div class="section" name="license"&gt;
        bla bla bla bla bla bla bla
     &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="col" style="width:50%;float:right;"&gt;
     &lt;div class="section" name="about"&gt;
        bla bla bla bla bla
     &lt;/div&gt;
     &lt;div class="section" name="contactus"&gt;
        bla bla bla bla bla
     &lt;/div&gt;
     &lt;div class="section" name="etc"&gt;
        bla bla
     &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Where "col" stands for column. Pretty basic stuff.</p>
  
  <p>As you can see, the sections, which make up the columns, are not of equal length. That is, there is more text in section two than section one. For this reason, section two will be taller than section one if the browser window is small enough.</p>
  
  <p>What I want to accomplish is, after the columns have loaded, I want to divide the sections amongst the columns <em>so that the columns are as close as possible to equal height</em>. So, if the column on the left is taller compared to the one on the right, the function will move the appropriate amount of sections from the left column to the right column.</p>
  
  <p>But if the browser window is big enough that the text does not have to wrap, because each section is, in this example, quite a short piece of text, nothing needs to happen. They're all the same height.</p>
  
  <p>I also want to be able to call this function more than once if more content is added to a section/the window is resized.</p>
  
  <p>Here is some Javascript code I have tried:</p>
  
  <pre><code>function sections() {
  // collect the height of every section.
  sectionheightarray0 = new Array();
  sectionheightarray1 = new Array();
  for(i=0;i&lt;document.getElementsByClassName("section").length;i++){
  sectionheightarray1[i]=document.getElementsByClassName("section")[i].clientHeight;
  }
  Array.prototype.sum = function() {
  var count=0;
  for (i=this.length;i--;) {
  count+=this[i];
  }
  return count;
  }
  // using the height of each of the sections, check if the right side is longer than the left, and if so, make the left side longer by moving a section into the left column.
  while (sectionheightarray0.sum()&lt;sectionheightarray1.sum()) {
  sectionheightarray0.push(sectionheightarray1.shift());
  }
  // by default, the left side is always longer than the right. Uncomment to not always have the left side be longer depending on the height
  // if (sectionheightarray0.sum()-sectionheightarray1.sum()&gt;(sectionheightarray0.sum()-sectionheightarray0[sectionheightarray0.length-1])-(sectionheightarray1.sum()+sectionheightarray0[sectionheightarray0.length-1])) {
  // sectionheightarray1.push(sectionheightarray0.pop());
  // }
  // swap the sections here.
  if (document.getElementById("col1").getElementsByClassName("section").length&lt;sectionheightarray0.length) {
  while (document.getElementById("col1").getElementsByClassName("section").length!=sectionheightarray0.length) {
  document.getElementById("col1").appendChild(document.getElementById("col2").getElementsByClassName("section")[0]);
  }
  } else {
  if (document.getElementById("col2").getElementsByClassName("section").length&lt;sectionheightarray1.length) {
  while (document.getElementById("col2").getElementsByClassName("section").length!=sectionheightarray1.length) {
  document.getElementById("col2").insertBefore(document.getElementById("col1").getElementsByClassName("section")[(document.getElementById("col1").getElementsByClassName("section").length-1)], document.getElementById("col2").getElementsByClassName("section")[0]);
  }
  }
  }
  sectionheightarray0 = [];
  sectionheightarray1 = [];
  }
  sections();
  // if the ser resizes the viewport, repeat the process.
  window.onresize=sections;
  </code></pre>
  
  <p>Surprisingly it does work to some extent, but as you can see, it isn't a very clean solution. It takes up a lot of space and I was hoping for a cleaner and smaller, perhaps more official solution. Cross browser is obviously preferred.</p>
  
  <p>I know the Columnizer JQuery script exists for this purpose, but I would like to be able to do this without the use of JQuery. Partly that's because I find JQuery to be quite confusing, but mainly it is because I don't use JQuery anywhere else on my site, so I would only be calling it for this one purpose - which seems overkill for such a simple task. Javascript or CSS solutions are fine. Any ideas? Thanks in advance.</p>