Web Standards
  Home arrow Web Standards arrow Page 2 - 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  
Actuate Whitepapers 
Moblin 
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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Matching div heights with CSS and JavaScript - The kingdom of non-matching heights


    (Page 2 of 4 )

    Say we've set up a generic three-column layout for Web pages, which suits our needs for displaying some dynamic content. We might start writing the code for doing that, defining the basic CSS styles and markup, respectively:

    <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: 0;

    }

    #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 />

    Content goes here...

    </div>

    <div id="rightcol">

    Right Section<br />

    Content goes here...

    </div>

    <div id="content">

    Content Section<br />

    Content goes here...

    </div>

    </body>

    </html>

    As we can see in the above example, a simple three-column layout has been defined using the "float" property to properly position each container <div> element in the document. Following the document structure, the "leftcol" <div> element is floated to the left side, the "rightcol" <div> is similarly floated to the right side, and finally, in a similar fashion, the "content" <div> has been floated to the left, sticking to the "leftcol" <div> element, being displayed nearly the center of the screen. Also, widths for each element have been defined by percentages. It's simple enough and effective to use in liquid designs.

    Although we've used floating boxes to achieve the general page layout, other techniques are perfectly valid, such as using absolute positioning and margin specifications. At this point, we have a good three-column design in which each column has the same height. Of course, this case is not a real representation of most Web pages out there. What if we start adding more content to, for example, the left positioned column and a little more to the content <div>? We might see something similar to the code shown below, derived from the initial example:

    <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...

    </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...<br />

    Content goes here...<br />

    Content goes here...<br />

    Content goes here...

    </div>

    </body>

    </html>

    This spoils the party! As we can appreciate, heights for each <div> element won't be the same and columns will be unequally displayed. We might end up with a visual output like this:

    Certainly, this is not what we want for our rendered Web pages, right? Those <div> elements are causing big headaches, and making our pages look pretty unprofessional. What's more, other possible variations to the example might be feasibly implemented. Let's see them in turn, in order to illustrate each of them.

    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