常用浏览器CSS hack汇总表及部分示例打赏

1. 此汇总表中测试浏览器的版本为:
微软系统自带:IE6、 IE7、IE8
火狐:Firefox 3.6.6
Safari:Safari 5.0
谷歌浏览器:Chrome 6.0.458.1 dev
Opera浏览器:Opera 10.60
2. 其中,多数CSS hack是在selector{property:value;}基础上更改的。
selector代表CSS选择器,property代表CSS特性,value代表特性的值。

3. FF代表Firefox,Ch代表Chorme,Sa代表Safari,Op代表Opera

4. Q代表Quirks Mode,S代表Standards Mode。

5. Hack Type列的数字,指的是上面CSS hack的实现方式中的列表号。
1是指“利用浏览器对相同代码的解析和支持的不同实现的hack”,2是指以Firefox或Webkit特有的扩展样式实现的hack。

一定遵守CSS hack的三条原则。CSS hack是没有办法的时候才使用的解决兼容性问题的招术,是用兼容性问题去解决兼容性问题,无异于饮鸩止渴。切莫一有兼容性问题就使用,时刻记得改掉用CSS hack修补的问题。

下面列出常见几种浏览器的部分css hack。
1、IE条件注释 IE only

<!--[if lt IE 9]>Less Than IE9< ![endif]-->
<!--[if lte IE 9]>Less Than or Equal IE9< ![endif]-->
<!--[if IE]>IE only< ![endif]-->
<!--[if IE 6]>IE6 only< ![endif]-->
<!--[if IE 7]>IE7 only< ![endif]-->
<!--[if gt IE 6]> Greater Than IE6< ![endif]-->
<!--[if gte IE 6]> Greater Than or Equal IE6< ![endif]-->
...

2、CSS 选择器(Selector)Hack

/***** Selector Hacks ******/
/* IE6 and below */
* html .setest { color: red }

/* IE7 */
*:first-child+html .setest { color: blue }

/* IE7 */
*+html .setest { color: blue }

/* IE7, FF, Saf, Opera */
html&gt;body .setest { color: green }

/* IE8, FF, Saf, Opera (Everything but IE 6,7) */
html>/**/body .setest { color: #ccc }

/* Everything but IE6-8 */
:root>.setest { color: #000 }

3、CSS 属性(Attribute)Hack

.attest{
color: blue !ie;
/* IE6, IE7 -- acts as an !important string after ! can be anything */
color/**/: green;/* Everything but IE6 */
color:#000; /* all */
+color:blue; /* ie7 */
_color:red; /*ie6注意ie7的"+"号hack写在前端,ietester中的ie6认识"+"*/
*color:#ccc; /* ie6-7 or #color: 777 */
color:#fafafa\9; /* all-ie */
color/*\**/: #ddd\9;/* IE7, IE8 */
color: #fefefe\0;/* IE8, IE9 */
}

Firefox CSS Hack

/* all firefox */
@-moz-document url-prefix(){
.fftest{color:red;}
}

/* Firefox only. 1+ */
.fftest, x:-moz-any-link { color: red }

/* Firefox 3.0+ */
.fftest, x:-moz-any-link, x:default { color: red }
Opera CSS Hack
/* Opera */
@media all and (-webkit-min-device-pixel-ratio:10000), 
not all and (-webkit-min-device-pixel-ratio:0){
head~body .optest { color: red }
}

Safari CSS Hack

/* Safari */
@media screen and (-webkit-min-device-pixel-ratio:0){
.satest { color: red; }
}

/* Safari 2-3 */
html[xmlns*=""] body:last-child .satest { color: red; }

/* Safari 2 - 3.1 */
html[xmlns*=""]:root #.satest { color: red; }

Chrome及其它

/* safari 3+, chrome 1+, opera9+, ff 3.5+ */
body:nth-of-type(1) .chtest { color: red }

/* safari 3+, chrome 1+, opera9+, ff 3.5+ */
body:first-of-type .chtest { color: red }

/* saf3+, chrome1+ */
@media screen and (-webkit-min-device-pixel-ratio:0) {
.chtest { color: red }
}

/* iPhone / mobile webkit */
@media screen and (max-device-width: 480px) {
.chtest{ color: red }
}
常用浏览器CSS hack汇总表及部分示例
文章《常用浏览器CSS hack汇总表及部分示例》二维码
  • 微信打赏
  • 支付宝打赏

已有3条评论

  1. 王伟

    这个对SEO有什么影响吗?

    2012-05-10 22:14 回复
  2. 不锈钢水箱

    我自己刚做了个网站,现在又想再让网络公司帮我做一个博客,就像你一样的。以后我会经常来光顾

    2012-05-10 21:00 回复
  3. 不亦乐乎

    这个好,纯工具知识,我留下了。

    2012-05-10 10:42 回复

(必填)

(必填)

(可选)