css hack的理解

什麼是CSS hack

因爲不一樣廠商的流覽器或某瀏覽器的不一樣版本(如IE6-IE11,Firefox/Safari/Opera/Chrome等),對CSS的支持、解析不同,致使在不一樣瀏覽器的環境中呈現出不一致的頁面展示效果。這時,咱們爲了得到統一的頁面效果,就須要針對不一樣的瀏覽器或不一樣版本寫特定的CSS樣式,咱們把這個針對不一樣的瀏覽器/不一樣版本寫相應的CSS code的過程,叫作CSS hack!html

原理:利用不一樣瀏覽器對CSS的支持和解析結果不同編寫針對特定瀏覽器樣式。常見的hack有web

1.屬性hack   2.選擇器hack   3.IE條件註釋chrome

ie條件註釋:適用於[IE5, IE9]常見格式以下:     瀏覽器

 

 <!--[if IE 6]>
   Special instructions for IE 6 here
<![endif]-->

 


選擇器hack:不一樣瀏覽器對選擇器的支持不同

/***** Selector Hacks ******/

/* IE6 and below */
* html #uno  { color: red }

/* IE7 */
*:first-child+html #dos { color: red }

/* IE7, FF, Saf, Opera  */
html>body #tres { color: red }

/* IE8, FF, Saf, Opera (Everything but IE 6,7) */
html>/**/body #cuatro { color: red }

/* Opera 9.27 and below, safari 2 */
html:first-child #cinco { color: red }

/* Safari 2-3 */
html[xmlns*=""] body:last-child #seis { color: red }

/* safari 3+, chrome 1+, opera9+, ff 3.5+ */
body:nth-of-type(1) #siete { color: red }

/* safari 3+, chrome 1+, opera9+, ff 3.5+ */
body:first-of-type #ocho {  color: red }

/* saf3+, chrome1+ */
@media screen and (-webkit-min-device-pixel-ratio:0) {
 #diez  { color: red  }
}

/* iPhone / mobile webkit */
@media screen and (max-device-width: 480px) {
 #veintiseis { color: red  }
}

/* Safari 2 - 3.1 */
html[xmlns*=""]:root #trece  { color: red  }

/* Safari 2 - 3.1, Opera 9.25 */
*|html[xmlns*=""] #catorce { color: red  }

/* Everything but IE6-8 */
:root *> #quince { color: red  }

/* IE7 */
*+html #dieciocho {  color: red }

/* Firefox only. 1+ */
#veinticuatro,  x:-moz-any-link  { color: red }

/* Firefox 3.0+ */
#veinticinco,  x:-moz-any-link, x:default  { color: red  }
 
不一樣瀏覽器解析bug或方法屬性hack:

 

/* IE6 */
#once { _color: blue }

/* IE6, IE7 */
#doce { *color: blue; /* or #color: blue */ }

/* Everything but IE6 */
#diecisiete { color/**/: blue }

/* IE6, IE7, IE8 */
#diecinueve { color: blue\9; }

/* IE7, IE8 */
#veinte { color/*\**/: blue\9; }

/* IE6, IE7 -- acts as an !important */
#veintesiete { color: blue !ie; } /* string after ! can be anything */
相關文章
相關標籤/搜索