瀏覽器版本增長快於淘汰,CSS Hack 治理必須建立團隊共識。核心原則:
/* Opera 9.27- */
html:first-child #cinco { color: red; }
/* Safari 2-3 */
html[xmlns*=""] body:last-child #seis { color: red; }
/* Firefox 1+ */
#veinticuatro, x:-moz-any-link { color: red; }
/* Firefox 3.0+ */
#veinticinco, x:-moz-any-link, x:default { color: red; }
/* WebKit/Chrome/Opera 15+ */
@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; }
}
/* IE6- */
* html #uno { color: red; }
/* IE7 */
*:first-child+html #dos { color: red; }
/* IE7+ (含現代瀏覽器) */
html>body #tres { color: red; }
/* IE8+ (非 IE6/7) */
html>/**/body #cuatro { color: red; }
/* IE10+ (非標準,僅參考) */
@media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {
#ie10 { color: red; }
}
優點:無副作用、不汙染屬性、易於搜尋清理。
缺點:選擇器冗長、需重複寫選擇器、記憶負擔大。
.sofish {
padding: 10px; /* 標準瀏覽器 */
padding: 9px\9; /* IE8-9 (IE9 支援 \9) */
*padding: 5px; /* IE6-7 */
_padding: 6px; /* IE6 */
+padding: 7px; /* IE7 (可選,\9 已涵蓋) */
}
優點:極易記憶、代碼極少、同一規則集內完成。
缺點:不標準、CSS Lint 報錯、非 IE 瀏覽器 Hack 覆蓋面有限。
<!--[if IE 6]> <link rel="stylesheet" href="ie6.css"> <![endif]-->
<!--[if IE 7]> <link rel="stylesheet" href="ie7.css"> <![endif]-->
<!--[if IE 8]> <link rel="stylesheet" href="ie8.css"> <![endif]-->
<!--[if IE 9]> <link rel="stylesheet" href="ie9.css"> <![endif]-->
<!--[if gt IE 9]><!--> <link rel="stylesheet" href="modern.css"> <!--<![endif]-->
<!--[if !IE]>> <link rel="stylesheet" href="non-ie.css"> <![endif]-->
優點:標準 HTML 註釋、非 IE 完全忽略、無解析開銷、按需載入、極易維護清理。
缺點:增加 HTTP 請求 (可用構建工具合併)、IE10+ 移除支援 (需改用特性檢測)。
// jQuery 範例 (已棄用 $.browser)
if (navigator.userAgent.indexOf('MSIE 6.0') > -1) {
document.body.className += ' ie6';
}
// 現代特性檢測 (推薦)
if (!('querySelector' in document)) { /* IE6-7 */ }
if (CSS.supports('display', 'grid')) { /* 現代瀏覽器 */ }
優點:精準控制、可處理複雜邏輯。
缺點:依賴 JS、增加複雜度、UA 偽裝不可靠、違反漸進增強原則。
.component {
/* 標準屬性 */
padding: 10px;
border-radius: 4px;
box-shadow: 0 2px 4px rgba(0,0,0,.1);
/* IE 專屬 Hack (按版本遞減) */
padding: 9px\9; /* IE8-9 */
*padding: 5px; /* IE6-7 */
_padding: 6px; /* IE6 */
/* border-radius/box-shadow IE9- 不支援,接受優雅降級 (無圓角/陰影) */
}
/* 非 IE 現代瀏覽器特定修正 (極少需求) */
@media screen and (-webkit-min-device-pixel-ratio:0) {
.component { /* WebKit/Blink 專屬微調 */ }
}
@-moz-document url-prefix() {
.component { /* Firefox 專屬微調 */ }
}
/* IE10+ 現代瀏覽器不需 Hack,若有極罕見 Bug 使用 @supports */
@supports (-ms-ime-align:auto) {
.component { /* IE10+ / Edge 專屬 */ }
}
<!-- HTML -->
<!--[if IE 6]><body class="ie6"><![endif]-->
<!--[if IE 7]><body class="ie7"><![endif]-->
<!--[if IE 8]><body class="ie8"><![endif]-->
<!--[if IE 9]><body class="ie9"><![endif]-->
<!--[if gt IE 9]><!--><body class="modern"><!--<![endif]-->
<!--[if !IE]>><body class="non-ie"><![endif]-->
<!-- CSS -->
.component { padding: 10px; } /* 現代預設 */
.non-ie .component { padding: 12px; } /* 非 IE 微調 */
.ie9 .component { padding: 9px; }
.ie8 .component { padding: 8px; }
.ie7 .component { padding: 7px; }
.ie6 .component { padding: 6px; }
優點:零 Hack、語意清晰、極易維護、構建工具可按 class 打包。缺點:HTML 體積微增。
/***** 選擇器 Hack *****/
/* IE6- */ * html #uno { color: red }
/* IE7 */ *:first-child+html #dos { color: red }
/* IE7+ */ html>body #tres { color: red }
/* IE8+ */ html>/**/body #cuatro { color: red }
/* Opera 9.27- */ html:first-child #cinco { color: red }
/* Safari 2-3 */ html[xmlns*=""] body:last-child #seis { color: red }
/* Safari 3+/Chrome/Opera 9+/FF 3.5+ */ body:nth-of-type(1) #siete { color: red }
/* Safari 3+/Chrome */ @media screen and (-webkit-min-device-pixel-ratio:0) { #diez { color: red } }
/* iPhone */ @media screen and (max-device-width:480px) { #veintiseis { color: red } }
/* Safari 2-3.1 */ html[xmlns*=""]:root #trece { color: red }
/* Everything but IE6-8 */ :root * > #quince { color: red }
/* IE7 */ *+html #dieciocho { color: red }
/* FF 1+ */ #veinticuatro, x:-moz-any-link { color: red }
/* FF 3.0+ */ #veinticinco, x:-moz-any-link, x:default { color: red }
/***** 屬性 Hack *****/
/* IE6 */ #once { _color: blue }
/* IE6-7 */ #doce { *color: blue; }
/* Non-IE6 */ #diecisiete { color/**/: blue }
/* IE6-8 */ #diecinueve { color: blue\9; }
/* IE7-8 */ #veinte { color/***/: blue\9; }
/* IE6-7 !important */ #veintesiete { color: blue !ie; }
\9, *, _ 批次刪除* html, :-moz-any-link, -webkit-min-device-pixel-ratio 批次刪除<link> 與 <!--[if IE] 區塊CSS Hack 治理核心在於**「單一職責、易於清理、不汙染標準流」**。推薦:**主流專案用屬性 Hack (經濟實惠) + 條件註釋載入 IE 專用樣式表 (完美主義)**;**新專案棄用 Hack,採用特性檢測 + 漸進增強 + @supports**。建立團隊 Hack 白名單與清理 SOP,讓兼容性代碼可控、可追蹤、可退役。