Code Snippet
Force Element To Self-Clear its Children
A.K.A The "Clearfix" hack.
.clearfix:after {
visibility: hidden;
display: block;
font-size: 0;
content: " ";
clear: both;
height: 0;
}
.clearfix { display: inline-block; }
/* start commented backslash hack \*/
* html .clearfix { height: 1%; }
.clearfix { display: block; }
/* close commented backslash hack */
Just apply a class="clearfix" to the parent element. This is an improved version of the original, and documented here.
Updated by Jeff Starr to be cleaner, based on the face that nobody uses IE for Mac, which is what the backslash hack was all about.
.clearfix:after {
visibility: hidden;
display: block;
font-size: 0;
content: " ";
clear: both;
height: 0;
}
* html .clearfix { zoom: 1; } /* IE6 */
*:first-child+html .clearfix { zoom: 1; } /* IE7 */
Updated again... "group" class name is nicer and more semantic (via Dan Cederholm). Content property doesn't even need the space, can be empty string (via Nicolas Gallagher). Without any text, font-size is un-needed (Chris Coyier).
.group:after {
visibility: hidden;
display: block;
content: "";
clear: both;
height: 0;
}
* html .group { zoom: 1; } /* IE6 */
*:first-child+html .group { zoom: 1; } /* IE7 */
Of course if you drop IE 6 or 7 support, remove the associated lines.
Update May 18, 2011: Nicolas Gallagher again with the "micro" clearfix. Also see this additional stuff.
.group:before,
.group:after {
content:"";
display:table;
}
.group:after {
clear:both;
}
.group {
zoom:1; /* For IE 6/7 (trigger hasLayout) */
}
just so i’m clear: would this be for child elements that are floated, and thus the parent element disappears?
i’ve found that applying overflow: hidden; to the parent element takes care of this. the parent element then fills in behind the child elements.
Yes that is right, but overflow: hidden; also hides the overflow, which may be undesirable behavior in some circumstances.
overflow: auto;works well as long as you are able to keep control of your margins and padding (else you see scrollbars). That’s ironic. :)
I still use this religiously. I rarely fails me and has worked cross browser extremely well.
Only time I get a hiccup with it is when I had a fluid container that was floated left or right. I would get a large gap underneath one of the elements when clearfix was in place.
Rocking ! I ve always had a confusion on judging whether to use the markup based clear or this one. This is cool … but still have to test it in a pretty bigger application.
Im concerned about the .clearfix{display:inline-block;}, block. Im not sure if this would be 100% perfect in a application of a high volume of markups / complex layouts. Correct me if wrong.
The edit and delete functionality of this comment section just rocks :). Im loving the counter … great user experience and awesome interaction design. Keep it going chris !
Chris – you, like Mary Poppins, are practically perfect in every way, except for it’s and its. So in order to help you attain a higher level of perfection, I am going to give you a free lesson!!!! (are you excited yet?)
Possessive pronouns: his, hers, theirs, ours, mine, yours, whose, and its
Notice no apostrophes on any of those – just think “his, hers, its” to help you remember that.
Contraction: It is shortens to it’s, he is => he’s, she is => she’s, I am => I’m, you are => you’re, they are => they’re, we are => we’re, who is => who’s
For pronouns, the only apostrophes are for contractions as shown above. Regular nouns do use apostrophes to show possession but not pronouns.
So your title for this post should be “Force Element To Self-Clear its Children” (not it’s). I see this mistake a lot in your posts, but this one stands out even more since it’s in the title.
Whew! That out of the way (and seeing those errors really does get in the way of content), you are awesome and I have learned SO much from you – I hope my little grammar lesson actually helps!
And, on topic, thank you for THIS helpful snippet!
You’re the worst person I’ve ever met.
Am I really the only one who things there should be a standardized non-hack way of doing this added to CSS3?
Really? I’m the only one? (sad, slow facepalm)
overflow: evelop;
overflow: contain-floats;
float-collapse: none;
SOMETHING, ANYTHING.
You aren’t the only one. I like the third one the best.
Aaaaagreed.
This can be a dangerous technique to use unless authors know exactly what this styling means in term of construct.
See everything you know about clearfix is wrong
The best way to use a clearfix can be found at best clearfix ever. It doesn’t use class names to fix the problem but an automatic solution that should be applied to all block level elements except for the p and footer elements.
Wow. This is hands down one of the best designed websites I’ve seen. Everything is beautiful, loads quick, and has just the right amount of interactivity and minimalism.
About this post, I’m new to CSS, and this problem had me puzzled for a while, till I learned that a thing like a clearfix hack existed from a TutorialZine template. I can’t believe that so many people, for so many years, have pointed out this problem, and it still hasn’t been fixed. Semantic tags like nav and section mean little when more basic problems such as this are not addressed. I find the whole designing by CSS technique very tedious, I can’t blame developers for using tables in the past.
Clearfix completly doesn’t work on IE7 for me, it fixed IE6 issues, but not IE7, any ideas?
I’ve just found this site
http://wiki.fluidproject.org/display/fluid/Testing+clear-fix+solutions
where all the clearfixes are tested, it seems like on IE7 none of them works :( bizzare. Has any one got any solution to this?
May 18th Update:
Isn’t “display:table” usually discouraged? or is it ok here because it is applying it to the “content:”" “?
Html tables are for tabular data and too use them for layout is semantically wrong. CSS is not about semantics but presentation.
So presenting an element as display:table or display:table-cell or whatever is just dandy!
I am pretty sure that :before and :after pseudo-selectors don’t work in IE6 and IE7, so this solution isn’t useful regarding cross-browser functionality…
Any further comments on the matter?
It does work. Try things before crying foul. It’s very easy to throw together tests in http://jsfiddle.net/. All the techniques have multiple selectors, the ones without pseudo elements are for IE 6/7.
Sorry, I was talking about the last one. The only line without pseudo element is that one triggering hasLayout for IE. I’m surely missing something important or is this one working as clear:both for IE…
I’ve been using this for years now, and it hasn’t failed me once. But it amazes me how people still have difficulties grasping this concept when they are UI developers. I had to teach this to a programmer of 15+ years recently, and still they are cloudy with the understanding of it.
One of the best examples I show them is with background colors for parent, and separate child containers. That usually clears things up almost immediately with the difference between using clear fix and not having it applied.
I probably *should* know this, but what exactly IS a clearfix and what is it’s purpose? I’ve searched all over for an explanation, but I couldn’t find anything on what they are. Thanks for your help! :)
Hi! Due to last update: Parent elements with ‘display:table’ don’t mess out their absolute positioned child elements?
Very nice how you display the uptates of the fix. This way it’s really easy to see the development of it! Thanks!
Clearest article on the Clearfix hack yet.
Outstanding work on this site’s graphics.
Bravo!
Totally agree~~
Overflow is the missing ‘chink’ [thanks, Patrick, you old Dog] …
CSS3 parenting [CSScube] semantic ever since IE4-6, Opera.fun is simply
overflow: auto
In context, it’s [sp. for ms. Lady things]
.cleargroup, parentname.cleargroup {
display: block;
font-size: 0;
overflow: auto;
}
And then, since we’re taking this to the nth degree
Font-size is the display element spacer, not height. Attribute font-size is the logical display anchor, and it’s value will be ignored (selector not required in new browsers? oh dear, anchor!). Echoing display:block in the attribute is critical to manage fluid float overflow inside cleargroup, with an absolute position. Relative positioning handles any CSS widgets you may throw into the cleargroup zone. For those still hobbled with old IE and Firefox foibles, the <operator cache helps; operator is ignored by others. Similarly, visibillity (loosen your collar, scriptites) is managed by display:block being attached to parent and child, securing attribute operator overflow in display as required.
This runs especially well where build includes side-by-side vertical columns that wrap around a floated element or block. Parentname allows inclusion of an opening blockquote salvo, or whatever. A closing salvo, however, would slightly screw your float wrap, inline or otherwise. Because there’s no overflow left over. It’s one of those characteristic CSS logical die-out things, Ugh.
Now, the design and development question is inside of parent or outside of predecessor. On page, in new browsers it seems a non-challant either-or div. Which leaves the style cue open for scripting and devicing. For the pure at heart, there’s also cleargroup container popup tutorials. In that scenario, overflow balances clip margins, etc.
ergo: hope those awesome “echo” and “zone” terms in the vicinity of any CSS attribute don’t upset designers out there. The hermeneutic envelope that secures CSS discourse is not quite secure, is it? Net5 and CSS4 are on the move. … overflow:auto
I love this approach. The group class has now completely replaced clearfix for me. You’ll never see a clearfix class name in my work again. Awesome as always, sir!
I love your site, but it would be simpler if you offer only one working clearfix snippet instead of two or three.
Thanks Chris for your clearfix-snippet! Still working great. I was using the micro-fix from Nicolas that comes along with Pauls Boilerplate, but in the newest Firefox it broke my layout and behaved unexpected. Yours is still working, niceOne on this :D
Hi Chris,
I think there is currently a bug in Firefox that if you don’t have anything in “content” then form elements get double margin. I’m positive that even a space doesn’t work and FF needs at least a dot. Haven’t noticed such behavior with other elements.
DigWP
A book and blog co-authored by Jeff Starr and myself about the World's most popular publishing platform.
Quotes on Design
Design, like Art, can be an elusive word to define and an awfully fun thing to have opinions about.
HTML-Ipsum
One-click copy to clipboard access to Lorem Ipsum text that comes wrapped in a variety of HTML.
Bookshelf
Hey Chris, what books do you recommend? These, young fertile mind, these.