6

/* Clear Fix */

.clearfix:after {content: ".";display:block;height:0;clear:both;visibility:hidden;}
* html .clearfix {height:1%;}

or

.clearfix:after {content: ".";display:block;height:0;clear:both;visibility:hidden;}
* html .clearfix, *:first-child+html .clearfix {zoom:1;}

Which would work the best? I used first one by now and never had an issue.. Thanks.

Will Vousden
  • 32,488
  • 9
  • 84
  • 95
dzhi
  • 1,534
  • 2
  • 18
  • 29
  • 1
    i think you made a mistake on the second one > in my experience you have to put IE6 and IE7 hack on separate line statement or it won't work – Knu Jul 13 '10 at 08:30
  • could anyone confirm this one? – dzhi Jul 13 '10 at 16:29
  • why waste properties on hiding the dot you've added, when `content:""` creates element without creating a visible character and line!? – Kornel Jan 15 '11 at 17:51

3 Answers3

3

Most succinct technique is setting overflow:hidden for modern browsers:

overflow:hidden;
zoom:1;

If an element needs to flow out of the dimensions ( negative margins or positioning ) then clearfix:

#el:after { content:""; clear:both; display:block; visibility:hidden; }

For IE6 and below, you need to trigger hasLayout ( through a width, zoom:1, height, and other property/value combos ). Starting with IE7, overflow will clear the floats.

meder omuraliev
  • 183,342
  • 71
  • 393
  • 434
1

The latter seems to be fine because it also considers IE6 (zoom:1;).

Chris Frederick
  • 5,482
  • 3
  • 36
  • 44
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
0

This has always worked for me. Very similar to yours

.clearfix:after {
    content: "."; 
    display: block;
    height: 0; 
    font-size:0;
    clear: both; 
    visibility:hidden;
}
    .clearfix{display: inline-block;}
    * html .clearfix {height: 1%;}
    .clearfix {display:block;}
Marko
  • 71,361
  • 28
  • 124
  • 158