CSS兼容性問(wèn)題 && CSS HACK

字號(hào):


    以前做網(wǎng)頁(yè)的時(shí)候,只考慮 IE6 和 FF 的兼容性,公司換了,要求也高了,F(xiàn)F和IE 6 7 8 要全兼容了,
    碰到要單獨(dú)Hack IE8的。當(dāng)然,用注釋非常方便,只要添加相應(yīng)的注釋就可以解決。但問(wèn)題是,為了一句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>
    <p>
    <span>嘿嘿,小子竟然也用Firefox,藍(lán)色文字。</span>
    <!--[if IE 8]>不錯(cuò)不錯(cuò),挺先進(jìn)的嘛,使用IE8呢!文字是褐色的。<![endif]-->
    <!--[if IE 7]>你,IE7,紅色文字!<![endif]-->
    <!--[if IE 6]>孩子,雖然顯示的是綠色文字,不過(guò),IE6可不是好東西呢!<![endif]-->
    </p>
    </body>
    注意下面介紹的這些hack寫法僅適用于XHTML1.0。如果沒有在HTML最前加上
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "xmlns="">
    那么效果將不一樣!此外,這里所說(shuō)的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)的注釋解決兼容性問(wèn)題
    注釋相應(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]-->