瀏覽器的CSS Hacks,瀏覽器CSSHacks

來源:互聯網
上載者:User

瀏覽器的CSS Hacks,瀏覽器CSSHacks

LZ註:此文原作者是:Paul Irish(Google的前端開發工程師),本文是原文的部分譯文。

我不再使用CSS Hacks了,相反的是,我將使用IE的條件判斷將類應用到body標籤。

 

但是,我想記錄我之前碰到過的每一個瀏覽器特定的CSS 選取器和樣式屬性。我相信也沒有其他方式提供樣式表給獨特的Safari.

 

利用這些CSS Hacks,你能夠更好的針對IE、Chrome、Firefox、Opera和Safari,代碼如下:

 

瀏覽器特定的CSS Hacks綜合列表:

   1:  
   2: /***** Selector Hacks ******/
   3:  
   4: /* IE6 and below */
   5: * html #uno  { color: red }
   6:  
   7: /* IE7 */
   8: *:first-child+html #dos { color: red }
   9:  
  10: /* IE7, FF, Saf, Opera  */
  11: html>body #tres { color: red }
  12:  
  13: /* IE8, FF, Saf, Opera (Everything but IE 6,7) */
  14: html>/**/body #cuatro { color: red }
  15:  
  16: /* Opera 9.27 and below, safari 2 */
  17: html:first-child #cinco { color: red }
  18:  
  19: /* Safari 2-3 */
  20: html[xmlns*=""] body:last-child #seis { color: red }
  21:  
  22: /* safari 3+, chrome 1+, opera9+, ff 3.5+ */
  23: body:nth-of-type(1) #siete { color: red }
  24:  
  25: /* safari 3+, chrome 1+, opera9+, ff 3.5+ */
  26: body:first-of-type #ocho {  color: red }
  27:  
  28: /* saf3+, chrome1+ */
  29: @media screen and (-webkit-min-device-pixel-ratio:0) {
  30:  #diez  { color: red  }
  31: }
  32:  
  33: /* iPhone / mobile webkit */
  34: @media screen and (max-device-width: 480px) {
  35:  #veintiseis { color: red  }
  36: }
  37:  
  38:  
  39: /* Safari 2 - 3.1 */
  40: html[xmlns*=""]:root #trece  { color: red  }
  41:  
  42: /* Safari 2 - 3.1, Opera 9.25 */
  43: *|html[xmlns*=""] #catorce { color: red  }
  44:  
  45: /* Everything but IE6-8 */
  46: :root *> #quince { color: red  }
  47:  
  48: /* IE7 */
  49: *+html #dieciocho {  color: red }
  50:  
  51: /* IE 10+ */
  52: @media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {
  53:    #veintiun { color: red; }
  54: }
  55:  
  56: /* Firefox only. 1+ */
  57: #veinticuatro,  x:-moz-any-link  { color: red }
  58:  
  59: /* Firefox 3.0+ */
  60: #veinticinco,  x:-moz-any-link, x:default  { color: red  }
  61:  
  62: /* FF 3.5+ */
  63: body:not(:-moz-handler-blocked) #cuarenta { color: red; }
  64:  
  65:  
  66: /***** Attribute Hacks ******/
  67:  
  68: /* IE6 */
  69: #once { _color: blue }
  70:  
  71: /* IE6, IE7 */
  72: #doce { *color: blue; /* or #color: blue */ }
  73:  
  74: /* Everything but IE6 */
  75: #diecisiete { color/**/: blue }
  76:  
  77: /* IE6, IE7, IE8, but also IE9 in some cases   */
  78: #diecinueve { color: blue\9; }
  79:  
  80: /* IE7, IE8 */
  81: #veinte { color/*\**/: blue\9; }
  82:  
  83: /* IE6, IE7 -- acts as an !important */
  84: #veintesiete { color: blue !ie; } /* string after ! can be anything */
  85:  
  86: /* IE8, IE9 */
  87: #anotherone  {color: blue\0/;} /* must go at the END of all rules */
  88:  
  89: /* IE9, IE10 */
  90: @media screen and (min-width:0\0) {
  91:     #veintidos { color: red}
  92: }

 

來源:http://www.ido321.com/509.html


css hacks什意思

為瞭解決不同瀏覽器之間 CSS 支援的不同, CSS Hacks 誕生了.
CSS Hack 在用CSS的幾點:
1. 有效. 能夠通過 Web 標準的驗證.
2. 只針對太古老的/不再開發的/已被拋棄的瀏覽器. 而不是目前的主流瀏覽器.
3. 代碼要醜陋. 讓人記住這是一個不得已而為之的 Hack, 時刻記住要想辦法去掉.

你把他們當成是網路駭客一樣,只是對象是處理CSS就行了。
 
為IE 單獨寫 CSS 的幾種方法

熟悉的方法有 IE hacks 和條件注釋 CSS(Conditional Stylesheets),下面,不妨討論一下這兩種方法的優缺點。IE hacks舉個例子,一個元素在其他瀏覽中的左邊距是 30px ,而在 IE6 中則設定為 20px ,可以如下編寫: .demo {margin-left: 30px; _margin-left: 20px; } 對我個人而言,喜歡條件注釋 CSS 是勝於 IE hacks ,光是 IE hacks 裡面帶有“hacks”這個單詞已經讓人很不舒服,總覺得這是偏方,而且是很偏的解決方案。但是,IE hacks 也有它的優點——CSS hacks 內嵌在普通的 CSS 裡面,不會產生更多 HTTP 要求。CSS hacks 內嵌在普通的 CSS 裡面,編寫時比較方便。當然,它的缺點也很明顯——它是不標準的產物。內嵌在其他 CSS 中,不便維護。尤其是當 hacks 的數量比較多的時候維護簡直是個惡夢。內嵌在其他 CSS 中,即使在非 IE 瀏覽器中也會被載入,浪費資源。條件注釋 CSS同樣是上面的例子,如果使用條件注釋 CSS ,可以如下編寫:HTML <!--[if IE 6 ]> <link rel="stylesheet" type="text/css" media="all" href="./ie6.css" /> <![endif]--> ie6.css .demo {margin-left: 20px; } 這裡說明一下:條件注釋是一種 IE 專有的、對常規(X)HTML 注釋的 Miscrosoft 擴充。從 W3C 標準來說,它也是不標準的產物,但它是微軟官方推出的針對 IE 進行開發的方式,並且條件注釋對於其他所有瀏覽器作為常規注釋出現,因此對其他瀏覽器無害。條件注釋 CSS 的好處是在獨立的 CSS 檔案中編寫,能準確控制在特定的 IE 中載入,不會造成資源浪費,並且便於維護。缺點就是會產生多餘的 HTTP 要求,尤其是當你需要相容的 IE 版本很多的時候,你就需要產生多個 HTTP 要求,這對於本來通道數目就少的低版本 IE 來說無疑會影響頁面載入速度。顯然,以上兩種方法都不是很好的方法,因此,接下來介紹一種相對來說更好的解決方案。例如: <!DOCTYPE html> <!--[if IE 6 ]> <html class="ie6 lte_ie6 lte_ie7 lte_ie8" lang="zh-CN"> <![endif]--> <!--[if lte IE 6 ]> <html class="lte_ie6 lte_ie7 lte_ie8" lang="zh-CN"> <![endif]--> <!--[if lte IE 7 ]> <html class="lte_ie7 lte_ie8" lang="zh-CN"> <![endif]--> <!--[if lte IE 8 ]> <html class="lte_ie8" lang="zh-CN&qu......餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.