css background color with floating elements
I use a empty clear div at the end of the content container
added CSS:
.clear {
clear: both;
}
HTML:
<div class="content">
<div class="left">
<p>some content</p>
</div>
<div class="right">
<p>some content</p>
</div>
<div class="clear"></div>
</div>
When the children elements are floated, they are taken out of the flow of the document. In doing so, the parent no longer has defined dimensions, as the children aren't technically occupying space. Thus, the parent element collapses upon itself. The same thing occurs when absolutely positioning the children elements too.
In this instance, we can fix it by adding overflow:hidden
to the parent element, thus forcing it to contain the children elements. Alternatively overflow:auto
works just as well. Some may suggest it is even better than overflow:hidden
as you will be able to tell if any calculations are off.
jsFiddle example
.content {
overflow:hidden;
}
Now the parent element is no longer collapsed and the red background is visible.
You could alternatively use a clearfix if you are looking for support in older browsers, but I don't recommend doing so.