Having contributed to a feature in last month’s Web Designer Magazine on great CSS secrets, we thought that the blog would be a great place to share the tip we came up with for clearing a float container without extra HTML mark up:
If a floated element is nested within a box that has a border or a background colour applied to it, the floated element will not force its container to stretch all the way down (mainly in standards compliant browsers).
To force the container box to enclose the floated div(s), it needs a ‘cleared’ element added after the last floated div. Rather than adding extra HTML mark up such as a clearing div or paragraph we can use CSS.
Using the pseudo-class
:after it’s possible to add content at the end of an element with the followingCSS commands:
Sometimes the container will also collapse in Internet Explorer. Unfortunately IE still doesn’t support the
:after pseudo-class yet, so a simple declaration of
height: 1% to the floated containing div will stop it from collapsing.