萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> HTML5 教學

HTML 連結顏色與狀態樣式完全指南:CSS 偽類、無障礙對比度與現代最佳實踐

連結四大狀態偽類(LVHA 順序)

/* 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; }

CSS 變數主題化範例

: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; }

按鈕樣式連結(CTA)與文字連結區分

/* 文字連結:融入段落 */
.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; }

無障礙對比度要求(WCAG 2.1 AA)

文字大小最小對比度範例通過組合(白底 #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; }

現代 CSS 新功能增強

/* 文字底線位置與粗細控制 */
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; }  /* 強制邊框 */
}

避坑指南

  • **不要移除 `outline` 而不提供替代焦點樣式** — 鍵盤使用者會迷失。
  • **`a:visited` 隱私限制**:無法用 `getComputedStyle` 讀取真實顏色,`background-image`、`border` 等屬性不生效。
  • **`target="_blank"` 必加 `rel="noopener noreferrer"`** — 防止 `window.opener` 魔術導航攻擊。
  • **連結文字勿只用「這裡」、「更多」** — 螢幕閱讀器用戶聽不出上下文。
  • **高對比度模式測試** — Windows 高對比度、macOS 增加對比度、瀏覽器強制深色模式。

快速複製:最小可行性連結樣式包

: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 技巧,打造專業、包容、美觀的連結體驗。

copyright © 萬盛學電腦網 all rights reserved