13

The div#inner1 and div#inner2 are inside the div#outer, but still the height of div#outer shows as 0px with height:auto.

How do I get the height of the child elements for the outer div?

This is my code:

#outer {
  width: 300px;
  height: auto;
  background: #ccc;
}

#inner1 {
  float: left;
  width: 100px;
  height: 100px;
  background: #f00;
}

#inner2 {
  float: left;
  width: 100px;
  height: 100px;
  background: #0f0;
}
<div id="outer">
  <div id="inner1"></div>
  <div id="inner2"></div>
</div>
Marvin
  • 853
  • 2
  • 14
  • 38

2 Answers2

38

Add overflow:auto to the div with id outer. This will solve your problem.

Demo

Sreeja Das
  • 1,234
  • 12
  • 12
9

Float the outer div. that will cover your all height, whatever the inner divs holding heights. But if you will provide your inner div float property. then i will suggest you to use the hack clearfix..

    /* Assuming this HTML structure:

    <div class="clear">
        <div class="floated"></div>
        <div class="floated"></div>
        <div class="floated"></div>
    </div>
*/

.clear:before, .clear:after {
    content: "\0020";
    display: block;
    height: 0;
    overflow: hidden;
}

.clear:after {
    clear: both;
}

try this it will sure work