萬盛學電腦網

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

CSS 偽選擇器完整指南:Link、Visited、Hover、Active 狀態樣式

什麼是偽選擇器?

CSS 偽選擇器用於定義元素在特定狀態下的樣式,最常見的是超連結(<a>)的四種狀態:

四大連結狀態偽類

<style type="text/css">
a:link    { color: #FF0000; }  /* 正常未訪問狀態 */
a:visited { color: #0000FF; }  /* 已訪問狀態 */
a:hover   { color: #00FF00; }  /* 滑鼠懸停狀態 */
a:active  { color: #FF00FF; }  /* 點擊按下瞬間 */
</style>

實際測試區

請嘗試互動下方連結觀察顏色變化:

正常連結 | 已訪問過的連結 | 懸停測試

撰寫順序原則:LVHA

為避免樣式被覆蓋,必須遵循 LoVe HAte 順序:

  1. :link
  2. :visited
  3. :hover
  4. :active

錯誤順序會導致 hover/active 樣式失效。

進階偽選擇器補充

選擇器說明範例
:focus元素獲得焦點input:focus { border-color: blue; }
:first-child父元素第一個子元素li:first-child { font-weight: bold; }
:nth-child(n)第 n 個子元素tr:nth-child(odd) { background: #f0f0f0; }
:before / :after插入內容.clearfix:after { content: ""; display: table; clear: both; }
:not(selector)排除符合選擇器input:not([type="submit"]) { width: 100%; }

相關推薦閱讀

copyright © 萬盛學電腦網 all rights reserved