超鏈接是 Web 的靈魂。本章系統講解四種偽類狀態、行內轉區塊技巧、CSS 純樣式按鈕製作、首字下沉排版,讓鏈接既美觀又具備極佳可用性。
| 偽類 | 觸發時機 | 典型用途 |
|---|---|---|
a:link | 未訪問鏈接 | 品牌色標識可點擊元素 |
a:visited | 已訪問鏈接 | 降低對比度/加刪除線,提示「已讀」 |
a:hover | 鼠標懸停 | 變色、下劃線、背景變化,增強可點擊暗示 |
a:active | 鼠標按下未釋放 | 按壓反饋(如內陰影、位移 1px) |
記憶口訣:「愛恨原則」— LoVe Hate Always — :link → :visited → :hover → :active 順序不可顛倒。
a:link { color: #06F; text-decoration: none; }
a:visited { color: #999; text-decoration: line-through; }
a:hover { color: #F00; text-decoration: underline; }
a:active { color: #F0F; }

默認 <a> 為行內元素,點擊區域僅限文字範圍。加上 display: block 後:
width/height/paddingtext-align: center; line-height: height 實現垂直水平居中a {
display: block;
width: 100px;
height: 30px;
line-height: 30px;
text-align: center;
background: #CCC;
color: #000;
text-decoration: none;
}
a:hover { background: #333; color: #FFF; }

利用背景圖片 + 文字分離,實現零圖片按鈕或單圖片多狀態按鈕。
準備兩張狀態圖:默認態 btn_bg.gif、懸停態 btn_bg_hover.gif
a.btn-register {
display: block;
width: 107px; height: 34px;
line-height: 2; text-align: center;
background: url(/upload/2010-08/14/014304_btn_bg.gif) no-repeat 0 0;
color: #d84700; font-size: 14px; font-weight: bold;
text-decoration: none; padding-top: 3px;
}
a.btn-register:hover {
background: url(/upload/2010-08/14/014304_btn_bg_hover.gif) no-repeat 0 0;
}

將多狀態圖片合併為一張,通過 background-position 定位,減少 HTTP 請求:
/* 單圖多態:默認 0 0,懸停 0 -34px,按下 0 -68px */
a.btn { background: url(sprite.png) no-repeat 0 0; width: 100px; height: 34px; }
a.btn:hover { background-position: 0 -34px; }
a.btn:active { background-position: 0 -68px; }
報紙雜誌式排版效果,利用 :first-letter 偽元素:
p:first-letter {
float: left;
font-size: 3em;
line-height: 1;
margin: 0.05em 0.1em 0 0;
color: #c00;
font-family: Georgia, serif;
}
注意:中文首字下沉需配合 float: left 且字體需包含大號漢字字形,否則回退字體會導致基線偏移。
標準之路提示:現代項目建議使用 <button> 或 <input type="button"> 做表單提交,保留 <a> 做導航跳轉,語義更清晰、無障礙支持更好。