萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> HTML5 教學

舊版 IE 相容 HTML5/CSS3 完整解決方案:html5shiv、selectivizr、條件註解與漸進增強策略

雖然 IE 已退出歷史舞台,但許多企業內部系統、政府專案、特定行業應用仍需支援 IE11 甚至 IE9/10。本文系統整理**讓 HTML5/CSS3 在舊版 IE 正常運行**的完整技術棧:html5shiv (語意標籤)、selectivizr (CSS3 選擇器)、respond.js (媒體查詢)、條件註解 Hack、Modernizr 整合,並闡述**漸進增強**核心哲學與現代棄用策略。

核心 Polyfill 組合包

腳本解決問題支援範圍CDN 連結 (jsDelivr)
html5shiv 識別 HTML5 新標籤 (article, section, nav...),修正 innerHTML/cloneNode、列印樣式 IE6~IE9 https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js
html5shiv-printshiv 修正 IE 列印/預覽時 HTML5 標籤樣式失效 IE6~IE9 https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv-printshiv.min.js
selectivizr 支援 CSS3 偽類/屬性選擇器 (:last-child, :nth-child, [attr^=val] 等) IE6~IE8 (需搭配 JS 庫: jQuery/Prototype/MooTools/DOM) https://cdn.jsdelivr.net/npm/selectivizr@1.0.3/selectivizr-min.js
respond.js 支援 CSS3 Media Queries (min-width, max-width) IE6~IE8 https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js
Modernizr (含 html5shiv) 特性偵測 + HTML5 標籤修補 (選項 html5shiv: true) 所有 IE https://cdn.jsdelivr.net/npm/modernizr@3.11.9/modernizr.min.js

標準引入模板 (放在 <head> 內)

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv-printshiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->
<!--[if (gte IE 6)&(lte IE 8)]>
  <script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script> <!-- selectivizr 需要 JS 庫 -->
  <script src="https://cdn.jsdelivr.net/npm/selectivizr@1.0.3/selectivizr-min.js"></script>
<![endif]-->
  • 順序關鍵:html5shiv → respond.js → JS 庫 → selectivizr。
  • selectivizr 必須在 CSS 之後載入,否則無法分析樣式表。放在 </body> 前或用 defer。
  • respond.js 僅支援 min/max-width,min/max-height, orientation 不支援。

條件註解 CSS Hack:零 JS、零閃爍、最高效能

在 <html> 標籤加上 IE 版本 class,CSS 直接針對性覆寫,**無需等待 JS、無閃爍、效能最佳**。

<!--[if lt IE 7]> <html class="ie6" lang="zh-Hant"> <![endif]-->
<!--[if IE 7]>     <html class="ie7" lang="zh-Hant"> <![endif]-->
<!--[if IE 8]>     <html class="ie8" lang="zh-Hant"> <![endif]-->
<!--[if IE 9]>     <html class="ie9" lang="zh-Hant"> <![endif]-->
<!--[if gt IE 9]><!--> <html lang="zh-Hant"> <!--<![endif]-->

CSS 中針對性修補

.ie6 .layout { zoom: 1; }           /* hasLayout 觸發 */
.ie7 .nav li { display: inline; }   /* inline-block 修正 */
.ie8 .box { filter: progid:DXImageTransform.Microsoft.gradient(...); } /* 漸層 */
.ie9 .shadow { box-shadow: none; }  /* 關閉不支援效果 */

漸進增強核心原則

  1. 基礎體驗無 JS/CSS3 也能用:語意化 HTML、伺服器端渲染內容、標準表單提交。
  2. 現代瀏覽器享受豐富體驗:CSS3 動畫、Flexbox/Grid、Web Fonts、JS 互動增強。
  3. 舊瀏覽器提供可用的降級體驗:佈局不亂、功能可操作、視覺可接受(不需像素級一致)。
  4. Polyfill 只填補「關鍵功能缺口」,不追求「所有特性全模擬」。

Modernizr 整合策略

Modernizr 3.x 內建 html5shiv 選項,建置時勾選 Modernizr.load 可同時獲得特性偵測、條件載入、HTML5 標籤修補三合一:

<script src="modernizr-custom.js"></script>
<script>
Modernizr.load({
  test: Modernizr.flexbox,
  nope: ['css/flexbox-fallback.css', 'js/flexbox-polyfill.js'],
  complete: () => { /* 後續初始化 */ }
});
</script>

現代專案棄用舊 IE 的決策清單

  • [ ] 專案需求文件明確移除 IE 支援條款
  • [ ] 分析工具 (GA/Matomo) 確認 IE 使用率 < 1% 且持續下降
  • [ ] 團隊共識:不再投入資源修復 IE 專屬 Bug
  • [ ] 移除 html5shiv, selectivizr, respond.js, 條件註解、IE Hack CSS
  • [ ] 更新 browserslist 設定:">0.5%", "not dead", "not ie 11"
  • [ ] CI/CD 加入 eslint-plugin-compat、stylelint-no-unsupported-browser-features 防止誤用新特性
  • [ ] 文件化遷移紀錄,供未來維護參考

結語

「讓舊版 IE 正常運行」的本質是**以最小成本彌補關鍵功能缺口**,而非追求視覺像素級一致。html5shiv + 條件註解 CSS Hack 是歷經實戰驗證的黃金組合;Modernizr 提供更系統化的特性偵測與條件載入能力。隨著 IE 徹底退場,新專案應果斷擁抱現代標準,將資源投入創新體驗而非相容性維護。

copyright © 萬盛學電腦網 all rights reserved