I think everyone in the writing CSS should be to clear the use of floating in the deep experience, today we also discuss the evolution of the Clearfix of the History bar.
Clearfix Clear Float
First of all, many years ago we used to clear the float is like this.
. clear{clear:both;line-height:0;}
Now it may be possible to see such code on many old sites, which is quite violent and effective in solving floating problems. But this usage has a fatal injury, that is, each time you clear the float, you need to add an empty tag to use.
This practice if the page complex layout to often clear floating time will produce a lot of empty tags, added page useless tags, not conducive to page optimization. But I found that the big Web site is still using this clear floating method. Interested students can be on their homepage to search their. Blank0 this style name.
So there are a lot of great gods to study the Clearfix clear floating method, directly solve the above defects, do not need to add empty tags, directly in the floating layer with this style can be, this is what we will discuss today Clearfix evolutionary history.
Origin
. clearfix:after {
Visibility:hidden;
Display:block;
font-size:0;
Content: "";
Clear:both;
height:0;
}
. clearfix {display:inline-table;}
* HTML. Clearfix {height:1%;} Hides from Ie-mac
. clearfix {Display:block;} End Hide from Ie-mac
Explain the above code:
The first declaration block is applied to most standards-compliant browsers to create an empty block of invisible content to clear the float for the target element.
The second applies inline-table display properties for Clearfix, only for Ie/mac. Use * To hide some rules from Ie/mac:
The height:1% is used to trigger the haslayout under IE6.
Re-apply the block display property to IE outside of Ie/mac.
The last line is used to end the hack for Ie/mac. (Do you think it's a pit daddy, Mac and IE)
The Origin code may also be very early time, and then back to the Mac under the IE5 also developed to IE6, various browsers began to the web of this standard slowly on the line. So the following is the wording of the present.
. 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 */
Both IE6 and IE7 do not support: After this pseudo-class, the following two are required to trigger the haslayout of IE6/7 to clear the float. Fortunately, IE8 supports: after Pseudo-class. So only the hack for IE6/7 is needed.
Adding a div package with the Clearfix attribute to the outer layer of a float property element guarantees the height of the outer Div, which clears the question of "floating elements out of the flow of the document and the div that surrounds the picture and text does not occupy space".
Jeff Starr here uses two statements for IE6/7 to trigger the haslayout. I wonder why the author does not directly use the * to apply zoom:1 directly to IE6/7 or write it directly:
. clearfix:after {
Visibility:hidden;
Display:block;
font-size:0;
Content: "";
Clear:both;
height:0;
}
. Clearfix{*zoom:1;}
But for many students this level of optimization code is not enough to force, Clearfix developed to the present two Ultimate version.
Refactoring Clearfix Floating
There are several ways to make block formatting context:
The value of float is not none.
The value of the overflow is not visible.
The value of display is Table-cell, table-caption, any of the inline-block.
The value of position is not relative and static.
It is clear that float and position are not suitable for our needs. That can only be selected from the overflow or display.
Because it is applied. Clearfix and. Menu menus are highly likely to be multilevel, so overflow:hidden or Overflow:auto do not meet the requirements
(The drop-down menu is hidden or scrolled), so you can only start with display.
We can set the display value of the. Clearfix to Table-cell, Table-caption, any of the Inline-block
But Display:inline-block will produce extra blanks, so it's also ruled out.
The remaining only Table-cell, table-caption, in order to ensure compatibility can be used display:table to make. Clearfix form a block formatting Context
Because display:table will produce some anonymous boxes, one of these anonymous boxes (display value of Table-cell) forms the block formatting Context.
So our new. Clearfix will close the float of the inner element.
This was further improved by the following:
Ultimate Edition One:
. clearfix:after {
Content: "\200b";
Display:block;
height:0;
Clear:both;
}
. clearfix {*zoom:1;} /*ie/7/6*/
Explanation: Content: "\200b"; this parameter, the Unicode character character has a "0 width space", that is u+200b, instead of the original ".", you can reduce the amount of code. And no longer use Visibility:hidden.
Ultimate Edition II:
. clearfix:before,.clearfix:after{
Content: "";
display:table;
}
. Clearfix:after{clear:both;}
. clearfix{
*zoom:1;/*ie/7/6*/
}
The two ultimate version of the code are very concise, the ultimate version of one and two can be used, the above code has been tested, we hurriedly use it, if there is any problem please timely feedback with me, if you still stay in the old code of Clearfix the time to quickly update the code bar.
Clearfix Clear Floating Evolutionary history