Heim  >  Artikel  >  Web-Frontend  >  史上最全的css hack(ie6-9,firefox,chrome,opera,safari)_html/css_WEB-ITnose

史上最全的css hack(ie6-9,firefox,chrome,opera,safari)_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:47:141016Durchsuche

在这个浏览器百花争鸣的时代,作为前端开发的我们为了我们漂亮的设计能适应各个浏览器可为煞费苦心,主要体现在javascript和css上面。javascript我这次就不谈了,先说说css。

      为了适应不同浏览器不同的版本(版本主要就ie来说),ie这朵奇葩现在我们要兼容6-9,它的10也快出来了。。。在ie下我们可以写条件注释来区分ie和其他浏览器,以及ie的版本,这些请大家自行google。这篇文章主要讨论的是css hack。下面废话补多说了,直接上代码

 1 <!DOCTYPE html>   2  3 <html>   4  5 <head>   6  7     <title>Css Hack</title>   8  9     <style>  10 11     #test   12 13     {   14 15         width:300px;   16 17         height:300px;   18 19           20 21         background-color:blue;      /*firefox*/22 23         background-color:red\9;      /*all ie*/24 25         background-color:yellow\0;    /*ie8*/26 27         +background-color:pink;        /*ie7*/28 29         _background-color:orange;       /*ie6*/30 31     }  32 33     :root #test { background-color:purple\9; }  /*ie9*/34 35     @media all and (min-width:0px){ #test {background-color:black\0;} }  /*opera*/36 37     @media screen and (-webkit-min-device-pixel-ratio:0){ #test {background-color:gray;} }  /*chrome and safari*/38 39     </style>  40 41 </head>  42 43 <body>  44 45     <div id="test">test</div>  46 47 </body>  48 49 </html>

      上面这段代码大家可以直接copy出来,保存成html在各浏览器试试。下面我来分析下:

    各个浏览器都认识,这里给firefox用;

    \9所有的ie浏览器可识别;

    \0 是留给ie8的,但笔者测试,发现最新版opera也认识,汗。。。不过且慢,后面自有hack写了给opera认的,所以,\0我们就认为是给ie8留的;

    +background-color:pink; + ie7定了;

    _ _专门留给神奇的ie6;

    :root #test { background-color:purple\9; } :root是给ie9的,网上流传了个版本是 :root #test { background-color:purple\0;},呃。。。这个。。。,新版opera也认识,所以经笔者反复验证最终ie9特有的为:root 选择符 {属性\9;}

    @media all and (min-width:0px){ #test {} } 这个是老是跟ie抢着认\0的神奇的opera,必须加个\0,不然firefox,chrome,safari也都认识。。。

    @media screen and (-webkit-min-device-pixel-ratio:0){ #test {background-color:gray;} }最后这个是浏览器新贵chrome和safari的。

 

    好了就这么多了,特别注意以上顺序是不可以改变的。css hack虽然可以解决个浏览器之间css显示的差异问题,但是毕竟不符合W3C规范,我们平时写css最好是按照标准来,这样对我们以后维护也是大有好处的,实在不行再用。

 

    如果给位看官有更好的css hack写法或者本文有不妥之处,欢迎留言,第一次写文章,请轻砸。

 

转自:http://www.cnblogs.com/WuQiang/archive/2011/08/23/2150240.html

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn