Skip to content

Clearing CSS Floats Without Markup

This has done the rounds loads of times before – but clearing css floats is something I need to do all the time, and since I have to look up the code on every project I thought I would add it here so that I have a reminder.

This method uses css pseudo elements to do the clearing without adding random elements that contain ‘clear:both;’ which in turn means that the html is kept nice and clean and the css doesn’t interfere with anything else.

.clear:after{content:" ";display:block;visibility:hidden;clear:both;}
.clear:before{content:" ";display:block;visibility:hidden;}

Since I also use it with LESS here’s how I do that:

.clear {
	&:after {
		content:" ";
	&:before {
		content:" ";

To use this just add the clear class to the element you want to clear.

<div class="clear">
<div class="float">Floaty thing here</div>
<div class="float">Floaty thing here</div>


Web Design

Ben View All

Ben is a lifelong Nintendo fan who also likes to build websites, and develop games. He also buys way too much Lego.

4 thoughts on “Clearing CSS Floats Without Markup Leave a comment

  1. Pingback: Clearing CSS Floats Without Markup
  2. Ben, this looks very cool and I’m all in favor of using less code and saving time. Can you show a brief example of what the html would look like. Do you add .clear to a content element?

    Now you can see I don’t understand…


    • Hi Robert – yeah, just add clear as the css class for the container of the floated items. I’ve added a small sample to the code above 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.