Web Standards
  Home arrow Web Standards arrow Page 3 - Matching div heights with CSS and JavaScri...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WEB STANDARDS

Matching div heights with CSS and JavaScript
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 34
    2005-03-14

    Table of Contents:
  • Matching div heights with CSS and JavaScript
  • The kingdom of non-matching heights
  • More non-matching heights just around the corner
  • The JavaScript solution

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Matching div heights with CSS and JavaScript - More non-matching heights just around the corner


    (Page 3 of 4 )

    Based on the previous examples, suppose we need to add more content to the left column of the Web document, while the content <div> maintains its original height. This simple code will do the trick:

    <html>

    <head>

    <title>THREE-COLUMN LAYOUT</title>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

    <style type="text/css">

    body {

         margin: 0px;

    }

    #leftcol {

         background: #ccf;

         float: left;

         width: 17%;

    }

    #content {

         width: 65%;

         float: left;

         background: #ccf;

    }

    #rightcol {

         float: right;

         width: 17%;

         background: #ccf;

    }

    </style>

    </head>

    <body>

    <div id="leftcol">

    Left Section<br /><br />

    Content goes here...<br />

    Content goes here...<br />

    Content goes here...<br />

    Content goes here...<br />

    Content goes here...<br />

    Content goes here...

    </div>

    <div id="rightcol">

    Right Section<br /><br />

    Content goes here...

    </div>

    <div id="content">

    Content Section<br /><br />

    Content goes here...<br />

    Content goes here...

    </div>

    </body>

    </html>

    Once again, unfortunately, <div> elements will be unequally displayed in the document, resulting in the following undesirable visual output:

    The last remaining case to be analyzed, which certainly would render non-matching <div> heights for the Web document's columns, will occur when additional content is included in the right column. This situation might be represented with the following code, based on the previous examples:

    <html>

    <head>

    <title>THREE-COLUMN LAYOUT</title>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

    <style type="text/css">

    body {

         margin: 0px;

    }

    #leftcol {

         background: #ccf;

         float: left;

         width: 17%;

    }

    #content {

         width: 65%;

         float: left;

         background: #ccf;

    }

    #rightcol {

         float: right;

         width: 17%;

         background: #ccf;

    }

    </style>

    </head>

    <body>

    <div id="leftcol">

    Left Section<br /><br />

    Content goes here...<br />

    Content goes here...

    </div>

    <div id="rightcol">

    Right Section<br /><br />

    Content goes here...<br />

    Content goes here...<br />

    Content goes here...<br />

    Content goes here...<br />

    Content goes here...<br />

    Content goes here...

    </div>

    <div id="content">

    Content Section<br /><br />

    Content goes here...

    </div>

    </body>

    </html>  

    As we've done with the above samples, the visual output for this code is illustrated below:

    Consciously, we've taken a detailed look at the most common cases where the columns and content sections are displayed with different heights, according to the content that each element should contain.  Indeed, the problem must be quickly tackled, preventing its ugly side effects, when we're utilizing <div> elements for page layout. So, how can we make column heights match properly, showing a very polished and consistent look, independently from the content generated? Fortunately, the answer is quite simple: a JavaScript function that will set <div> heights to the same value, achieving the desired matching height. Now, let's take a look at the JavaScript code.

    More Web Standards Articles
    More By Alejandro Gervasio


       ·  I think that the exposed technique is easy to understand and effective to maintain...
       · The idea of javascript is good, but it's just working with IE only. I didn't play...
       ·  Hello friend, The solution works in IE, Netscape 6+ and Mozilla. So, It might...
       · I have only been able to get the script working in IE 6. Been having a mess about...
       · I don't understand why a web developer would do this. If a web surfer has...
       · Hello again,The script works fine in Firefox and Netscape 6+. I've tried it, and...
       · Yes, what you're pointing is correct. It's another possibility to achieve the same...
       · Modern browsers, like FireFox 1.0+ (and I'm guessing Netscape 6+) support CSS2.1,...
       ·  Well, I think that display: table-cell could be pretty easily to be implementedon...
       · Am I the only one that thinks it is crazy that we are running around coming up with...
       · Your opinion it truly worthy, since the use of tables is currently strong. The...
       · Actually it doesn't work in firefox. To get it working, you need to add 'px' to the...
       · The addition of the 'px' unit is recommendable. However, I've tested the script in...
       · I've tried this over and over and over again on my three column absolutely...
       · The script works great in IE, Firefox and Opera. However, I had a problem with the...
       · Thank you for your feedback here. In fact, the problem you mention shouldn't be that...
       · I was struggling with this - I could get it to work in html but not xhtml, and then...
       · I meant "http-equiv" meta tag - apologies.
       · Hello,Thank you for your introducing your comments on my article. Also, I'm glad...
       · No problem at all, friend. I understood what you meant.Thank you.
       · The script does exactly what it is supposed to do (thanks by the way for the...
       · Thank you for your detailed feedkack on my article, and particularly about your...
       · This seems unnecessarily complicated. This effect (as mentioned before) can be...
       · Hello Martin,Thank you for commenting in this article. Although the script is...
       · Alejandro, any way to have this great JS code working also on HXTML? Please let me...
       · First off, thank you for commenting here. Now, concerning your question, if you're...
       · I've looked at about 50 tutes for a pure CSS solution to this problem as I agree...
       · First of all, thank you for commenting here. Now, concerning the DIVS issue, if...
     

    WEB STANDARDS ARTICLES

    - Completing a Configuration for Chrome and a ...
    - Getting Connected with Firefox and Chrome
    - Configuring Servers and Databases with Chrome
    - Configuring Firefox for Chrome and a Server
    - Designing the Elements of a Web Page
    - Matching div heights with CSS and JavaScript
    - Forms
    - Get Down With Markup
    - If I Said You Had a Beautiful Body...
    - Web Standards in Dreamweaver Part 3
    - Web Standards in Dreamweaver, Part 2
    - Web Forms
    - Making Lists Using XHTML
    - Web Standards in Dreamweaver, Part 1







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway