核心結論:順序決定生死
a:link → a:visited → a:hover → a:active(Love-Hate 記憶法)。後定義覆蓋前定義,順序錯誤會導致狀態樣式失效(如 :hover 寫在 :link 前,訪問過的鏈接永遠不會顯示 Hover 效果)。
四大偽類語義與典型樣式
| 偽類 | 觸發條件 | 典型視覺反饋 | 無障礙要求 |
:link | 未訪問的鏈接 | 品牌色 + 下劃線 | 對比度 ≥ 4.5:1,非僅靠顏色區分(保留下劃線) |
:visited | 已訪問的鏈接 | 降低飽和度/改變色相(如紫色),保留下劃線 | 安全限制:getComputedStyle 只能讀取顏色,不可讀取幾何屬性 |
:hover | 鼠標指針懸停 | 顏色加深/背景色/下劃線動畫/圖標變化 | 非鼠標用戶不可見,關鍵信息勿僅靠 Hover 傳達 |
:active | 鼠標按下未釋放 | 縮放 0.98、背景加深、內陰影「按下感」 | 觸屏同樣觸發,提供即時反饋 |
:focus | 鍵盤聚焦 (Tab) | 明顯輪廓 outline: 2px solid #0066ff; outline-offset: 2px; | 必須可見,不可移除 outline: none 無替代方案 |
:focus-visible | 僅鍵盤/非鼠標聚焦 | 同 :focus,鼠標點擊不顯示 | 現代最佳實踐,平衡美觀與無障礙 |
完整鏈接樣式模板(生產級)
:root {
--link-color: #0066ff;
--link-visited: #6633cc;
--link-hover: #0052cc;
--link-active: #003d99;
--link-focus: #0066ff;
--link-underline: currentColor; /* 繼承文字色 */
}
a {
color: var(--link-color);
text-decoration: underline;
text-underline-offset: 2px; /* 下劃線與文字間距 */
text-decoration-thickness: 1px; /* 下劃線粗細 */
text-decoration-skip-ink: auto; /* 避開下行字母 (g, y, p) */
transition: color 0.15s ease, text-decoration-color 0.15s ease;
}
/* LVHA 順序嚴格遵守 */
a:link { color: var(--link-color); }
a:visited { color: var(--link-visited); }
a:hover {
color: var(--link-hover);
text-decoration-color: var(--link-hover); /* 下劃線同色 */
}
a:active { color: var(--link-active); }
/* 焦點樣式:獨立於 LVHA,優先級通常更高 */
a:focus-visible {
outline: 2px solid var(--link-focus);
outline-offset: 3px;
border-radius: 2px;
/* 不移除 text-decoration,保持語義 */
}
/* 禁用/非導航鏈接 */
a[aria-disabled="true"],
a[href=""] {
color: var(--color-text-disabled, #999);
pointer-events: none;
text-decoration: none;
cursor: not-allowed;
}
/* 圖標鏈接:外部鏈接圖標 */
a[href^="http"]:not([href*="mydomain.com"])::after {
content: "";
display: inline-block;
width: 1em; height: 1em;
margin-left: 0.25em;
background: url("data:image/svg+xml,") center/contain no-repeat;
vertical-align: middle;
transition: transform 0.2s ease;
}
a[href^="http"]:hover::after { transform: translate(2px, -2px); }
進階模式:按鈕化鏈接 vs 真按鈕
| 場景 | 元素 | 關鍵屬性 |
| 導航、跳轉、錨點 | <a href> | 語義正確、SEO、瀏覽器歷史、右鍵菜單「在新標籤打開」 |
| 表單提交、模態框觸發、JS 行為 | <button type="button"> | 無 href、語義操作、Enter/Space 觸發、表單提交 |
| 外觀像按鈕的導航 | <a href class="btn btn--primary"> | BEM 修飾符複用按鈕樣式,但語義仍是鏈接 |
安全與隱私::visited 限制
瀏覽器為防止歷史記錄洩露,嚴格限制 :visited 可用屬性:
- ✅ 允許:
color、background-color、border-color、outline-color、column-rule-color、text-decoration-color、text-emphasis-color、SVG fill/stroke
- ❌ 禁止:
font-size、display、visibility、transform、opacity、background-image、layout 相關屬性
getComputedStyle(link) 永遠返回 :link 計算值,JS 無法探測訪問歷史。
無障礙增強清單
- 可見焦點:
:focus-visible 提供清晰輪廓,outline-offset 防止貼邊。
- 語義區分:下載鏈接
download、外部鏈接 target="_blank" rel="noopener noreferrer" + 圖標提示、錨點 href="#id"。
- 觸控目標:移動端鏈接最小 44×44px,
padding: 12px 16px 或 display: inline-flex; align-items: center; min-height: 44px。
- 減少動效:
@media (prefers-reduced-motion: reduce) { a { transition: none; } }
- 高對比度模式:Windows High Contrast 強制系統色,保留
border/outline 作為邊界保障。
常見錯誤與修正
| 錯誤寫法 | 後果 | 修正 |
a:hover { } a:link { } | 未訪問鏈接 Hover 失效 | 嚴格 LVHA 順序 |
a { outline: none; } | 鍵盤用戶完全失焦,嚴重違反 WCAG | a:focus-visible { outline: ... } |
僅用顏色區分 :hover | 色盲/低視力用戶無法感知 | 增加 text-decoration/background/transform |
:visited 設 font-weight: bold | 瀏覽器忽略,無效果 | 僅用允許的色值屬性 |
「鏈接是 Web 的靈魂,偽類狀態機是用戶導航的指南針。LVHA 不是任意規則,而是『狀態互斥與繼承』邏輯的必然推導。」