23

How do I make a parent/container DIV grow as more children DIV's are added.

<div id="container">
     <div id="child" style="height:100px;">
     </div>
     <div id="child2" style="height:100px;">
     </div>
</div>
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
funk-shun
  • 4,331
  • 11
  • 32
  • 41

6 Answers6

28

The only reason why your parent div would not grow with its content is if it's content is absolute positioned or is using floats, in the former case there is nothing you can do short of resizing it with javascript, in the latter you can put the following code at the end of your floating elements:

<br style="clear: both">

So say both the child elements in your example have a float, the code would look like this

<div id="container">
     <div id="child" style="height:100px;">
           ** CONTENT GOES HERE **
           <br style="clear: both">
     </div>
     <div id="child2" style="height:100px;">
           ** CONTENT GOES HERE **
           <br style="clear: both">
     </div>
</div>

You can use any node, as long as you use "clear: both" on it (So <div style="clear: both"></div> would work too).

Naatan
  • 3,424
  • 4
  • 32
  • 51
  • 2
    Instead of using `
    `, you can add `overflow: hidden` to the `#container` style.
    – BoltClock Jan 15 '11 at 01:49
  • @BoltClock that would just hide any content overflowing the height of the parent.. unless you are referring to a hack I'm unaware of, in which case; please tell us more :) – Naatan Jan 15 '11 at 02:49
  • 3
    Adding `overflow:hidden` works because many DOM elements completely ignore height settings unless they are explicitly instructed to do so. Using `overflow:hidden` will only work on children that are floating. – Michael Irigoyen Jan 15 '11 at 03:38
  • @Michael Irigoyen Are you aware as to the browser compatability of this handy little tool? Is it implemented by all browsers and IE? – dudewad Oct 22 '12 at 16:33
22

If the content inside your <div> is floated, it won't expand. You can easily fix this by placing the overflow:hidden CSS style to the parent <div>. Note, this will not work if the children are all positioned absolutely. When you absolutely position an element, you're taking it out of the document flow. As far as positioning is concerned, that element is no longer a "child", even though semantically it still is.

<div style="overflow:hidden">
  <div style="float:left">
    <!--Content-->
  </div>
  <div style="float:left">
    <!--Content-->
  </div>
</div>
Michael Irigoyen
  • 22,513
  • 17
  • 89
  • 131
2

Using display:table in the parent div makes it grow even if its contents are floating elements. It makes the div behave like a table, basically.

Take a look to the display properties: http://www.w3.org/wiki/CSS/Properties/display

Jorge
  • 831
  • 13
  • 18
1

display: unset; or display: inline; works on the parent element.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
0

Usually I create a fix class:

.clearfix{clear:both;}

Then, you can always use that when you need to.

<div class='wrapperContainer'>
    <div class="classFloated">
         Content
    </div>
    <div class="clearfix"></div>
    <div class="classFloated">
         Content
    </div>
    <div class="clearfix"></div>
</div>
kittykittybangbang
  • 2,380
  • 4
  • 16
  • 27
thelimarenan
  • 183
  • 9
0

Adding display:flex to the parent div may work.

For example, I wanted an unknown number of circles in a row, centered within an absolutely positioned div that expands as more are added in.

#grandparent{

  width:300px;
  height:20px;
  position:absolute;
  right:30;
  bottom:30;

}

#parent{

  transform: translateX(-50%);
  left: 50%;
  position: absolute;
  display: flex;

}


.children{

   background-color: white;
   border-radius: 50%;
   width: 16px;
   height: 16px;
   margin-left: 3px;
   margin-right: 3px;

}

See https://codepen.io/anon/pen/zXwRza

JohnG
  • 486
  • 3
  • 22