CSS pseudo class tip

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:

#id:after {
content:".";
clear: both;
height: 0;
visibility: hidden;
display: block;
}

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.

#id {
height:1%
}

Thank you for your comment. It has been submitted for approval.

Comment Again?

Leave a comment

Please enter your name.
Sorry, this email address is not valid.
Please enter your comment.

Course basket x

Course

Price per place

Add another courseCheckout

Pay now by credit card or later by invoice (invoice payments only possible if all courses are 3+ weeks in advance)