web開發之瀏覽器(五)----瀏覽器兼容CSS hack

首先,僅僅IE支持的條件註釋語句css

<!--[if !IE]><!--> 除IE外均可識別 <!--<![endif]-->
<!--[if IE]> 全部的IE可識別 <![endif]-->
<!--[if IE 6]> 僅IE6可識別 <![endif]-->
<!--[if lt IE 6]> IE6以及IE6如下版本可識別 <![endif]-->
<!--[if gte IE 6]> IE6以及IE6以上版本可識別 <![endif]-->
<!--[if IE 7]> 僅IE7可識別 <![endif]-->
<!--[if lt IE 7]> IE7以及IE7如下版本可識別 <![endif]-->
<!--[if gte IE 7]> IE7以及IE7以上版本可識別 <![endif]-->
<!--[if IE 8]> 僅IE8可識別 <![endif]-->
<!--[if IE 9]> 僅IE9可識別 <![endif]-->html

例子web

<!--[if lt IE 9]>瀏覽器

加載CSS1
<!--[else]>
加載CSS2
<![endif]-->
這樣有效是有效,可是用HTML VALIDATOR裏,報錯,由於這個不符合XHTML 1.1的規範,
若是把ELSE語句去掉,則正確.ui

方法:url

加載CSS2
<!--[if lt IE 9]>
加載CSS1(能夠把要重寫的寫在這裏).
<![endif]-->spa

其次,各IE瀏覽器經常使用前綴(又是IE,我已經無力吐槽了).net

*html *前綴只對IE6生效
*+html *+前綴只對IE7生效
@media screen\9{...}只對IE6/7生效
@media \0screen {body { background: red; }}只對IE8有效
@media \0screen\,screen\9{body { background: blue; }}只對IE6/7/8有效
@media screen\0 {body { background: green; }} 只對IE8/9/10有效
@media screen and (min-width:0\0) {body { background: gray; }} 只對IE9/10有效
@media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {body { background: orange; }} 只對IE10有效

再次,CSS特殊字符hackfirefox

「\9」、「*」、「_」「!important」code

「\9」支持全部IE系列

「*」僅IE6,IE7

「_」IE6

「!important」僅IE7,firefox等


最後,各類特殊字符Hack實例驗證

  • Hack應用情境(一)

  • 適用範圍:IE:6.0,IE7.0,IE8.0之間的兼容

  • 實例代碼:

  • .bb{
    height:32px;
    background-color:#f1ee18;/*全部識別*/
    .background-color:#00deff\9; /*IE六、七、8識別*/
    +background-color:#a200ff;/*IE六、7識別*/
    _background-color:#1e0bd1;/*IE6識別*/
    }

    /*一個用於展現的class爲bb的div標籤*/

    < div class ="bb"></ div >

  • Hack應用情境(二)

  • 適用範圍:IE:6.0,IE7.0,IE8.0,Firefox之間的兼容

  • 實例代碼:

  • .bb{
    height:32px;
    background-color:#f1ee18;/*全部識別*/
    background-color:#00deff\9; /*IE六、七、8識別*/
    +background-color:#a200ff;/*IE六、7識別*/
    _background-color:#1e0bd1;/*IE6識別*/
    }
    .bb, x:-moz-any-link, x:default{background-color:#00ff00;}/*IE7 firefox3.5及如下 識別 */ 
    @-moz-document url-prefix(){.bb{background-color:#00ff00;}}/* 僅firefox 識別 */ 
    * +html .bb{background-color:#a200ff;}/* 僅IE7 識別 */


    /*一個用於展現的class爲bb的div標籤*/

    < div class ="bb"></ div >


  • Hack應用情境(三)

  • 適用範圍:IE:6.0,IE7.0,IE8.0,Firefox,Safari(Chrome)之間的兼容

  • 實例代碼:

  • .bb{
    height:32px;
    background-color:#f1ee18;/*全部識別*/
    background-color:#00deff\9; /*IE六、七、8識別*/
    +background-color:#a200ff;/*IE六、7識別*/
    _background-color:#1e0bd1;/*IE6識別*/
    }
    @media screen and (-webkit-min-device-pixel-ratio:0){.bb{background-color:#f1ee18}}{} /*safari(Chrome) 有效 */
    .bb, x:-moz-any-link, x:default{background-color:#00ff00;}/*IE7 firefox3.5及如下 識別 */ 
    @-moz-document url-prefix(){.bb{background-color:#00ff00;}}/*僅firefox 識別*/ 
    * +html .bb{background-color:#a200ff;}/* 僅IE7 識別 */


    /*一個用於展現的class爲bb的div標籤*/

    < div class ="bb"></ div >

  • Hack應用情境(四)GOOD)

  • 適用範圍:IE:6.0+,FireFox:2.0+,Opera 10.0+,Sarari 3.0+,Chrome全兼容

  • 實例代碼:

  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" lang="gb2312">

  • <head>

  • <meta http-equiv=Content-Type content="text/html; charset=gb2312"/>

  • <style type="text/css">

  • /***************************************** 各遊覽器兼容CSS **********************************************/
    .bb{height:32px;background-color:#f1ee18;/*全部識別*/ background-color:#00deff\9; /*IE六、七、8識別*/ +background-color:#a200ff;/*IE六、7識別*/ _background-color:#1e0bd1/*IE6識別*/

    @media screen and (-webkit-min-device-pixel-ratio:0){.bb{background-color:#f1ee18}}{} /* Safari(Chrome) 有效 */
    @media all and (min-width: 0px){ .bb{background-color:#f1ee18;/*opera and Safari(Chrome) and firefox*/ background-color:#4cac70\0;}/* 僅 Opera 有效 */ }{} 

    .bb, x:-moz-any-link, x:default{background-color:#4eff00;/*IE七、Firefox3.5及如下 識別 */
    @-moz-document url-prefix(){.bb{background-color:#4eff00;/*僅 Firefox 識別 */}} 
    * +html .bb{background-color:#a200ff;}/* 僅IE7 識別 */

    /* 通常狀況下 咱們區分IE7 只用 +background-color 配合 _background-color 就好了 若是必須寫 .bb, x:-moz-any-link, x:default 這樣的代碼區分 Firefox3.5及如下 則謹記此寫法對IE7也有效,故在其中要再重寫一次 +background-color 或者使用 * +html .bb{background-color:blue;} 方法僅對 IE7 有效。可以使用 @-moz-document url-prefix(){} 方法獨立區分全部 firefox */

    .browsers td{width:8%;text-align:center;padding:8px;}} 
    .browsercolor{color:#333;font-size:18px;font-weight:bold;} 
    .ie6{background-color:#1e0bd1} 
    .ie7{background-color:#a200ff} 
    .ie8{background-color:#00deff} 
    .firefox{background-color:#4eff00} 
    .opera{background-color:#4cac70} 
    .other{background-color:#f1ee18;} 

    #tipTable td,#tipTable th{border:1px solid black;width:56px;height:16px;text-align:center;} 
    #wordTable td{margin-left:8px;} 
    #firefoxTip{display:none;} 
    #firefoxTip, x:-moz-any-link, x:default{display:block;/*IE7 firefox3.5及如下 識別 */+display:none/*再區分一次IE7*/
    @-moz-document url-prefix(){#firefoxTip{display:block;/*僅 firefox 識別 */}} 
    #ChromeTip{display:none;} 
    @media screen and (-webkit-min-device-pixel-ratio:0){#ChromeTip{display:block;}}{} /* safari(Chrome) 有效 */ 
    @media all and (min-width: 0px){#ChromeTip{display:none\0;} /* 僅 Opera 有效 */ }{}

  • </style>

  • </head>

  • <body>

  • <table class="browsers" width="100%" cellspacing="0" cellpadding="0">

  • <tr>

  • <td>IE6</td>

  • <td></td>

  • <td>IE7</td>

  • <td></td>

  • <td>IE8</td>

  • <td></td>

  • <td>Firefox</td>

  • <td></td>

  • <td>Opera</td>

  • <td></td>

  • <td>Safari(Chrome)</td>

  • <td></td>

  • </tr>

  • <tr class="browsercolor">

  • <td class="ie6">IE6</td>

  • <td></td>

  • <td class="ie7">IE7</td>

  • <td></td>

  • <td class="ie8">IE8</td>

  • <td></td>

  • <td class="firefox">Firefox</td>

  • <td></td>

  • <td class="opera">Opera</td>

  • <td></td>

  • <td class="other">Safari(Chrome)</td>

  • <td></td>

  • </tr>

  • </table>

  • <div class="bb">

  • <span style="display:none;display:block\0;display:none\9;">Opera的辨別色是深綠色,Opera遊覽器很時髦麼。</span >

  • <span id="firefoxTip">Firefox的辨別色是淺綠色,Firefox是很強大的遊覽器。</span >

  • <span id="ChromeTip">Safari和Chrome的辨別色是金黃色,Safari和Chrome使用的都是Webkit內核</span >

  • <!--[if IE 8]>IE8的辨別色是藍色,新版IE8的功能但是很多呢。<![endif]-->

  • <!--[if IE 7]>IE7的辨別色是紫色,IE7還能夠湊合着用!<![endif]-->

  • <!--[if IE 6]>IE6的辨別色是紅色,不過,IE6但是有點落後了!<![endif]-->

  • </div>

  • </body>

  • </html>

附表:

相關文章
相關標籤/搜索