瀏覽器樣式兼容之CSS hack技巧大全

  • 原文鏈接點擊打開鏈接
  • 兼容範圍:
  • IE:6.0+,FireFox:2.0+,Opera 10.0+,Sarari 3.0+,Chrome
  • 參考資料:
  • 各遊覽器常用兼容標記一覽表:

  • 標記IE6IE7IE8FFOperaSarari
    [*+><]XXXX
    _XXXXX
    \9XXX
    \0XXXX
    @media screen and (-webkit-min-device-pixel-ratio:0){.bb {}}XXXXX
    .bb , x:-moz-any-link, x:defaultXX√(ff3.5及以下)XX
    @-moz-document url-prefix(){.bb{}}XXXXX
    @media all and (min-width: 0px){.bb {}}XXX
    * +html .bb {}XXXXX
    遊覽器內核TridentTridentTridentGeckoPrestoWebKit
    (以上 .bb 可更換爲其它樣式名)
    • 注意點:

    • 網上很多資料中常常把!important也作爲一個hack手段,其實這是一個誤區。!important常常被我們用來更改樣式,而不是兼容hack。造成這個誤區的原因是IE6在某些情況下不主動識別!important,以至於常常被人誤用做識別IE6的hack。可是,大家注意一下,IE6只是在某些情況下不識別(ie6下,同一個大括號裏對同一個樣式屬性定義,其中一個加important 則important標記是被忽略的,例:{background:red!important; background:green;} ie6下解釋爲背景色green,其它瀏覽器解釋爲背景色red;如果這同一個樣式在不同大括號裏定義,其中一個加important 則important發揮正常作用,例:div{background:red!important} div{background:green},這時所有瀏覽器統一解釋爲背景色red。)


    • 實例講解:
    • Hack應用情境(一)

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

    • 實例說明:

    • 此例中我們使用了漸進識別的方式,從總體中逐漸排除局部。首先,巧妙的使用“\9”這一標記,將IE遊覽器從所有情況中分離出來。接着,再次使用“+”將IE8和IE7、IE6分離開來,此時,我們的IE8已經獨立識別。
    • 實例代碼:

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

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

      < div class ="bb"></ div >
    • Hack應用情境(二)

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

    • 實例說明:

    • 大家很容易的可以看出這是情境(一)的加強版,適用於更廣泛的環境。其實情境(一)中也已經做到了把火狐與IE遊覽器區分開來了,現在我們要做的是把火狐從其它遊覽器中再次識別出來。大家仔細看下代碼,大家會發現其實遊覽器識別是很簡單的。火狐如何識別?對了,IE中對僞類支持不廣泛,所以僞類是個不錯的途徑。(.yourClass,x:-moz-any-link, x:default)注意,這個區分僞類往往IE7也能識別,所以最好還需要把IE7單獨識別出來,且此方法對ff3.6 已無效,firefox的區分可以使用@-moz-document url-prefix(){}
    • 實例代碼:

    • .bb{
      height:32px;
      background-color:#f1ee18;/*所有識別*/
      background-color:#00deff\9; /*IE6、7、8識別*/
      +background-color:#a200ff;/*IE6、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)之間的兼容

    • 實例說明:

    • 我們現在將再次對我們的CSS進行加強了,使其能識別Safari(Chrome)遊覽器。這是基於它們的內核webkit來識別的,用法爲@media screen and (-webkit-min-device-pixel-ratio:0)
    • 實例代碼:

    • .bb{
      height:32px;
      background-color:#f1ee18;/*所有識別*/
      background-color:#00deff\9; /*IE6、7、8識別*/
      +background-color:#a200ff;/*IE6、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應用情境(四)

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

    • 實例說明:

    • 實例的具體代碼在下面實例代碼中已經列出,具體效果如此頁面的頂端部分效果,您可以通過不同遊覽器檢測該效果。這次我們基本把所有的主流遊覽器都兼容了,大家來看下代碼。Opera的識別有一部分歸功於“\0”標記,這個標記只被IE8和Opera識別,特殊的標記往往造就的是我們更廣泛的hack手段。下例的代碼比較完整,大家可以選擇參考。
    • 實例代碼:

    • <!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; /*IE6、7、8識別*/ +background-color:#a200ff;/*IE6、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;/*IE7、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>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章