tags:

views:

33

answers:

2

Hello,

When I try to run the following:

​<div id="container">
     //This is a 200x200 image        
     <img src="http://dummyimage.com/200x200/CCC/000" />
</div>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

with CSS:

​#container {
    background:#000;
}

​I get a DIV with a black background container like I want.

However, when I add the following to the CSS:

#container img {
   float:left;
}

It seems like the container does not detect the image inside it and its height is set to minimum (can be seen here: http://jsfiddle.net/wc4GJ/ ).

How come floating the image to the left mess up the container DIV's height?

Thanks,

Joel

A: 

You need to add a clearing div after the img:

​<div id="container">
     //This is a 200x200 image        
     <img src="http://dummyimage.com/200x200/CCC/000" />
     <div class="clearing"></div>
</div>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

And in CSS:

.clearing { clear: both; }
reko_t
What is the reason for this? Shouldn't the container DIV recognize an image inside of it, even though it is floated to the left?
Joel
+1  A: 

Add overflow:auto; to #container

(Explanations below)

MatTheCat
Why should adding an image (with a fixed width and height) be considered as an overflow for the DIV?
Joel
Because floating takes the element out from the flow, which means that the parent element no longer has any content in it that determines its height. Adding overflow: hidden/auto; to the parent ensures that it wraps its contents properly.
PatrikAkerstrand
I agree with you ^^
MatTheCat