IE9 以下瀏覽器對 HTML5 新語意標籤 (header, footer, article, section, nav, aside, main, figure, figcaption, time, mark, details, summary 等) **不認識**,導致兩大問題:
本文提供**雙管齊下**的標準解決方案。
雖然 IE 不認識標籤,但若強制指定 display: block,**現代瀏覽器**會正確套用;IE9- 仍無法識別標籤本身,故**必須搭配 JS 註冊**。
/* CSS Reset 片段 - 放在所有樣式最前面 */
article, aside, details, figcaption, figure,
footer, header, hgroup, main, menu, nav, section, summary {
display: block;
}
/* 新表單元素 */
input[type="search"] { -webkit-appearance: textfield; }
在 **DOM Ready 之前** (放在 <head>) 呼叫 document.createElement('tagName'),IE 會將該標籤註冊為已知元素,後續解析器即能正確建立樹狀結構、套用 CSS。
<!--[if lt IE 9]>
<script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv-printshiv.min.js"></script>
<![endif]-->
jsdelivr.net、cdnjs.cloudflare.com,支援 HTTPS、HTTP/2、全球 CDN。// 安裝
npm install html5shiv --save
// 入口 JS 最頂部 (僅開發/建構時注入,生產環境由 HTML 模板條件註解處理)
// 或者使用 html-webpack-plugin 模板注入
if (process.env.NODE_ENV === 'development' && !('hidden' in document.createElement('div'))) {
require('html5shiv/dist/html5shiv-printshiv');
}
Modernizr 3.x 建置時勾選 html5shiv 選項,產出的腳本**同時包含特性偵測類別注入與 HTML5 標籤修補**。
<script src="modernizr-custom.js"></script>
<!-- 會在 <html> 加上 js, flexbox, canvas... 等 class,並自動執行 html5shiv -->
abbr, article, aside, audio, bdi, canvas, data, datalist,
details, dialog, figcaption, figure, footer, header, hgroup,
main, mark, meter, nav, output, picture, progress, rp, rt,
ruby, section, summary, time, video, wbr
<body> 底部、defer、async → **失效**。**必須在 <head> 同步執行**。container.innerHTML = '<section>...</section>' 在 IE9- 仍會失效。需用 html5shiv 內建的 html5shiv.elements() 或手動 document.createElement 建立後再 appendChild。article::before 在 IE9- **完全不支援**,無法用 Polyfill 修復。改用 <span class="before"> 或放棄。input[type="date"] 等在 IE 會降級為 text,需自行引入 Datepicker 庫 (如 flatpickr、datepicker.js)。css-vars-ponyfill) 或採用漸進增強寫法。browserslist: [">0.5%", "not dead", "not ie 11"],**完全移除 html5shiv 等 Polyfill**,專注現代 Web 標準開發。**CSS display: block + html5shiv (printshiv 版) + 條件註解** 是支援 IE9- 識別 HTML5 新標籤的**工業標準解法**。理解其原理 (document.createElement 註冊機制)、載入時機 (head 同步)、列印修正 (printshiv) 等細節,能確保舊環境穩定運行。隨著 IE 退場,新專案應大方移除相容包袱,擁抱原生 HTML5/CSS3/ES6+ 的開發效率與體驗紅利。