tags:

views:

248

answers:

6

I have a container div#content, which contains three divs inside. Now, how could I make sure that three divs inside have the same height? Of course, I hope each div's height could be expanded according to its content. For example: here is what I tried

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title></title>
<style type="text/css">
   #content{background-color:#DDDDD;width:100%;overflow:auto;clear:both;height:100%;}
   #col1{background-color:yellow;width:10%;float:left;height:100%;}
   #col2{background-color:red;width:30%;float:left;height:100%;}
   #col3{background-color:#AAAAAA;width:10%;float:left;;height:100%;}
</style>
</head>
<body>
<div id="content">
 <div id="col1">
     <script language="javascript">
 for(i=0;i<1000;i++){
 document.write(i+"<br />");
 }
 </script>
 </div>
 <div id="col2">
      <script language="javascript">
 for(i=0;i<100;i++){
 document.write(i+"<br />");
 }
 </script>
 </div>
 <div id="col3">
       <script language="javascript">
 for(i=0;i<10;i++){
 document.write(i+"<br />");
 }
 </script>
 </div>

A: 

I may be wrong, but I think you either put the same values for the heights in the divs (either in percent or px) or you'll have to do some script (for example in JavaScript) that will check on the content height and set the other heights. I don't think it's doable with CSS. If I'm wrong I'd like to know the answer though:P

webdreamer
percentage height is not widely supported
pstanton
+1  A: 

First of all: if you have an equal attribute on different elements, please adhere to the DRY principle (Don't Repeat Yourself) and write it like so:

.content div{
    border:1px solid #404040
}

That way you'll only have to change it in one place.

Now about your question. For a dynamic height, I'd specify that the div's should have a height of 100%, so they fill all the vertical space. This doesn't work nicely cross-browser so look for a hack that does this. If you don't want the div's to fill up the content div, put another div inside the content div and put that around the 3 divs.

So:

<div id="content">
    <div class="innerContent">
        <div class="1">Lorem Ipsum</div>
        <div class="2">Lorem Ipsum</div>
        <div class="3">Lorem Ipsum</div>
    </div>
</div>
Niels Bom
as the example you made here, just add height:100% to .innerContent will make all 3 divs inside have the same height? I don't think so. Of course, in your example, 3 divs have the exactly same content. How about different contents with different heights?
WilliamLou
I know that height:100% does not work, a combination of one or more hacks and min-height:100% should. The div with the most content will dictate how high div.innerContent is and therefore the other two divs will adapt their height to be 100% and thus the same.
Niels Bom
could you be more specific about "combination of hacks"?
WilliamLou
Here's some more info on exactly how to do that. http://www.456bereastreet.com/archive/200406/equal_height_boxes_with_css_part_ii/and http://www.dave-woods.co.uk/?p=144
Niels Bom
+3  A: 

A very useful technique for creating divs of equal height is to emulate it with a technique called "Faux Columns". This was an idea first suggested by Dan Cederholm (You can read his original article here), and has since evolved. You can see a good tutorial here. If you need it in a liquid layout environment, you might want to read this article.

Basically, the idea builds on NOT trying to force the divs to be of equal height, but have a wrapper of the three divs with a background-image that simulates the background of the columns. This approach works consistently among all modern browsers (ie6 even counts as modern in this context). The negative part is that you'll need a background image that is at least as wide as the page is allowed to expand. i.e. X pixels wide and 1px high.

PatrikAkerstrand
Why is this downvoted? +1...
jeroen
+2  A: 
Carl Smotricz
I´m getting a bit tired of that same old misunderstanding. CSS DOES OFFER A SOLUTION, it's one mayor browser that just doesn´t know how to handle ccc correctly.
jeroen
If one major browser doesn't handle it, it's not a solution. I know that isn't CSS' fault, but it also doesn't solve the problem.
Carl Smotricz
Well, there are different ways to solve the problem: fake it, use css + javascript for some browsers, use javascript, use tables, etc. They're all valid solutions; I was commenting on your css remarks which are not correct, not your table solution, which works just fine.
jeroen
A: 

You could try it using css for modern browsers (display:table-cell, etc.), however, that will not work in IE6 and IE7.

If IE6 and IE7 are a requirement (I suppose it is...), you can include some javascript just for them using conditional statements and have that javascript set the height of all columns to the tallest. Not really pretty, but the percentages of IE6 and 7 should be going down fast anyway.

By the way, Machine's solution (faux columns) is another solution that works for a lot of designs.

jeroen
I promise this is the last comment I will make on this question, but I can't resist: It's really amazing to what lengths the no-tables crowd will go to avoid the most workable solution.
Carl Smotricz
Just offering alternatives that were not mentioned before, take your pick for a working solution...
jeroen