Say goodbye to the age-old clearfix hack and hello to the new and improved clearfix method..

The clearfix hack, or “easy-clearing” hack, is a useful method of clearing floats. I have written about the original method and even suggested a few improvements. The original clearfix hack works great, but the browsers that it targets are either obsolete or well on their way. Specifically, Internet Explorer 5 for Mac is now history, so there is no reason to bother with it when using the clearfix method of clearing floats.

Yes it’s ugly, but it works very well, enabling designers to clear floats without hiding overflow and setting a width or floating (nearly) everything to get the job done. The logic behind this hack goes something like this:

Target compliant browsers with the first declaration block (if all browsers were standards-compliant, this would be the only thing needed) and create a hidden clearing block after the content of the target element.

The second declaration applies an inline-table display property, exclusively for the benefit of IE/Mac.

At this point, we use the comment-backslash hack to hide the remainder of the rules from IE/Mac. This enables us to do the following:

Apply a 1% height only to IE6 to trigger hasLayout (which is required for the hack to work)

Re-apply display:block to everything exceptIE/Mac

The last line is a comment that serves to close the hack for IE/Mac

As you can see, that’s a lot of fuss over a browser that has been dead for at least the last three or four years. Nobody uses IE/Mac anymore, so it is time to drop it from the clearfix hack. The result is a much cleaner and more efficient slice of CSS:

Stripping out that IE/Mac cruft cleans things up real nice. Notice that we have further improved the clearfix hack by adding support for IE7. Neither IE6 nor IE7 support the :after pseudo-class used in the first declaration, so we need an alternate method of applying the clearfix. Fortunately, applying zoom:1 to either browser triggers IE’s proprietary hasLayout mechanism, which works just fine to clear the float. For expediency’s sake, we accomplish this with a couple of valid browser-specific selectors, but you should be advised that conditionalcomments are the recommended way to go.

Fortunately, IE8 supports the :after pseudo-class, so this new clearfix method will only become more simplified as IE6 and, eventually, IE7 finally die off.

Bottom line: The new clearfix method applies clearing rules to standards-compliant browsers using the :after pseudo-class. For IE6 and IE7, the new clearfix method triggers hasLayout with some proprietary CSS. Thus, the New Clearfix method effectively clears floats in all currently used browsers without using any hacks.

@Thierry, why did you drop the ‘font-size:0’ on your ‘improved’ clearfix in #133? When you take it out, you can get a 12px margin at the bottom of the page if you’re using the clearfix on a full-page ‘wrapper’ element. This makes sense as you’re adding a ‘.’ after the wrapper. Put ‘font-size:0’ in and the margin goes away.

@josh, I get the collapsing margins issue with Rofl_Waffler’s method so I think Thierry’s improved solution in #133 (with font-size:0 added) is the soundest approach.

BTW, I’m using .clearfix {min-height: 0;} in my stylesheet to make it compatible with IE7 so I only need to maintain an IE6-specific stylesheet (inserted with a conditional comment). For me, this strikes the appropriate balance between minimizing hacks/conditional comments and maintaining valid CSS.

This is a great article Jeff! As much as I have used “overflow hidden” in the past, 50% of the time I end up removing that later during development because of some tool-tip or image “nudged” relatively outside of its limits.

I have also learned to not really care too much about the CSS validating anymore. I try best I can of course, but in the end, why? Is what you have in place is needed to make the site work? Sure, it may matter to almost everyone reading this blog, however, explaining to a client that their CSS file validates to W3C’s regulations is about as useful as a plumber explaining to a housewife who just returned from the beauty parlor how he had to route a certain drain through the floor joists.

Hello,
I am somewhat restarter in webdesign after taking a relatively long break.
Anyways, I just can’t seem to use the clearfix with 960 css framework.
Would you guys look et my simple example and let me know what I am doing wrong?

It looks bad in both osx firefox and safari. When I add a clearing div it look sfine, but I would like to avoid using it.

Projects

About the site

Perishable Press is the work of Jeff Starr, professional developer, designer, author, and publisher with over 10 years of experience.
Check out some of Jeff's books and projects, follow on Twitter, or learn more »

Fun fact: Perishable Press has been online since 2005, and features over 800 articles and more than 11,000 comments. More stats »