欢迎来到DIVCSS5查找CSS资料与学习DIV CSS布局技术!
    什么是CSShack
    CSShack由于不同的浏览器,比如IE6,IE7,Firefox等,对CSS的解析认识不一样,因此会导致生成的页面效果不一样,得不到我们所需要的页面效果。这个时候我们就需要针对不同的浏览器去写不同的CSS,让它能够同时兼容不同的浏览器,能在不同的浏览器中也能得到我们想要的页面效果。
    CSShack分类
    hack主要分为CSS选择器hack、CSS属性hack、IE条件注释hack。
    CSS选择器hack:比如IE6能识别*html.class{},IE7能识别*+html.class{}或者*:first-child+html.class{}等。
    CSS属性hack:比如IE6能识别下划线"_"和星号"*",IE7能识别星号"*",但不能识别下划线"_",而firefox两个都不能认识等。
    IE条件注释hack:
    针对所有IE:<!--[ifIE]><!--您的代码--><![endif]-->
    针对IE6及以下版本:<!--[ifltIE7]><!--您的代码--><![endif]-->
    这类Hack不仅对CSS生效,对写在判断语句里面的所有代码都会生效。
    书写顺序,一般是将识别能力强的浏览器的CSS写在前面。
    用法
    比如要分辨IE6和firefox两种浏览器,可以这样写:
    div{
    background:green;/*forfirefox*/
    *background:red;/*forIE6*/(bothIE6&&IE7)
    }
    可以看到在IE6中看到是红色的,在firefox中看到是绿色的。
    在firefox中,它是识别不了后面的那个带星号的东西是什么的,于是将它过滤掉,解析得到的结果是:div{background:green},于是理所当然这个div的背景是绿色的。
    在IE6中,它两个background都能识别出来,它解析得到的结果是:div{background:green;background:red;},于是根据优先级别,处在后面的red的优先级高,于是当然这个div的背景颜色就是红色的了。
    浏览器识别
    <!DOCTYPEhtml>
    <html>
    <head>
    <title>CssHack</title>
    <style>
    #test
    {
    width:300px;
    height:300px;
    background-color:blue;/*firefox*/
    background-color:red\9;/*allie*/
    background-color:yellow\0;/*ie8*/
    +background-color:pink;/*ie7*/
    _background-color:orange;/*ie6*/
    }
    :root#test{background-color:purple\9;}/*ie9*/
    @mediaalland(min-width:0px){#test{background-color:black\0;}}/*opera*/
    @mediascreenand(-webkit-min-device-pixel-ratio:0){#test{background-color:gray;}}/*chromeandsafari*/
    </style>
    </head>
    <body>
    <divid="test">test</div>
    </body>
    </html>
    上面这段代码大家可以直接copy出来,保存成html在各浏览器试试。分析:
    (1)background-color:blue;各个浏览器都认识,这里给firefox用;
    (2)background-color:red\9;\9所有的ie浏览器可识别;
    (3)background-color:yellow\0;\0是留给ie8的,但笔者测试,发现最新版opera也认识,汗。。。不过且慢,后面自有hack写了给opera认的,所以,\0我们就认为是给ie8留的;
    (4)+background-color:pink;+ie7定了;
    (5)_background-color:orange;_专门留给神奇的ie6;
    (6):root#test{background-color:purple\9;}:root是给ie9的,网上流传了个版本是:root#test{background-color:purple\0;},新版opera也认识,所以经笔者反复验证最终ie9特有的为:root选择符{属性\9;}
    (7)@mediaalland(min-width:0px){#test{background-color:black\0;}}这个是老是跟ie抢着认\0的神奇的opera,必须加个\0,不然firefox,chrome,safari也都认识。。。
    (8)@mediascreenand(-webkit-min-device-pixel-ratio:0){#test{background-color:gray;}}最后这个是浏览器新贵chrome和safari的。







本文转载自中文网

如需转载,请注明文章出处和来源网址:http://www.divcss5.com/css3-style/c57152.shtml