When positioniseverything.net posted an article about clearfix technique, it didn't take much for web developers and designers to start adopting it. Lately, Dan Cederholm has been talking about renaming the class clearfix to group, which semantically speaking makes a lot more sense. So, let's go ahead and rename the class.
New Easy-Clearing
.group:after { content: "."; display: block; height: 0; clear: both; visibility: hidden; }
Using conditional comments, let's add the proper code to handle IE 6 and IE 7 (IE8 supports the :after pseudo-elements; no extra code is necessary).
New Easy-Clearing for IE
/* IE6 */ * html .group { height: 1%; } /*IE7 */ .group { min-height: 1px; }
I've been using the group class on my latest projects and I'd like to share a small tip, when clearing list items, with you.
Normally, we'd have the group class on every LI, like so:
Clearing List Items the Old Way
<ul class="comments"> <li class="group"> <img src="carla.jpg" alt="Carla's Picture" /> <p>Tiny and I miss you</p> </li> <li class="group"> <img src="steven.jpg" alt="Steven's Picture" /> <p>I need a favor! It will take you 5 seconds.</p> </li> <li class="group"> <img src="buzz.jpg" alt="Buzz's Picture" /> <p>I speak gibberish</p> </li> </ul>
Although the markup doesn't look very bad, we can make things better by introducing a new class (groupList) and adding it to the UL.
Clearing List Items the New Way
<ul class="comments groupList"> <li> <img src="carla.jpg" alt="Carla's Picture" /> <p>Tiny and I miss you</p> </li> <li> <img src="steven.jpg" alt="Steven's Picture" /> <p>I need a favor! It will take you 5 seconds.</p> </li> <li> <img src="buzz.jpg" alt="Buzz's Picture" /> <p>I speak gibberish</p> </li> </ul>
Phew, the markup looks much cleaner! Now that we've fixed it, let's add the .groupList > li selector to the existing .group styles.
Extending .group
.groupList > li, .group:after { content: "."; display: block; height: 0; clear: both; visibility: hidden; }
Wait! How about the beloved IE?
Extending .group IE
/* IE6 */ * html .groupList li, * html .group { height: 1%; } * html .groupList li li { height: auto; } /* Resets nested LIs */ /*IE7 */ .group > li, .group { min-height: 1px; }
Browser Support
- Firefox
- Safari
- Chrome
- Opera
- Internet Explore 7 & 8
- Internet Explore 6
Comments
leave a commentThe group list thing should just be added to all navigation lists practically. Maybe someday they’ll be some kind of a CSS Selector extension that we could combine with a fictitious ‘css attribute’ attribute selector.
So:
ul li[float='left'] { inherit-from: group; }
That means all floated list items should take groups properties. Maybe that’s too bizarre. They aren’t really doing much with the clearing/semantics problems in HTML5 are they?
- Tateman
One more thing Rodio ….
We have to give Dan Cederholm of SimpleBits his due on this one.
You rock Dan. I’m buying Hand Crafted CSS as soon as the damn bookstore gets it!
-Tateman
Steven,
I’m waiting for Hand Crafted CSS to hit the bookstore too. I know pre-order is available at Amazon.
I agree with you Dan rocks. Actually, Andy Clarke, Jeremy Keith and Dan Cederholm are way ahead of the game when comes to CSS and HTML.
I am confused. The link you have to the clearfix post starts off by saying:
The “newer” article is from 2005 and says: baaahh… just use ‘overflow:auto’ and all is well. Several comments confirm this working.
So, I scratch my head… huh?
..hmm blockquote didn’t really help show the quote in that last comment. Sorry people. It looks confusing now.
It’s a good solution but the “width:100%” has to be set, on complicated layouts it can be a problem. More info here
Sorry for the commenting problem earlier today. I’ll fix some known bugs tonight including the blockquote script block. Bare with me, my QA team sucks