Since your container contains floating element, its height is equal to zero, unless you will set the height
or min-height
via CSS, that is not an optimal solution. Below are explained all the known solutions to clear floating elements:
The safe and traditional manner: float (all!) container(s)
In this case, you make all the containers of the floating element likewise floating itself, and setting the width,
like this:
header {
background-color: blue;
float:left;
}
#main-header nav {
float: right;
background:red;
width:50%
}
Live example
The pitfall is that it affects the layout, you have to handle it applying the float to all parents, endless waterfall-like CSS ensues...
The backward-looking way: clear with non-semantic element
You can clear the float inserting a void element before the closing tag of the parent, like this:
[CSS]
.clear-all {clear:both;}
[HTML]
<header>
.....
<span class="clear-all"></span>
</header>
The pitfall is that the span
element, or whatever, is not semantic.
The quick and dirty way: Overflow: hidden
Setting overflow: hidden
to the parent/container, it makes it to define its boundaries.
The pitfall is that if your layoud must have element the overlap the container, you can't afford it.
The modern way: clearfix:after
By reading this Css-tricks article, you can learn that:
The Easy Clearing Method uses a clever CSS pseudo selector (:after) to clear floats. Rather than setting the overflow on the parent, you apply an additional class like "clearfix" to it. Then apply this CSS:
.clearfix:after {
content: ".";
visibility: hidden;
display: block;
height: 0;
clear: both;
}
That is in my opinion the best, concise, restrained way to clear floats.