Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stretch nested div to height of container div when container has height:auto?

I have a container div and two nested divs within. I do not have control over the content of either of these nested divs.

What I essentially need is to make the two nested divs always have the same height. I figured this can be achieved by giving the container div height:auto so that it would stretch its own height to the tallest of the two nested divs (each of which stretches to fit its own content), and then the other nested div would stretch to 100% of the container's height.

Here's what I tried:

Style:

#container{
  background-color:#FF0;
  overflow:auto;
}

#left{
  float:left;
  height:100%;
  width:20%;
  background-color:#F00;
}

#right{
  height:100%;
  width:60%;
  background-color:#0F3;
}

HTML:

<div id="container">

 <div id="left">
  <p>Content</p>
  <p>Content</p>
  <p>Content</p>
 </div>

 <div id="right">
  <p>Content</p>
  <p>Content</p>
 </div>

</div>

But this does not work. The container stretches to fit the longest div ("left" in this case) but the shorter nested div ("right") does not stretch itself.

Note, however, that this DOES work if I give the container a specific height:

#container{
  background-color:#FF0;
  overflow:auto;
  height:300px;
}

Is there any way I can get this to work without resorting to tables?

like image 309
Rahul Avatar asked Mar 06 '10 07:03

Rahul


People also ask

How auto adjust the div height according to content?

Syntax: height: length|percentage|auto|initial|inherit; Property Values: height: auto; It is used to set height property to its default value.

How do I stretch a div to fit a container?

Method 2: We can make the display attribute of the child container to table-row and display attribute of parent container to table, that will take all the height available from the parent div element. To cover all the width, we can make the width of parent div to 100%.

How do I make a div extend beyond container?

- First set the width of the extended-content-container div to 500%. This will make the div you are trying to extend 5 time wider than the center column it lives inside. This can be adjusted up or down depending on the size of the center column you are working with.


2 Answers

The two most common ways to do this are Faux Columns using images or setting the heights to equal values with JavaScript. I did a screencast to demonstrate the second technique, Equalizing Column Heights with jQuery. The technique could be easily adapted for other libraries or for plain JavaScript.

like image 173
Jimmy Avatar answered Oct 12 '22 06:10

Jimmy


The Important Note is that Percent Value for Height Deprecated a while ago. So you have to use another pattern.

Most of the time (specially in your case) the panels height are sets automatically. So it is better to increase the height with a little javascript

<script>
    function IncreaseHeight()
    {
        var first = Document.getElementById("Right").style.height;
        var second = Document.getElementById("Left").style.height;

        if(first < second)
        Document.getElementById("Right").style.height = Document.getElementById("Left").style.height;
        else
        Document.getElementById("Left").style.height = Document.getElementById("Right").style.height;
    }
</script>

note that change the place of Right and Left base onyour case.

like image 22
Nasser Hadjloo Avatar answered Oct 12 '22 05:10

Nasser Hadjloo