访问器特殊的CSS Hacks汇总

2020-10-20 13:48 jianzhan

我已不应用CSS Hacks了,相反的是,我将应用IE的标准分辨将类运用到body标识。
 
可是,我想纪录我以前碰到过的每个访问器特殊的CSS 挑选器和款式特性。我坚信也沒有别的方法出示款式表给与众不同的Safari.
 
运用这些CSS Hacks,你可以更好的对于IE、Chrome、Firefox、Opera和Safari,编码以下:
 
访问器特殊的CSS Hacks综合性目录:


拷贝编码
编码以下:

/***** 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⑶ */
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⑻ */
root *> #quince { color: red }
/* IE7 */
*+html #dieciocho { color: red }
/* IE 10+ */
@media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {
#veintiun { color: red; }
}
/* Firefox only. 1+ */
#veinticuatro, x:-moz-any-link { color: red }
/* Firefox 3.0+ */
#veinticinco, x:-moz-any-link, x:default { color: red }
/* FF 3.5+ */
body:not(:-moz-handler-blocked) #cuarenta { color: red; }
/***** Attribute Hacks ******/
/* IE6 */
#once { _color: blue }
/* IE6, IE7 */
#doce { *color: blue; /* or #color: blue */ }
/* Everything but IE6 */
#diecisiete { color/**/: blue }
/* IE6, IE7, IE8, but also IE9 in some cases */
#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 */
/* IE8, IE9 */
#anotherone {color: blue\0/;} /* must go at the END of all rules */
/* IE9, IE10 */
@media screen and (min-width:0\0) {
#veintidos { color: red}
}

1.标准款式表

像这样的编码你应当见过:


拷贝编码
编码以下:

<link rel="stylesheet" type="text/css" href="css/style.css" />
<!--[if IE]>
<link rel="stylesheet" type="text/css"href="css/ie.css" />
< ![endif]-->

PS:yahoo的內部编号最好做法其实不提议应用有标准的款式表。它会提升附加的均值1或2个HTTP免费下载恳求(参照这里)。

2.挑选器Hacks(Selector Hacks)


拷贝编码
编码以下:

/* IE6 及下列 */
* html #uno { color: red }

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

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

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

/* Opera 9.27 及下列, safari 2 */
html:first-child #cinco { color: red }

/* Safari 2⑶ */
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 }
}</p> <p>/* Safari 2 - 3.1 */
html[xmlns*=""]:root #trece { color: red }

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

/* IE6⑻之外 */
: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 }

PS:挑选器Hacks方法较为多, 但要是编码写得够规范, 实际上要 Hack 的地区不容易许多的, 除有时IE捣蛋,IE 之外的访问器基本上都不容易有难题。

3.特性hacks(Attribute Hacks)


拷贝编码
编码以下:

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

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

/* IE6之外 */
#diecisiete { color/**/: blue }

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

/* IE7, IE8 */
#veinte { color/*\**/: blue\9; }</p> <p>/* 仅IE8 */
#veinte { color: blue\0; }

PS:特性Hacks混写是我较多应用的1种方法,觉得写起来较为简易。CSS Hacks的应用大多数状况下是以便兼具1下爱捣蛋的IE,应用Attribute Hacks基础上能处理对IE的适配。
4.特性hacks混写


拷贝编码
编码以下:

/* !important优先选择 */
#bgcolor {
background:red !important; /* Firefox 等别的访问器 */
background:blue; /* IE6 */
}</p> <p>#test {
background-color: black; /* Firefox, Opera, IE8 */
[;background-color: green;] /* Safari, Chrome */
*background-color: blue; /* IE7 */
_background-color: red; /* IE6 */
}

PS:特性hacks混写要留意撰写顺序。