*:lang(zh) select {font:12px !important;} /*FF,OP可见,特别提醒:由于Opera最近的升级,目前此句只为FF所识别*/
select:empty {font:12px !important;} /*safari可见*/
这里select是选择符,根据情况更换。第二句是MAC上safari浏览器独有的。
- 仅IE7与IE5.0可以识别
- *+html select {…}
当面临需要只针对IE7与IE5.0做样式的时候就可以采用这个HACK。
- 仅IE7可以识别
- *+html select {…!important;}
当面临需要只针对IE7做样式的时候就可以采用这个HACK。
- IE6及IE6以下识别
- * html select {…}
这个地方要特别注意很多博客都写成了是IE6的HACK其实IE5.x同样可以识别这个HACK。其它浏览器不识别。
html/**/ >body select {…}
这句与上一句的作用相同。
- 仅IE6不识别,屏蔽IE6
- select { display /*屏蔽IE6*/:none;}
这里主要是通过CSS注释分开一个属性与值,注释在冒号前。
- 仅IE6与IE5不识别,屏蔽IE6与IE5
- select/**/ { display /*IE6,IE5不识别*/:none;}
这里与上面一句不同的是在选择符与花括号之间多了一个CSS注释。不屏蔽IE5.5
- 仅IE5不识别,屏蔽IE5
- select/*IE5不识别*/ {…}
这一句是在上一句中去掉了属性区的注释。只有IE5不识别,IE5.5可以识别。
- 盒模型解决方法
- selct {width:IE5.x宽度; voice-family :"\"}\""; voice-family:inherit; width:正确宽度;}
盒模型的清除方法不是通过!important来处理的。这点要明确。
- 清除浮动
- select:after {content:"."; display:block; height:0; clear:both; visibility:hidden;}
在Firefox中,当子级都为浮动时,那么父级的高度就无法完全的包住整个子级,那么这时用这个清除浮动的HACK来对父级做一次定义,那么就可以解决这个问题。
- 截字省略号
- select { -o-text-overflow:ellipsis; text-overflow:ellipsis; white-space:nowrap; overflow:hidden; }
这个是在越出长度后会自行的截掉多出部分的文字,并以省略号结尾,很好的一个技术。只是目前Firefox并不支持。
- 只有Opera识别
- @media all and (min-width: 0px){ select {……} }
针对Opera浏览器做单独的设定。
以上都是写CSS中的一些HACK,这些都是用来解决局部的兼容性问题,如果希望把兼容性的内容也分离出来,不妨试一下下面的几种过滤器。这些过滤器有的是写在CSS中通过过滤器导入特别的样式,也有的是写在HTML中的通过条件来链接或是导入需要的补丁样式。
- IE5.x的过滤器,只有IE5.x可见
- @media tty {
i{content:"\";/*" "*/}} @import 'ie5win.css'; /*";}
}/* */
- IE5/MAC的过滤器,一般用不着
- /*\*//*/
@import "ie5mac.css";
/**/
- IE的if条件Hack
- <!--[if IE]> Only IE <![endif]-->
所有的IE可识别
<!--[if IE 5.0]> Only IE 5.0 <![endif]-->
只有IE5.0可以识别
<!--[if gt IE 5.0]> Only IE 5.0+ <![endif]-->
IE5.0包换IE5.5都可以识别
<!--[if lt IE 6]> Only IE 6- <![endif]-->
仅IE6可识别
<!--[if gte IE 6]> Only IE 6/+ <![endif]-->
IE6以及IE6以下的IE5.x都可识别
<!--[if lte IE 7]> Only IE 7/- <![endif]-->
仅IE7可识别
以上内容可能并不全面,欢迎大家能和我一起把这些技巧都汇总起来,为以后工作的查询提供一个方便,同时在这里感谢那些研究出这些HACK的作者们。
------------------------------------------------------2009-04-05-------------------------------------------
opera浏览器:
- @media all and (min-width:0px){#yourId/{ }} empty
IE系列:
- *html _html
- *+html !important
FF浏览器:!important
- *:lang(zh) select {font:12px !important;}
- select:empty {font:12px !important;}
html>body ie7 ff识别 ie6一下不识别 opear支持
ie6在一个class类里定义一个样式的 !important优先级是无效的(即还是按最后一个定义的样式表现),如果把它放在不同的类里定义优先级,则!important有效.
---------------------------------------2009-04-10---------------------------------------
zoom zoom属性也仅IE支持,FF和GC不支持
万能清浮动
这个清浮动代码通过IE6,IE7,FF,Chrome浏览器的测试
.clear_f:after{
content:".";
display:block;
clear:both;
visibility:hidden;
height:0px;
}
display:inline-block
顾名思义,就是在内联情况也块状,可以设定高度宽度.
.element-class {
display: -moz-inline-stack; //Firefox only code
display: inline-block; //some standard browsers
zoom: 1; //IE only
*display: inline; //Only IE know this code (CSS Hack)
}
透明
IE中
#element{Filter:alpah(opacity=40);}
FF中
#element{Opacity:0.4;}
针对IE8正式版的CSS hack
IE8正式版出来有一段日志了,但是针对ie8正式版的CSS hack却很少,其实这是值得庆幸的,因为ie8修复了很多IE6和IE7的一些BUG,更加接近W3C标准。
针对IE8正式版的CSS hack目前可以找到的分为2种:
第一种:”\9″:
基本的写法:
view sourceprint?1..test { color/*\**/: blue\9 }
这个IE6、IE7、IE8都能识别;但是IE8不能识别“*”和“_”的css hack;所以我们可以这样写hack:(转载请
view sourceprint?1..header {width:300px;} /* 所有浏览器*/
2..header {width/*\**/:330px\9;} /* 所有浏览器IE浏览器 */
3..header {*width:310px;} /* IE7和IE6能识别,IE8和FF不能识别*/
4..header {_width:290px;} /* IE6能识别,IE7、IE8和FF不能识别*/
第二种:就是使用IE的条件注释
具体可以查看这里其他一些css hack的测试:
view sourceprint?1..color1{ color:#F00; color/*\**/:#00F /*\**/}/*IE6,IE7,IE8,FF,OP,SA识别*/
2..color2{ color:#F00; color /*\**/:#00F /*\9**/}/*IE7,IE8,FF,OP,SA识别*/
3..color3{ color:#F00; color/*\**/:#00F \9}/*IE6,IE7,IE8识别*/
4..color4{ color:#F00; color /*\**/:#00F\9}/*IE7,IE8识别*//*“color”和“/*\**/”之间有个空格*/
其中:OP表示Opera,SA表示Safari;
=====================================2009-09-18====================================
清除浮动的最简写法
.clearfix {height: 1%;}
.clearfix:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
以上代码的目的是制作一个简单的横向菜单,但因为对所有的li采用了浮动,导致ul高度丢失,其边框不能包含li元素,解决办法就是对ul子元素清除浮动。
对于IE只要使元素拥有Layout属性即可清除,方法有很多,如height:1%,overflow:hidden,zoom:1等,但我们一般应选取尽可能不造成副作用的方式,即最优办法是采用zoom:1的办法。
对于符合标准的浏览器,一般采用CSS的:after伪类为浮动元素后面增加一个清除浮动的元素来实现。上面的代码通过增加一个.号,然后通过clear:both;display:block来清除浮动,然后通过visibility:hidden和height:0来隐藏这个点号。
上面的写法其实并不是最简的,根据我自己的试验及在51js上看到某人的办法后尝试出了一个更简单且所有浏览器的写法:
.clearfix {zoom:1}
.clearfix:after {
content: '\20';
display: block;
clear: both;
}
content:'\20'指定在浮动列表后增加一个空白字符,首先这个字符不可见,无需visibility:hidden来隐藏,其次他没有高度,无需height:0来隐藏其高度。因此这样就大大简化了原来的代码同时也达到了清除浮动的目的。
=====================================2010-01-21====================================
CSS图片垂直居中
使用纯CSS实现未知尺寸的图片(但高宽都小于200px)在200px的正方形容器中水平和垂直居中。
.box {
/*非IE的主流浏览器识别的垂直居中的方法*/
display: table-cell;
vertical-align:middle;
/*设置水平居中*/
text-align:center;
/* 针对IE的Hack */
*display: block;
*font-size: 175px;/*约为高度的0.873,200*0.873 约为175*/
*font-family:Arial;/*防止非utf-8引起的hack失效问题,如gbk编码*/
width:200px;
height:200px;
border: 1px solid #eee;
}
.box img {
/*设置图片垂直居中*/
vertical-align:middle;
}<div class="box">
<img src="http://www.playgoogle.com/image/logo/playgoogleLogo.jpg" />
</div>
这种解决方法的好处是不用加一些不必要的xhtml代码。缺陷是需要已知容器的高度,在容器高度未知时,无法处理。
可同时区分IE8、IE7、IE6、Firefox3、Firefox2的CSS hacks
可同时区分IE8、IE7、IE6、Firefox3、Firefox2的CSS hacks:
.test{
color:/*\**/#00f\9; /* IE8 */
}
.test,
.test:-moz-any-link{
color:#f60; /* Firefox2 */
}
.test,
.test:default{
color:#000; /* Firefox3 */
*color:#f00; /* IE7 */
_color:#0f0; /* IE6 */
}
可同时区分IE8、IE7、IE6、Firefox的CSS hacks:
.test{
color:#000; /* Firefox */
color:/*\**/#00f\9; /* IE8 */
*color:#f00; /* IE7 */
_color:#0f0; /* IE6 */
}
关于IE8的hacks:
.test{
color:/*\**/#00f\9; /* IE8 only */
color:#00f\9; /* 适用于所有IE版本 */
}
IE6/IE7 /IE8/Firefox/Chrome/Safari的CSS hack兼容表
| |
IE6 |
IE7 |
IE8 |
Firefox |
Chrome |
Safari |
| !important |
|
Y |
|
Y |
|
|
| _ |
Y |
|
|
|
|
|
| * |
Y |
Y |
|
|
|
|
| *+ |
|
Y |
|
|
|
|
| \9 |
Y |
Y |
Y |
|
|
|
| \0 |
|
|
Y |
|
|
|
| nth-of-type(1) |
|
|
|
|
Y |
Y |
------------------------------------------------------2010.5.5------------------------------------------------------
.wmtimes{color:#c30; /* all */
+color:#f30; /* ie7 */
_color:#07f; /* ie6 注意ie7的"+"号hack写在前端,因为ietester中的ie6认识"+" */
*color:#777; /* ie6-7 */
color:#ccc\9; /* all-ie */
}
@media all and (min-width: 0px){ .wmtimes{color:#06f;} } /* webkit and opera */
@media screen and (-webkit-min-device-pixel-ratio:0){ .wmtimes{color:#aaa444;} } /* webkit */
@media all and (-webkit-min-device-pixel-ratio:10000), not all and (-webkit-min-device-pixel-ratio:0) { .wmtimes{color:#f36;} }/* opera */
@-moz-document url-prefix(){ .wmtimes{color:#f60;}} /* Firefox */
body:empty .sofish{color:#080;} /* Firefox 1-2 */
html>/**/body .wmtimes, x:-moz-any-link, x:default { color:#333; } /* newest firefox */
------------------------------------------------------2010.6.2------------------------------------------------------
/***** Selector Hacks ******/
/* IE6 and below */
* html #uno { color: red }
/* IE7 */
*:first-child+html #dos { color: red }
/* IE7, FF, Saf, Opera */
html>body #tres { color: red }
/* IE8, FF, Saf, Opera (Everything but IE 6,7) */
html>/**/body #cuatro { color: red }
/* Opera 9.27 and below, safari 2 */
html:first-child #cinco { color: red }
/* Safari 2-3 */
html[xmlns*=""] body:last-child #seis { color: red }
/* safari 3+, chrome 1+, opera9+, ff 3.5+ */
body:nth-of-type(1) #siete { color: red }
/* safari 3+, chrome 1+, opera9+, ff 3.5+ */
body:first-of-type #ocho { color: red }
/* saf3+, chrome1+ */
@media screen and (-webkit-min-device-pixel-ratio:0) {
#diez { color: red }
}
/* iPhone / mobile webkit */
@media screen and (max-device-width: 480px) {
#veintiseis { color: red }
}
/* Safari 2 - 3.1 */
html[xmlns*=""]:root #trece { color: red }
/* Safari 2 - 3.1, Opera 9.25 */
*|html[xmlns*=""] #catorce { color: red }
/* Everything but IE6-8 */
:root *> #quince { color: red }
/* IE7 */
*+html #dieciocho { color: red }
/* Firefox only. 1+ */
#veinticuatro, x:-moz-any-link { color: red }
/* Firefox 3.0+ */
#veinticinco, x:-moz-any-link, x:default { color: red }
/***** Attribute Hacks ******/
/* IE6 */
#once { _color: blue }
/* IE6, IE7 */
#doce { *color: blue; /* or #color: blue */ }
/* Everything but IE6 */
#diecisiete { color/**/: blue }
/* IE6, IE7, IE8 */
#diecinueve { color: blue\9; }
/* IE7, IE8 */
#veinte { color/*\**/: blue\9; }
/* IE6, IE7 -- acts as an !important */
#veintesiete { color: blue !ie; } /* string after ! can be anything */
看来是比较全面了,不过内容有点乱,抽时间整理下!
不断更新整理中...