css兼容性問題 && css hack

字號(hào):


    以前做網(wǎng)頁的時(shí)候,只考慮 ie6 和 ff 的兼容性,公司換了,要求也高了,ff和ie 6 7 8 要全兼容了,
    碰到要單獨(dú)hack ie8的。當(dāng)然,用注釋非常方便,只要添加相應(yīng)的注釋就可以解決。但問題是,為了一句css寫多一個(gè)文件,或者在header上添加注釋,那顯然不是懶人的習(xí)慣做法。結(jié)論如下:
    selector{
    property:value; /* 所有瀏覽器 */
    property:value9; /* 所有ie瀏覽器 */
    +property:value; /* ie7 */
    _property
    當(dāng)然,注意順序。根據(jù)css的優(yōu)先性,上面的寫法,分別針對(duì)firefox、ie8、ie7和ie6顯示值。讓我們看看這個(gè):
    css代碼如下:
    代碼如下:
    p.ie{
    height:60px;text-align:center;line-height:60px;border:1px dashed #bbb;background:#f7f7f7;font:15;
    color:blue; // 所有瀏覽器
    color:brown9; // 所有ie瀏覽器
    +color:red; // ie7
    _color:green; // ie6
    }
    html 代碼:
    代碼如下:
    <body style=width:500px;margin:0 auto;>
    <p class=ie>
    <span style=display:block;display:none9;>嘿嘿,小子竟然也用firefox,藍(lán)色文字。</span>
    <!--[if ie 8]>不錯(cuò)不錯(cuò),挺先進(jìn)的嘛,使用ie8呢!文字是褐色的。<![endif]-->
    <!--[if ie 7]>你,ie7,紅色文字!<![endif]-->
    <!--[if ie 6]>孩子,雖然顯示的是綠色文字,不過,ie6可不是好東西呢!<![endif]-->
    </p>
    </body>
    注意下面介紹的這些hack寫法僅適用于xhtml1.0。如果沒有在html最前加上
    <!doctype html public -//w3c//dtd xhtml 1.0 transitional//en xmlns=>
    那么效果將不一樣!此外,這里所說的ie8,不是指ie8的兼容模式,因?yàn)閕e8的兼容模式其實(shí)就是ie7。
    區(qū)別ie6、7與ff/ie8:
    background:blue;*background:orange;
    引用
    顯示效果:
    ie 6/7:orange
    ff/ie8:blue
    原理:ff/ie8不支持*開頭,而ie6/7都支持。
    區(qū)別ie6與ie7/ie8/ff:
    background:green;_background:blue;
    引用
    顯示效果:
    ie7/8/ff:green
    ie6:blue
    原理:ie6支持下劃線_,ie7、8和firefox均不支持下劃線。
    區(qū)別ff/ie8和ie6/7:
    background:orange;+background:green;-background:blue;
    或者
    background:orange;*background:green!important;*background:blue;
    引用
    顯示效果:
    ie6:blue
    ie7:green
    ff/ie8:orange
    原理:ie6能識(shí)別-,ie7能識(shí)別+,ie8和ff都不能識(shí)別+和-
    ie8/ff都不識(shí)別*,ie7優(yōu)先識(shí)別!important,ie6不能識(shí)別!important。
    關(guān)于ie8的hacks:
    .test{
    color:/***/#00f9; /* ie8 only */
    color:#00f9; /* 適用于所有ie版本 */
    }
    可同時(shí)區(qū)分ie8、ie7、ie6、firefox的css hacks:
    .test{
    color:#000; /* firefox */
    color:/***/#00f9; /* ie8 */
    *color:#f00; /* ie7 */
    _color:#0f0; /* ie6 */
    }
    添加相應(yīng)的注釋解決兼容性問題
    注釋相應(yīng)的css文件:
    <link rel=stylesheet type=text/css href=css/style.css media=screen />
    <!--[if ie 6]>
    <link rel=stylesheet type=text/css href=css/ie6style.css media=screen />
    <![endif]-->
    <!--[if ie 7]>
    <link rel=stylesheet type=text/css href=css/ie7style.css media=screen />
    <![endif]-->
    <!--[if gte ie 8]>
    <link rel=stylesheet type=text/css href=css/ie8style.css media=screen />
    <![endif]-->
    注釋相應(yīng)的css 內(nèi)容:
    <!--[if ie 6]>
    <style>
    <!--
    #warp{ padding-bottom:11px;}
    -->
    </style>
    <![endif]-->
    <!--[if ie 7]>
    <style>
    <!--
    #warp{ padding-bottom:11px;}
    -->
    </style>
    <![endif]-->
    <!--[if ie 8]>
    <style>
    <!--
    #warp{ padding-bottom:11px;}
    -->
    </style>
    <![endif]-->