教育行業(yè)A股IPO第一股(股票代碼 003032)

全國咨詢/投訴熱線:400-618-4000

UI培訓(xùn)之CSS HACK的使用

更新時間:2016年03月25日12時00分 來源:傳智播客UI培訓(xùn)學(xué)院 瀏覽次數(shù):

 

 
·         兼容范圍:
·         IE:6.0+,F(xiàn)ireFox:2.0+,Opera 10.0+,Sarari 3.0+,Chrome
·         參考資料:
·          
各游覽器常用兼容標(biāo)記一覽表:
·          
·          
標(biāo)記 IE6 IE7 IE8 FF Opera Sarari
[*+><] X X X X
_ X X X X X
\9 X X X
\0 X X X X
@media screen and (-webkit-min-device-pixel-ratio:0){.bb {}} X X X X X
.bb , x:-moz-any-link, x:default X X √(ff3.5及以下) X X
@-moz-document url-prefix(){.bb{}} X X X X X
@media all and (min-width: 0px){.bb {}} X X X
* +html .bb {} X X X X X
游覽器內(nèi)核 Trident Trident Trident Gecko Presto WebKit
·          
(以上 .bb 可更換為其它樣式名)
·          
·          
注意點:
·          
·          
網(wǎng)上很多資料中常常把!important也作為一個hack手段,其實這是一個誤區(qū)。!important常常被我們用來更改樣式,而不是兼容hack。造成這個誤區(qū)的原因是IE6在某些情況下不主動識別!important,以至于常常被人誤用做識別IE6的hack。可是,大家注意一下,IE6只是在某些情況下不識別(ie6下,同一個大括號里對同一個樣式屬性定義,其中一個加important 則important標(biāo)記是被忽略的,例:{background:red!important; background:green;} ie6下解釋為背景色green,其它瀏覽器解釋為背景色red;如果這同一個樣式在不同大括號里定義,其中一個加important 則important發(fā)揮正常作用,例:div{background:red!important} div{background:green},這時所有瀏覽器統(tǒng)一解釋為背景色red。)
·          
---
·          
·         實例講解:
·          
Hack應(yīng)用情境(一)
·          
·          
適用范圍:IE:6.0,IE7.0,IE8.0之間的兼容
·          
·          
實例說明:
·          
·         此例中我們使用了漸進識別的方式,從總體中逐漸排除局部。首先,巧妙的使用“\9”這一標(biāo)記,將IE游覽器從所有情況中分離出來。接著,再次使用“+”將IE8和IE7、IE6分離開來,此時,我們的IE8已經(jīng)獨立識別。
·          
實例代碼:
·          
·          
1  .bb{
height:32px;
background-color:#f1ee18;/*所有識別*/
.background-color:#00deff\9; /*IE6、7、8識別*/
+background-color:#a200ff;/*IE6、7識別*/
_background-color:#1e0bd1;/*IE6識別*/
}
2   
3  /*一個用于展示的class為bb的div標(biāo)簽*/
4  <div class="bb"></div>
 
---
·          
Hack應(yīng)用情境(二)
·          
·          
適用范圍:IE:6.0,IE7.0,IE8.0,Firefox之間的兼容
·          
·          
實例說明:
·          
·         大家很容易的可以看出這是情境(一)的加強版,適用于更廣泛的環(huán)境。其實情境(一)中也已經(jīng)做到了把火狐與IE游覽器區(qū)分開來了,現(xiàn)在我們要做的是把火狐從其它游覽器中再次識別出來。大家仔細看下代碼,大家會發(fā)現(xiàn)其實游覽器識別是很簡單的?;鸷绾巫R別?對了,IE中對偽類支持不廣泛,所以偽類是個不錯的途徑。(.yourClass,x:-moz-any-link, x:default)注意,這個區(qū)分偽類往往IE7也能識別,所以最好還需要把IE7單獨識別出來,且此方法對ff3.6 已無效,firefox的區(qū)分可以使用@-moz-document url-prefix(){}
·          
實例代碼:
·          
5   
6  .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 識別 */
7   
8  /*一個用于展示的class為bb的div標(biāo)簽*/
9  <div class="bb"></div>
 
---
·          
Hack應(yīng)用情境(三)
·          
·          
適用范圍:IE:6.0,IE7.0,IE8.0,Firefox,Safari(Chrome)之間的兼容
·          
·          
實例說明:
·          
·         我們現(xiàn)在將再次對我們的CSS進行加強了,使其能識別Safari(Chrome)游覽器。這是基于它們的內(nèi)核webkit來識別的,用法為@media screen and (-webkit-min-device-pixel-ratio:0)
·          
實例代碼:
·          
·          
10  .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 識別 */
11   
12  /*一個用于展示的class為bb的div標(biāo)簽*/
13  <div class="bb"></div>
 
---
·          
Hack應(yīng)用情境(四)
·          
·          
適用范圍:IE:6.0+,F(xiàn)ireFox:2.0+,Opera 10.0+,Sarari 3.0+,Chrome全兼容
·          
·          
·          
實例說明:
·          
·         實例的具體代碼在下面實例代碼中已經(jīng)列出,具體效果如此頁面的頂端部分效果,您可以通過不同游覽器檢測該效果。這次我們基本把所有的主流游覽器都兼容了,大家來看下代碼。Opera的識別有一部分歸功于“\0”標(biāo)記,這個標(biāo)記只被IE8和Opera識別,特殊的標(biāo)記往往造就的是我們更廣泛的hack手段。下例的代碼比較完整,大家可以選擇參考。
·          
實例代碼:
·          
·          
14  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
15  <html xmlns="http://www.w3.org/1999/xhtml" lang="gb2312">
16  <head>
17  <meta http-equiv=Content-Type content="text/html; charset=gb2312"/>
18  <style type="text/css">
19   
20  /***************************************** 各游覽器兼容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 識別 */

/* 一般情況下 我們區(qū)分IE7 只用 +background-color 配合 _background-color 就行了 如果必須寫 .bb, x:-moz-any-link, x:default 這樣的代碼區(qū)分 Firefox3.5及以下 則謹記此寫法對IE7也有效,故在其中要再重寫一次 +background-color或者使用 * +html .bb{background-color:blue;} 方法僅對 IE7 有效??墒褂?@-moz-document url-prefix(){} 方法獨立區(qū)分所有 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/*再區(qū)分一次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 有效 */ }{}
21   
22  </style>
23  </head>
24  <body>
25  <table class="browsers" width="100%" cellspacing="0" cellpadding="0">
26  <tr>
27  <td>IE6</td>
28  <td></td>
29  <td>IE7</td>
30  <td></td>
31  <td>IE8</td>
32  <td></td>
33  <td>Firefox</td>
34  <td></td>
35  <td>Opera</td>
36  <td></td>
37  <td>Safari(Chrome)</td>
38  <td></td>
39  </tr>
40  <tr class="browsercolor">
41  <td class="ie6">IE6</td>
42  <td></td>
43  <td class="ie7">IE7</td>
44  <td></td>
45  <td class="ie8">IE8</td>
46  <td></td>
47  <td class="firefox">Firefox</td>
48  <td></td>
49  <td class="opera">Opera</td>
50  <td></td>
51  <td class="other">Safari(Chrome)</td>
52  <td></td>
53  </tr>
54  </table>
55  <div class="bb">
56  <span style="display:none;display:block\0;display:none\9;">Opera的辨別色是深綠色,Opera游覽器很時髦么。</span >
57  <span id="firefoxTip">Firefox的辨別色是淺綠色,F(xiàn)irefox是很強大的游覽器。</span >
58  <span id="ChromeTip">Safari和Chrome的辨別色是金黃色,Safari和Chrome使用的都是Webkit內(nèi)核</span >
59   
60  <!--[if IE 8]>IE8的辨別色是藍色,新版IE8的功能可是不少呢。<![endif]-->
61   
62   
63  <!--[if IE 7]>IE7的辨別色是紫色,IE7還可以湊合著用!<![endif]-->
64   
65   
66  <!--[if IE 6]>IE6的辨別色是紅色,不過,IE6可是有點落后了!<![endif]-->
67   
68  </div>
69  </body>
70  </html>
 
0 分享到:
和我們在線交談!