清理浮动有很多种方式,像使用 br 标签自带的 clear 属,使用元素的 overflow,使用空标签来设置 clear:both 等等。但考虑到兼容问题和语义化的问题,一般我们都会使用如下代码来清理浮动。
/* 清理浮动 */
.clearfix:after {
visibility:hidden;
display:block;
font-size:0;
content:" ";
clear:both;
height:0;
}
.clearfix {
zoom:1;
}
其原理是,在「高级」浏览器中使用 :after 伪类在浮动块后面加上一个非 display:none 的不可见块状内容来,并给它设置 clear:both 来清理浮动。在 ie6 和 7 中给浮动块添加 haslayout 来让浮动块撑高并正常影响文档流。
上面的代码应该是现在主流的清理浮动方式。现在支付宝就使用这样的方式。而现在,Nicolas Gallagher 给出了一个更简洁的方案:
.cf:before, .cf:after {
content:"";
display:table;
}
.cf:after {
clear:both;
}
.cf {
zoom:1;
}
原理还是一样的。使用 :after 伪类来提供浮动块后的 clear:both。不同的是,隐藏这个空白使用的是 display: table。而不是设置 visibility:hidden;height:0;font-size:0; 这样的 hack。
值得注意的是这里中的 :before 伪类。其实他是来用处理 top-margin 边折叠的,跟清理浮动没有多大的关系。但因为浮动会创建 block formatting context,这样浮动元素上的另而一元素上如果刚好有 margin-bottom 而这个浮动元素刚好有margin-top 的话,应该让他们不折叠(虽然这种情况并不常见)。
2012年1月5日星期四
CSS 清理浮动方式
ref
2012年1月3日星期二
css resource
10 款非常棒的CSS代码格式化工具推荐 original link csslint:css检查 css3pie增强ie浏览器 less css框架 6个炫酷的CSS按钮制作教程 9个实用的CSS/jQuery编程技术与技巧 alice 7 个基于CSS/JavaScript的鼠标悬停效果教程 10 个实验性的 JS/CSS3 编程技术 css动画 oocss 页面重构中的模块化思维 样式命名规则 css3开发工具集 跨浏览器矢量图标 css压缩 CSS金矿 CSS在IE6,IE7和IE8中的兼容性1 CSS在IE6,IE7和IE8中的兼容性2 用CSS3将你的设计带入下个高度 HTML5和CSS3工具资源汇总 xHTML+CSS技巧教程资源大全 推荐CSS库: 更佳网站可读性项目
2011年12月21日星期三
自定义radio和checkbox
ref
p:not(#foo) > input + label { background: url(gr_custom-inputs.png) 0 -1px no-repeat; height: 16px; padding: 0 0 0 18px; } p:not(#foo) > input[type=radio]:hover + label, p:not(#foo) > input[type=radio]:focus + label, p:not(#foo) > input[type=radio] + label:hover { background-position: 0 -181px; } p:not(#foo) > input[type=radio]:hover:checked + label, p:not(#foo) > input[type=radio]:focus:checked + label, p:not(#foo) > input[type=radio]:checked + label:hover { background-position: 0 -261px; } p:not(#foo) > input[type=radio]:disabled + label, p:not(#foo) > input[type=radio]:hover:disabled + label, p:not(#foo) > input[type=radio]:focus:disabled + label, p:not(#foo) > input[type=radio]:disabled + label:hover, p:not(#foo) > input[type=radio]:disabled + label:hover:active { background-position: 0 -221px; } p:not(#foo) > input[type=radio]:disabled:checked + label, p:not(#foo) > input[type=radio]:hover:disabled:checked + label, p:not(#foo) > input[type=radio]:focus:disabled:checked + label, p:not(#foo) > input[type=radio]:disabled:checked + label:hover, p:not(#foo) > input[type=radio]:disabled:checked + label:hover:active { background-position: 0 -301px; } p:not(#foo) > input[type=radio]:active + label, p:not(#foo) > input[type=radio] + label:hover:active { background-position: 0 -201px; } p:not(#foo) > input[type=radio]:active:checked + label, p:not(#foo) > input[type=radio]:checked + label:hover:active { background-position: 0 -281px; }
订阅:
博文 (Atom)