/* 1. 未訪問 */
a:link { color: #0066cc; text-decoration: none; }
/* 2. 已訪問(隱私限制:只能改 color、background-color 等不影響佈局屬性) */
a:visited { color: #663399; }
/* 3. 滑鼠懸停 */
a:hover { color: #004499; text-decoration: underline; }
/* 4. 點擊瞬間 */
a:active { color: #cc0000; }
/* 5. 鍵盤焦點(無障礙關鍵,必須可見) */
a:focus-visible {
outline: 2px solid #0066cc;
outline-offset: 2px;
border-radius: 2px;
}
/* 現代寫法::where() 降低優先級,避免覆蓋困難 */
:where(a):link { color: var(--link-color, #0066cc); }
:where(a):visited { color: var(--link-visited, #663399); }
:where(a):hover { text-decoration: underline; }
:where(a):active { color: var(--link-active, #cc0000); }
:where(a):focus-visible { outline: 2px solid var(--link-color, #0066cc); outline-offset: 2px; }
:root {
--link-color: #1a73e8;
--link-hover: #1557b0;
--link-visited: #6e40c9;
--link-active: #c5221f;
--focus-ring: 2px solid var(--link-color);
}
a {
color: var(--link-color);
text-decoration: none;
transition: color 0.15s ease;
}
a:hover { color: var(--link-hover); text-decoration: underline; }
a:visited { color: var(--link-visited); }
a:active { color: var(--link-active); }
a:focus-visible { outline: var(--focus-ring); outline-offset: 2px; }
/* 文字連結:融入段落 */
.text-link {
color: var(--link-color);
text-underline-offset: 2px;
text-decoration-thickness: 1px;
}
.text-link:hover { text-decoration-thickness: 2px; }
/* 按鈕連結:獨立行動呼籲 */
.btn-link {
display: inline-flex; align-items: center; gap: 0.5rem;
padding: 0.625rem 1.25rem;
background: var(--link-color); color: white;
border-radius: 0.375rem; font-weight: 500;
text-decoration: none; white-space: nowrap;
}
.btn-link:hover { background: var(--link-hover); color: white; text-decoration: none; }
.btn-link:focus-visible { outline: var(--focus-ring); outline-offset: 2px; }
| 文字大小 | 最小對比度 | 範例通過組合(白底 #fff) |
|---|---|---|
| 一般文字(< 18.5px 粗體 / < 24px 正常) | 4.5:1 | `#0066cc` (5.9:1), `#1a73e8` (4.6:1) |
| 大字(≥ 18.5px 粗體 / ≥ 24px 正常) | 3:1 | `#0099cc` (3.1:1), `#2196f3` (3.1:1) |
| UI 元件邊框/焦點環 | 3:1 | 焦點環需對背景 3:1 |
**工具**:WebAIM Contrast Checker、Contrast Ratio、瀏覽器 DevTools 元素面板無障礙頁籤。
/***** 外部連結 *****/
a[href^="http"]:not([href*="yourdomain.com"])::after {
content: " ↗"; /* 或 SVG 圖示 */
font-size: 0.75em;
vertical-align: super;
margin-left: 0.25em;
opacity: 0.7;
}
/***** 下載連結 *****/
a[href$=".pdf"]::before,
a[download]::before {
content: "📄 "; /* 或圖示 */
margin-right: 0.25em;
}
/***** 郵件/電話連結 *****/
a[href^="mailto:"]::before { content: "📧 "; }
a[href^="tel:"]::before { content: "📞 "; }
/***** 錨點連結(頁內跳轉) *****/
a[href^="#"] { /* 可加平滑捲動 */ scroll-behavior: smooth; }
/* 文字底線位置與粗細控制 */
a {
text-underline-offset: 0.15em;
text-decoration-thickness: 0.08em; /* 或 1px, 2px */
text-decoration-skip-ink: auto; /* 底線避開下伸字母 g, y, p 等 */
}
/* 焦點不顯示在滑鼠點擊時(:focus-visible 已原生支援) */
/* 無需 JS polyfill */
/* 高對比度模式媒體查詢 */
@media (forced-colors: active) {
a { text-decoration: underline; } /* 強制底線 */
.btn-link { border: 2px solid; } /* 強制邊框 */
}
:root {
--link: #1a73e8; --link-hover: #1557b0; --link-visited: #6e40c9; --link-active: #c5221f;
--focus: 2px solid var(--link);
}
a { color: var(--link); text-decoration: none; text-underline-offset: 2px; }
a:hover { color: var(--link-hover); text-decoration: underline; }
a:visited { color: var(--link-visited); }
a:active { color: var(--link-active); }
a:focus-visible { outline: var(--focus); outline-offset: 2px; }
掌握連結四狀態、無障礙對比度、語意化區分與現代 CSS 技巧,打造專業、包容、美觀的連結體驗。