本文聚焦**語義結構元素**的實戰應用與兼容策略。
<!DOCTYPE html>
<html lang="zh-Hant">
<head>
<meta charset="utf-8">
<title>頁面標題</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles.css">
</head>
<body>
...
</body>
</html>
無需記憶冗長 DTD URL,IE6+ 皆進入標準模式。
包含:Logo、站點標題、導航、搜索、用戶菜單。**可多次使用** (頁面級、文章級、區塊級)。
<header role="banner">
<h1>站點標題</h1>
<nav>...</nav>
</header>
<article>
<header>
<h1>文章標題</h1>
<p class="meta">作者 · 日期</p>
</header>
...
</article>
**僅用於主要導航區塊** (主選單、側邊欄導航、麵包屑、分頁)。頁腳版權鏈接、側邊欄廣告鏈接**不應**用 `
<nav aria-label="主導航">
<ul>
<li><a href="/">首頁</a></li>
<li><a href="/about">關於</a></li>
</ul>
</nav>
**有主題標題** 的內容分組。**非通用容器** (樣式用 `
<section aria-labelledby="features-heading">
<h2 id="features-heading">核心功能</h2>
<ul>...</ul>
</section>
<section>
<h2>用戶評價</h2>
<article>...</article> <!-- 巢狀 article -->
<article>...</article>
</section>
可獨立發布/分發/聚合的內容:部落格文章、新聞、論壇帖子、產品卡片、評論。**可巢狀** (文章內的評論)。
<article>
<header><h1>標題</h1></header>
<div class="content">...</div>
<footer><time datetime="2024-01-15">1月15日</time></footer>
</article>
與主要內容**間接相關**:側邊欄、廣告、引用、標籤雲、相關鏈接、作者信息。**不應**包含主要導航。
<aside aria-labelledby="sidebar-heading">
<h2 id="sidebar-heading">側邊欄</h2>
<nav>...</nav> <!-- 次要導航可在 aside 內 -->
<div class="ads">...</div>
</aside>
包含:版權、作者、相關鏈接、聯繫方式、站點地圖。**可多次使用** (頁面級、文章級、區塊級)。
<footer role="contentinfo">
<p>© 2024 公司名稱. All rights reserved.</p>
<nav><a href="/privacy">隱私政策</a></nav>
</footer>
<body>
<header role="banner">...</header> <!-- 頁面頂部 -->
<nav role="navigation">...</nav> <!-- 主導航 -->
<main role="main"> <!-- 主內容區 (隱式 ARIA) -->
<article>
<header>...</header>
<div class="entry-content">...</div>
<footer>...</footer>
<section aria-labelledby="comments-heading">
<h2 id="comments-heading">評論</h2>
<article>...</article> <!-- 評論1 -->
<article>...</article> <!-- 評論2 -->
</section>
</article>
<aside role="complementary"> <!-- 側邊欄 -->
<section><h2>熱門文章</h2>...</section>
<section><h2>標籤雲</h2>...</section>
</aside>
</main>
<footer role="contentinfo">...</footer> <!-- 頁面底部 -->
</body>
HTML5 新元素在舊瀏覽器中默認 `display: inline`,**必須顯式設為塊級**:
/* HTML5 顯示重置 (包含在 Normalize.css / Modern CSS Reset 中) */
article, aside, details, figcaption, figure,
footer, header, hgroup, main, menu, nav, section {
display: block;
}
/* 可選:針對舊 IE (IE6-9) 啟用樣式支援 */
/* 需引入 html5shiv: */
/* 或現代打包工具自動注入 (vite-plugin-legacy, @babel/preset-env targets) */
HTML5 語義化元素不是裝飾,而是**內容結構的契約**。正確使用它們,能讓瀏覽器、搜尋引擎、螢幕閱讀器、開發工具、未來的你自己,都能精確理解頁面結構,構建更健壯、可維護、可訪問的 Web。