原文地址如下:
[url=http://www.webtoolkit.info/css-clearfix.html]http://www.webtoolkit.info/css-clearfix.html[/url]
See also:
[url=http://www.codecto.com/2011/02/css-clearfix-float/]http://www.codecto.com/2011/02/css-clearfix-float/[/url]
[b]CSS clearfix[/b]
The problem happens when a floated element is within a container box, that element does not automatically force the container’s height adjust to the floated element. When an element is floated, its parent no longer contains it because the float is removed from the flow. You can use 2 methods to fix it:
{clear: both;}
clearfix
Once you understand what is happening, use the method below to “clearfix” it.
DEMO
Source code for webtoolkit.clearfix.css
.clearfix:after {
content: ".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}
.clearfix {
display: inline-block;
}
html[xmlns] .clearfix {
display: block;
}
* html .clearfix {
height: 1%;
}
--------------------------------------------------------
以下是另外一种 clearfix 的写法,来处 jQuery UI:
.clearfix:before, .clearfix:after { content: ""; display: table; }
.clearfix:after { clear: both; }
.clearfix { zoom: 1; }
[url=http://www.webtoolkit.info/css-clearfix.html]http://www.webtoolkit.info/css-clearfix.html[/url]
See also:
[url=http://www.codecto.com/2011/02/css-clearfix-float/]http://www.codecto.com/2011/02/css-clearfix-float/[/url]
[b]CSS clearfix[/b]
The problem happens when a floated element is within a container box, that element does not automatically force the container’s height adjust to the floated element. When an element is floated, its parent no longer contains it because the float is removed from the flow. You can use 2 methods to fix it:
{clear: both;}
clearfix
Once you understand what is happening, use the method below to “clearfix” it.
DEMO
Source code for webtoolkit.clearfix.css
.clearfix:after {
content: ".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}
.clearfix {
display: inline-block;
}
html[xmlns] .clearfix {
display: block;
}
* html .clearfix {
height: 1%;
}
--------------------------------------------------------
以下是另外一种 clearfix 的写法,来处 jQuery UI:
.clearfix:before, .clearfix:after { content: ""; display: table; }
.clearfix:after { clear: both; }
.clearfix { zoom: 1; }