傳統 DIV 嵌套問題
<div id="header">
<div id="title">網站標題</div>
<div id="nav">導航選單</div>
</div>
/* 問題:無語意、嵌套過深、SEO 權重分散、無障礙差 */
HTML5 語意化重構
<header class="site-header" role="banner">
<div class="container">
<h1 class="site-title">
<a href="/" aria-label="回首頁">網站名稱</a>
</h1>
<nav class="main-nav" role="navigation" aria-label="主要導航">
<ul class="nav-list">
<li><a href="/about">關於我們</a></li>
<li><a href="/products">產品介紹</a></li>
<li><a href="/contact">聯絡我們</a></li>
</ul>
</nav>
<div class="header-actions">
<button class="btn-search" aria-label="搜尋" aria-expanded="false" aria-controls="search-panel">
<svg aria-hidden="true">...</svg>
</button>
<a class="btn-login" href="/login">登入</a>
</div>
</div>
</header>
關鍵優化點解析
1. 標題層級正確
- 每頁唯一 `
` 作為網站/頁面主標題
- 導航不使用標題標籤,使用 `
- Logo 圖片加 `alt="網站名稱"`,非裝飾性
2. 導航無障礙屬性
- `role="navigation"` + `aria-label` 標識導航區塊
- 當前頁面 `` 標記
- 下拉選單 `aria-haspopup="true" aria-expanded="false"`
- 鍵盤可操作:Tab 移動、Enter/Space 展開、Esc 關閉
3. 搜尋表單無障礙
<form role="search" class="site-search">
<label for="search-input" class="sr-only">搜尋內容</label>
<input type="search" id="search-input" name="q"
placeholder="搜尋產品、文章..."
autocomplete="off"
aria-autocomplete="list"
aria-controls="search-suggestions">
<button type="submit" aria-label="執行搜尋">
<svg aria-hidden="true">...</svg>
</button>
</form>
4. 響應式導航 (Hamburger Menu)
<button class="nav-toggle"
aria-expanded="false"
aria-controls="main-nav"
aria-label="切換導航選單">
<span class="hamburger" aria-hidden="true"></span>
</button>
<nav id="main-nav" class="main-nav" aria-label="主要導航">...</nav>
/* CSS */
.nav-toggle { display: none; }
@media (max-width: 768px) {
.nav-toggle { display: flex; }
.main-nav {
position: absolute; top: 100%; left: 0; right: 0;
display: none; /* JS 控制 display/visibility */
}
.main-nav.is-open { display: block; }
}
CSS 架構建議
.site-header {
position: sticky;
top: 0;
z-index: 100;
background: white;
border-bottom: 1px solid #e5e7eb;
}
.container {
max-width: 1200px;
margin: 0 auto;
padding: 0 24px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 24px;
height: 64px;
}
.site-title {
font-size: 1.5rem;
font-weight: 700;
line-height: 1;
flex-shrink: 0;
}
.main-nav { flex: 1; display: flex; justify-content: flex-end; }
.nav-list { display: flex; gap: 32px; align-items: center; }
.nav-list a {
color: #374151;
font-weight: 500;
padding: 8px 0;
border-bottom: 2px solid transparent;
transition: color 0.15s, border-color 0.15s;
}
.nav-list a:hover,
.nav-list a[aria-current="page"] {
color: #2563eb;
border-color: #2563eb;
}
.header-actions { display: flex; align-items: center; gap: 16px; flex-shrink: 0; }
SEO 與效能優化
- 關鍵導航連結放在 HTML 前段,爬蟲優先抓取
- 避免 JS 渲染導航 (SSR/SSG 確保 HTML 含完整導航)
- Logo 使用 SVG 或適當尺寸 PNG,加入 `width`/`height` 防止 CLS
- 預載入關鍵字體、Hero 圖片:``
- Service Worker 快取 Shell (App Shell 模式)
常見錯誤避雷
| 錯誤 | 後果 | 修正 |
| 多個 h1 | SEO 權重分散、大綱混亂 | 頁面唯一 h1,區塊用 h2-h6 |
| 導航用 div+span | 螢幕閱讀器無法識別導航區塊 | 用 nav + ul/ol |
| 當前頁未標記 | 用戶迷向、SEO 內部連結權重弱 | aria-current="page" |
| 手機版導航不可鍵盤操作 | 無障礙違規、法律風險 | 完整鍵盤交互、焦點管理 |
| 搜尋無 label | 螢幕閱讀器無法識別用途 | sr-only label 或 aria-label |