Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a div expand vertically to wrap the content within it?

Tags:

css

I have a div which wraps a number of images that are generated dynamically. I don't know how high the list of images is. My problem is the div that contains the dynamically generated images doesn't behave like it is housing any content - I want it to extend to the height of the list of images. Each image is itself wrapped in a div.

This is the wrapper div:

.block { padding:10px; margin-top:10px; height:auto; background-color:#f9f9f9; } 

This is the markup dynamically generated for (one of) the images:

<div class="block"> <div style="float: left; padding: 2px 2px 2px 2px;"><IMG SRC="45.jpg" BORDER="0"/></div> ..... 

How do I get the block div to extend down with the images?

Thanks

like image 409
MalcomTucker Avatar asked Apr 10 '10 19:04

MalcomTucker


People also ask

How do I make my div grow vertically?

Insert a div with {height:100%; overflow-y:auto} into the vertical filler to if the containers height shouldn't expand beyond its preset height. Behold the power of display:table!

How do you wrap a div in HTML?

If you've faced the situation when you need to wrap words in a <div>, you can use the white-space property with the "pre-wrap" value to preserve whitespace by the browser and wrap the text when necessary and on line breaks.

How do you expand a div in HTML?

You set that div with a height that matches the div on the left, then add a child div that is scrollable and expands to 100%. See this fiddle. You can try to fine tune the effect you want to achieve by replacing height by either max-height or min-height . Hope it helps.


1 Answers

The problem you're observing happens when you float an element, which takes it out of the normal flow of the elements (by normal flow I mean the way the elements would appear with no styling). When you float an element, the other elements still in the normal flow will simply ignore it and do not make room for it, which is why your block div does not extend the full height of your image.

There are a few different solutions:

1) Add the rule overflow: hidden; to the block class:

.block { overflow: hidden; padding:10px; margin-top:10px; height:auto; background-color:#f9f9f9; } 

2) Add a element after your image that clears the floating:

<div class="block">     <div style="float: left; padding: 2px 2px 2px 2px;"><IMG SRC="images/login1.png" BORDER="0"/></div>     <div style="clear: both;"></div> </div> 

Both will work, but I prefer the first solution.

like image 187
wsanville Avatar answered Sep 30 '22 16:09

wsanville