無障礙與 WCAG
語意化 HTML
<!-- 使用語意元素取代泛型 div -->
<header>
<nav aria-label="主要導航">
<ul>
<li><a href="/" aria-current="page">首頁</a></li>
<li><a href="/products">產品</a></li>
<li><a href="/about">關於我們</a></li>
</ul>
</nav>
</header>
<main>
<article>
<h1>產品詳細資訊</h1>
<section aria-labelledby="specs-heading">
<h2 id="specs-heading">規格</h2>
<dl>
<dt>重量</dt>
<dd>1.2 公斤</dd>
<dt>尺寸</dt>
<dd>30 x 20 x 10 公分</dd>
</dl>
</section>
</article>
</main>
<footer>
<p>© 2024 公司名稱</p>
</footer>
使用 <nav>、<main>、<article>、<section>、<aside> 取代 <div> 作為地標。螢幕閱讀器會利用這些元素來導覽頁面。
ARIA 模式
function Modal({ isOpen, onClose, title, children }) {
if (!isOpen) return null;
return (
<div
role="dialog"
aria-modal="true"
aria-labelledby="modal-title"
onKeyDown={(e) => e.key === "Escape" && onClose()}
>
<h2 id="modal-title">{title}</h2>
<div>{children}</div>
<button onClick={onClose} aria-label="關閉對話框">
<XIcon aria-hidden="true" />
</button>
</div>
);
}
function Tabs({ tabs, activeIndex, onChange }) {
return (
<div>
<div role="tablist" aria-label="設定區段">
{tabs.map((tab, i) => (
<button
key={tab.id}
role="tab"
id={`tab-${tab.id}`}
aria-selected={i === activeIndex}
aria-controls={`panel-${tab.id}`}
tabIndex={i === activeIndex ? 0 : -1}
onClick={() => onChange(i)}
onKeyDown={(e) => handleArrowKeys(e, i, tabs.length, onChange)}
>
{tab.label}
</button>
))}
</div>
{tabs.map((tab, i) => (
<div
key={tab.id}
role="tabpanel"
id={`panel-${tab.id}`}
aria-labelledby={`tab-${tab.id}`}
hidden={i !== activeIndex}
tabIndex={0}
>
{tab.content}
</div>
))}
</div>
);
}
鍵盤導航
function handleArrowKeys(
event: React.KeyboardEvent,
currentIndex: number,
totalItems: number,
onSelect: (index: number) => void
) {
let newIndex = currentIndex;
switch (event.key) {
case "ArrowRight":
case "ArrowDown":
newIndex = (currentIndex + 1) % totalItems;
break;
case "ArrowLeft":
case "ArrowUp":
newIndex = (currentIndex - 1 + totalItems) % totalItems;
break;
case "Home":
newIndex = 0;
break;
case "End":
newIndex = totalItems - 1;
break;
default:
return;
}
event.preventDefault();
onSelect(newIndex);
}
所有的互動元素都必須能透過鍵盤到達。使用 Tab 進行焦點導航,Enter/Space 進行觸發,箭頭鍵進行元件內導航。
表單無障礙
function SignupForm() {
return (
<form aria-labelledby="form-title" noValidate>
<h2 id="form-title">建立帳戶</h2>
<div>
<label htmlFor="email">電子郵件地址</label>
<input
id="email"
type="email"
required
aria-required="true"
aria-describedby="email-hint email-error"
aria-invalid={hasError ? "true" : undefined}
/>
<p id="email-hint">我們絕不會分享您的電子郵件。</p>
{hasError && (
<p id="email-error" role="alert">
請輸入有效的電子郵件地址。
</p>
)}
</div>
<button type="submit">建立帳戶</button>
</form>
);
}
色彩與對比度
:root {
--text-primary: #1a1a1a; /* 在白色背景上 15.3:1 */
--text-secondary: #595959; /* 在白色背景上 7.0:1 */
--text-on-primary: #ffffff; /* 確保在品牌色上達到 4.5:1 */
--border-focus: #0066cc; /* 明顯的焦點環 */
}
*:focus-visible {
outline: 3px solid var(--border-focus);
outline-offset: 2px;
}
.error-message {
color: #d32f2f;
/* 不要只依賴顏色傳達資訊 - 加入圖示或文字前綴 */
}
.error-message::before {
content: "錯誤:";
font-weight: bold;
}
WCAG AA 要求一般文字對比度為 4.5:1,大文字(18px 粗體或 24px 一般體)為 3:1。
反模式
- 使用
div和span作為可點擊元素,而非button或a - 移除焦點外框卻未提供替代指示器
- 僅靠顏色傳達資訊(紅色表示錯誤、綠色表示成功)
- 在元素已有可見文字標籤時仍使用
aria-label - 自動播放媒體但未提供暫停機制
- 缺少讓鍵盤使用者跳過導覽的連結
檢查清單
- 所有互動元素均可透過鍵盤操作(Tab、Enter、Escape、箭頭鍵)
- 使用語意化 HTML 地標(
nav、main、article、section) - 圖片具備描述性的
alt文字(裝飾性圖片則設為alt="") - 色彩對比度符合 WCAG AA 標準(一般文字 4.5:1,大文字 3:1)
- 所有互動元素皆有可見的焦點指示器
- 表單輸入欄位已關聯對應的
<label>元素 - 錯誤訊息透過
role="alert"向螢幕閱讀器宣告 - 使用螢幕閱讀器(VoiceOver、NVDA)及僅鍵盤模式測試頁面