a:link { color: #1a73e8; } /* 未訪問 */
a:visited { color: #6f42c1; } /* 已訪問 */
a:hover { color: #1557b0; text-decoration: underline; } /* 滑鼠懸停 */
a:active { color: #d93025; } /* 點擊瞬間 */
/* 必須按此順序寫入,否則後者會覆蓋前者 */
記憶口訣:**L**o**V**e **H**earts **A**nchors (LVHA)
| 偽類 | 觸發時機 | 典型用途 |
|---|---|---|
| :focus | 鍵盤/程式聚焦 | 無障礙焦點環 outline: 2px solid #1a73e8; outline-offset: 2px; |
| :focus-visible | 僅鍵盤聚焦顯示 (滑鼠點擊不顯示) | 現代最佳焦點樣式,避免滑鼠點擊出現難看外框 |
| :focus-within | 元素或其後代聚焦 | 表單欄位聚焦時高亮整個輸入區塊 |
| :any-link | 匹配所有 href 的 <a> (含 :link/:visited) | 統一套用連結基礎樣式 |
| :target | URL hash 對應的目標元素 | 錨點導航高亮、純 CSS 燈箱/側欄 |
:root {
--link-color: #1a73e8;
--link-visited: #6f42c1;
--link-hover: #1557b0;
--link-active: #d93025;
--link-focus: #1a73e8;
--link-underline: currentColor; /* 繼承文字色 */
}
a:any-link {
color: var(--link-color);
text-decoration: none;
text-underline-offset: 2px; /* 底線與文字間距 */
text-decoration-thickness: 2px; /* 底線粗細 */
transition: color 0.15s ease, text-decoration-color 0.15s ease;
}
a:visited { color: var(--link-visited); }
a:hover {
color: var(--link-hover);
text-decoration: underline;
text-decoration-color: var(--link-hover);
}
a:active { color: var(--link-active); }
a:focus-visible {
outline: 2px solid var(--link-focus);
outline-offset: 3px;
border-radius: 2px;
}
/* 禁用/不可互動連結 */
a[aria-disabled="true"],
a.disabled {
pointer-events: none;
color: var(--color-text-disabled);
text-decoration: none;
}
:focus-visible 必有明顯外框<a href> 導航、<button> 動作,勿混用getComputedStyle 總回傳未訪問色<a class="btn"> 當按鈕用,需提高優先級或用 :not(.btn) 排除a[href^="http"]:not([href*="mydomain"]):after { content: "↗"; margin-left: 0.3em; }