7

I am adding divs dynamically as shown in http://jsfiddle.net/Lijo/ZkLg6/5/.

The parent #mainHolder div is not increasing its width when child elements are added – as a result the children breaks the parent div. How can we overcome this by adjusting the parent div height?

enter image description here

jQuery

$('input').click(function()
{
 var existingDirectChildrenDivCount = $('#mainHolder > div').size();



 if( existingDirectChildrenDivCount % 3 == 0)
 {
      $('#mainHolder').append ("<div class='firstDiv'> A  </div>")
 }

 if( existingDirectChildrenDivCount % 3 == 1)
 {
      $('#mainHolder').append ("<div class='secondDiv'> B </div>")
 }

 if( existingDirectChildrenDivCount % 3 == 2)
 {
      $('#mainHolder').append ("<div class='thirdDiv'> C  </div>")
 }

}
);

HTML

<html>

   <input type="submit" value="Add" />
   <br/>   
   <div id="mainHolder">
   S    
  </div>

  <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.4.1.js"></script>

</html>

CSS

#mainHolder
{
    width: 400px;
    border-top: 3px solid orange;
    border-bottom: 3px solid red;
    border-left: 3px solid purple;
    border-right: 3px solid pink;
    height:auto;
}

.firstDiv
{
    float: left;
    display: inline;
    background-color: #f5B5f5;
    height:100px;
}

.secondDiv
{
    float: left;
    display: inline;
    background-color: #FF007F;
    height:100px;
}

.thirdDiv
{
    float: left;
    display: inline;
    background-color: Pink;
    height:100px;
}
LCJ
  • 22,196
  • 67
  • 260
  • 418

8 Answers8

20

Add overflow:auto

#mainHolder
{
    width: 400px;
    border-top: 3px solid orange;
    border-bottom: 3px solid red;
    border-left: 3px solid purple;
    border-right: 3px solid pink;
    height:auto; overflow:auto
}

Demo here http://jsfiddle.net/ZkLg6/11/

Sowmya
  • 26,684
  • 21
  • 96
  • 136
2

Try this: http://jsfiddle.net/ZkLg6/7/

The fix is to use a div that clears floated elements. I had to push your dynamic elements into a nested div inside mainHolder to ensure the clear div was always below them but it works well.

Michael Allen
  • 5,712
  • 3
  • 38
  • 63
1

Try to add overflow: auto; to the CSS of #mainHolder.

Sebass van Boxel
  • 2,514
  • 1
  • 22
  • 37
1

The solution is to add a at the end of your #mainHolder and insert elements before that (or just keep removing and re-adding it every time you add a new div. This is because you're using floats, alternatively if you can drop the float from the other divs everything should work as expected. The overflow: auto; solution is also good and seems simpler.

xception
  • 4,241
  • 1
  • 17
  • 27
0

Try something like this:

#mainHolder
{
    min-width: 400px;
    float:left;
    border-top: 3px solid orange;
    border-bottom: 3px solid red;
    border-left: 3px solid purple;
    border-right: 3px solid pink;
    height:20px;
}

The only problem here is letter "S",but you may put it inside some div. Like those colored. Here is updated JS fiddle.

Hm. But that works if you want to increase WIDTH, not HEIGHT. If you want to increase height - just add overflow:hidden; Plus there are some more changes in your css. Take a look at JSfiddle

Viktor S.
  • 12,736
  • 1
  • 27
  • 52
0

You have to clear the floating. You can do that inserting an element like br which has clear:both.

Here is a piece of code you can add in order to work:

$('#mainHolder').find("br").remove(); // remove already added br
$("<br>").css({clear : "both"}).appendTo($('#mainHolder')); // append a br after the last element.

I've updated your jsFiddle: http://jsfiddle.net/ZkLg6/13/

gabitzish
  • 9,535
  • 7
  • 44
  • 65
0

Check this http://jsfiddle.net/ZkLg6/19/
I used overflow:hidden

#mainHolder
{
width: 400px;
border-top: 3px solid orange;
border-bottom: 3px solid red;
border-left: 3px solid purple;
border-right: 3px solid pink;
height:auto; overflow:hidden;
}
Prasad Jadhav
  • 5,090
  • 16
  • 62
  • 80
-1

Set both overflow and height to auto, and now the parent div's offsetHeight will update dynamically!

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Hank
  • 1,658
  • 12
  • 13