Liquid Column Display Layout – Part II

In my earlier post I had discussed about the designing the liquid 3 column display layout. While developing the structure I had some problem with the column heights. Depending upon the contents length of those columns varies. I have declared the height property as auto in the css. I could have declared them with some specific pixel value to make the columns of equal length. But if you consider your pages to contain dynamic content generated by server side scripting then this technique may fail. Particularly in Internet Explorer the data part may be extended beyond the columns, which could make the structure awful. So I was in need to find some other solutions for that purpose.

I thought of using some JavaScript to make all the columns height equal. The algorithm to do this is very simple. Calculate the max height among three columns and assign it for all the columns. So first I introduced three different IDs for these three columns to access the properties of those columns using Javascript document.getElementById() method. So I changed the HTML code as follows:

  1. <div class="container">
  2.    <div class="contentDiv" id="contentDiv">
  3.       <div class="contentDiv_left" id="contentDiv_left"><img class="LcolImgDiv" src="../images/c2.jpg" border="0" alt="" /></div>
  4.       <div class="contentDiv_mid" id="contentDiv_mid">
  5.          <div class="hD1">Header Text</div>
  6.          <div class="hD2">Header text 2</div>
  7.          <p class="hD3">The text….</p>
  8.       </div>
  9.       <div class="contentDiv_right" id="contentDiv_right">
  10.          <ul>
  11.             <li><a class="item1" href="director/index.html">DIRECTOR SPEAKS </a> <img src="../images/bullet1.gif" alt="" vspace="0" /></li>
  12.             <li><a class="item1" href="students/index.html">STUDENTS </a> <img src="../images/bullet1.gif" alt="" vspace="2" /></li>
  13.             <li><a class="item1" href="alumni/index.html">ALUMNI </a> <img src="../images/bullet1.gif" alt="" vspace="2" /></li>
  14.          </ul>
  15.       </div>
  16.    </div>
  17.    <div class="footerDiv">
  18.       <div class="footerDivText">Copyright text</div>
  19.    </div>
  20. </div>

contentDiv_left for the left column, contentDiv_mid for the middle column and contentDiv_right for the right one. contentDiv ID for the container div for these 3 columns.


Then my job was to find out the maximum height of these 3 columns. First I used offsetHeight property for this purpose. Calculate the max height and assign it to all the 3 columns style.height property. While testing the code I found it was working fine with Internet Explorer. In case of FireFox or Opera or Netscape browsers the code was malfunctioning, more specifically it was unable to find out the height of those columns using offsetHeight property. So I started looking for bug. While doing that I found out offsetHeight is a readonly property of the DHTML Object model introduced by MSIE. But is not part of any W3C specification or technical recommendation. So incase of browsers like FF or Opera it won’t work. So I started looking for some alternate property to find out the heights of those columns. There are some properties which didn’t worked for me are element.style.height, element.style.max-height etc. I tried with
window.getComputedStyle(element,null).getPropertyValue(“height”).
As window.getComputedStyle method can be used to get the styles of an element that are not set using the style attribute or with JavaScript. But it didn’t work too. Finally I worked out with this method :
document.defaultView.getComputedStyle(element, null).getPropertyValue(“height”).
The document.defaultView returns a reference to the default AbstractView for the document. This document.defaultView is part of the DOM Level 2 DocumentView interface. This interface is not a mandatory part of Document Object Model Level 2 Core specification. DOM of IE doesn’t implement this but DOM in mozilla supports this. So I put a browser check and utilized the above method for browser like FF, Opera and offsetHeight propery for IE. Hence my code looks like this :

  1. function checkBrowserComponent() {
  2.    var ua = navigator.userAgent.toLowerCase();
  3.    //var p = ua.indexOf(‘msie’);
  4.    //alert(p);
  5.    if(ua.indexOf(‘msie’)!=-1) {
  6.       return "ie";
  7.    }
  8.    else if(ua.indexOf(‘firefox’)!=-1) {
  9.       return "ff";
  10.    }
  11.    return "oth";
  12. }
  13.  
  14. function maintainMaxColHeight(pHeight) {
  15.    var defMaxHeight = 650;
  16.    var elmMain = document.getElementById("contentDiv");
  17.    var elmLS = document.getElementById("contentDiv_leftSingle");
  18.    var elmL = document.getElementById("contentDiv_left");
  19.    var elmM = document.getElementById("contentDiv_mid");
  20.    var elmR = document.getElementById("contentDiv_right");
  21.    var ua = checkBrowserComponent();
  22.    if(ua == ‘ff’) {
  23.       if(elmLS != null) {
  24.            //alert(elmLS);
  25.            var lh = document.defaultView.getComputedStyle(elmLS, null).getPropertyValue("height");
  26.            lh = parseInt(lh);
  27.            if(lh > defMaxHeight) {
  28.               defMaxHeight = lh;
  29.            }
  30.       }
  31.       else {
  32.            var lh = document.defaultView.getComputedStyle(elmL, null).getPropertyValue("height");
  33.            lh = parseInt(lh);
  34.            if(lh > defMaxHeight) {
  35.               defMaxHeight = lh;
  36.            }
  37.            var mh = document.defaultView.getComputedStyle(elmM, null).getPropertyValue("height");
  38.            mh = parseInt(mh);
  39.            if(mh > defMaxHeight) {
  40.               defMaxHeight = mh;
  41.            }
  42.       }
  43.       var rh = document.defaultView.getComputedStyle(elmR, null).getPropertyValue("height");
  44.       rh = parseInt(rh);
  45.       if(rh > defMaxHeight) {
  46.          defMaxHeight = rh;
  47.       }
  48.    }
  49.    // this sets the height manually
  50.    if(pHeight > defMaxHeight) {
  51.       defMaxHeight = pHeight;
  52.    }
  53.    else if(ua == ‘ie’) {
  54.       //elmL.currentStyle
  55.       if(elmLS != null) {
  56.            var lh = elmLS.offsetHeight;
  57.            if(lh > defMaxHeight) {
  58.               defMaxHeight = lh;
  59.            }
  60.       }
  61.       else {
  62.            var lh = elmL.offsetHeight;
  63.            if(lh > defMaxHeight) {
  64.               defMaxHeight = lh;
  65.            }
  66.            var mh = elmM.offsetHeight;
  67.            if(mh > defMaxHeight) {
  68.               defMaxHeight = mh;
  69.            }
  70.       }
  71.       var rh = elmR.offsetHeight;
  72.       if(rh > defMaxHeight) {
  73.          defMaxHeight = rh;
  74.       }
  75.    }
  76.    if(elmLS != null) {
  77.        elmLS.style.height = defMaxHeight+"px";
  78.    }
  79.    else {
  80.        elmL.style.height = defMaxHeight+"px";
  81.        elmM.style.height = defMaxHeight+"px";
  82.    }
  83.    elmR.style.height = defMaxHeight+"px";
  84.    elmMain.style.height = defMaxHeight+"px";
  85. }

I called this function maintainMaxColHeight() in onLoad within body tag.

After adding this script the page now looks like :

3 equal cols liquid layout

Now what’s the conclusion? I think in case of maintaining the equal height for the columns there are 2 options. First one is to put a fixed pixel value as height property in css for all the columns. Next one is implementing this JavaScript code. For this second option browser should support JavaScript (I believe most of the current browsers do so). In my opinion if a website is designed using liquid column display layout where all pages contain static information, first option will the best for that case. I always prefer to avoid unnecessary script execution for web pages as it reduces the page download and display time. The second option is best for websites containing dynamic information (like my project).

The project where I have implemented this layout is near to completion. Still it is not fully online I won’t be able to show you. I need to wait a little more to show you the live example.

Leave a Reply