Web Design Trick: Javascript Column Heights

This is a simple web design trick I use in order to keep the left and center columns the same height when needed. There are a few CSS hacks you can try to accomplish the same thing, however because of browser compatibility I find it is much better to use javascript.

So to be clear, here is the problem, you design a website to have a left column and a center column. The left column content almost stays the same from page to page so it is usually the same height. The center column, which houses the main content, changes from page to page so sometimes it may be longer or shorter than the left column. By the way, you can use this script with as many columns as you like, it is not restricted to just two.

Script Overview

The javascript is placed in the header section of the page. Classes are placed inside of the divs that you want to be the same height. When the page loads, the javascript determines which of the divs is the tallest and makes the other div the same height. Pretty simple huh?

First copy this into the head section of your web page, (or you can use an external javascript). The comments should explain the script. It will work right out of the box.

  1.  
  2. fixHeight=function(){
  3.               // Declare and initialize variables
  4.                var divs,contDivs,maxHeight,divHeight,d;
  5.                 maxHeight=0;
  6.                 contDivs=[];
  7.                 divs=document.getElementsByTagName(‘div’);              // get all <div> elements in the document
  8.                 for(var i=0;i<divs.length;i++){                         // for all divs…
  9.                      if(/\bfixheight\b/.test(divs[i].className)){    // find those with the class attribute ‘fixheight’…
  10.                               d=divs[i];
  11.                               contDivs[contDivs.length]=d;
  12.                                // determine their height, defined as either offsetHeight or pixelHeight
  13.                                 if(d.offsetHeight){ divHeight=d.offsetHeight; }
  14.                                 else if(d.style.pixelHeight){ divHeight=d.style.pixelHeight; }
  15.                                 // Keep track of the largest div height
  16.                                 maxHeight=Math.max(maxHeight,divHeight);
  17.                       }
  18.              }
  19.               // assign all divs the height of the tallest div
  20.               for(var i=0;i<contDivs.length;i++){
  21.                       contDivs[i].style.height=maxHeight + "px";
  22.               }
  23.        }
  24.        // Run fixheight on page load
  25.        window.onload=function(){
  26.               if(document.getElementsByTagName){ fixHeight(); }
  27.       }
  28.       // ]]>
  29.  

The divs you want to have same height should look like this:

  1.  
  2. <div class="fixheight">
  3.  
  4. </div>
  5.  

I hope this script is helpful to you. Have fun web designing

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Google
  • StumbleUpon
  • Technorati
  • TwitThis

One Response to “Web Design Trick: Javascript Column Heights”

  1. Ray from Sunshine coast web design says:

    Hi,
    Very nice post.Scripts is very good.
    Thanks for the sharing the information.

Leave a Reply

This site uses KeywordLuv. Enter YourName@YourKeywords in the Name field to take advantage.

Get expert SEO Services designed
to add real value to your business

Get a web site that is designed to accomplish your business goals

Get a state of the art, SEO
optimized e-commerce website

SEO Blog

SEO Blog

Read our SEO Blog for tips and tricks.

Read More
Recent Posts
Categories