CSS 虛擬類別(pseudo-class)的元素,在特殊狀態下被選取的話,會作為關鍵字被加到選擇器裡面。例如 :hover
會讓用戶的滑鼠停在某個元素的時候,套用指定選擇器的樣式。
虛擬類別與 pseudo-elements
能讓你不只能給文件樹(document tree)本身的相關內容套用樣式,還能給諸如瀏覽歷史(:visited
)、內容的狀態(:checked
)、還有滑鼠的位置(像 :hover
就能讓偵測滑鼠是否在元件上)之類的外部相關因素套用樣式。
語法
selector:pseudo-class { property: value; }
基本虛擬類別的目錄
:active
:any
:checked
:default
:dir()
:disabled
:empty
:enabled
:first
:first-child
:first-of-type
:fullscreen
:focus
:hover
:indeterminate
:in-range
:invalid
:lang()
:last-child
:last-of-type
:left
:link
:not()
:nth-child()
:nth-last-child()
:nth-last-of-type()
:nth-of-type()
:only-child
:only-of-type
:optional
:out-of-range
:read-only
:read-write
:required
:right
:root
:scope
:target
:valid
:visited
規範
規範 | 狀態 | 註解 |
---|---|---|
Fullscreen API | Living Standard | 定義 :fullscreen 。 |
HTML Living Standard | Living Standard | 在 HTML5 並沒有改變。 |
Selectors Level 4 | Working Draft | 定義 :any-link , :local-link , :scope , :active-drop-target , :valid-drop-target , :invalid-drop-target , :current , :past , :future , :placeholder-shown , :user-error , :blank , :nth-match() , :nth-last-match() , :nth-column() , :nth-last-column() , and :matches() .沒有針對 Selectors Level 3 與 HTML5 虛擬類別的重大更改(though semantic meaning not taken over) |
HTML5 | Recommendation | 定義以下 HTML context 的語意含義: :link , :visited , :active , :enabled , :disabled , :checked , and :indeterminate .定義 :default , :valid , :invalid , :in-range , :out-of-range , :required , :optional , :read-only , :read-write , and :dir() . |
CSS Basic User Interface Module Level 3 | Recommendation | 定義 :default , :valid , :invalid , :in-range , :out-of-range , :required , :optional , :read-only , :read-write ,但沒有語意含義的定義。 |
Selectors Level 3 | Recommendation | 定義 :target , :root , :nth-child() , :nth-last-of-child() , :nth-of-type() , :nth-last-of-type() , :last-child , :first-of-type , :last-of-type , :only-child , :only-of-type , :empty , :not() .定義 :enabled , :disabled , :checked , :indeterminate 的語法,但沒有語意含義的定義。在 CSS Level 2 (Revision 1) 並沒有明顯改變。 |
CSS Level 2 (Revision 1) | Recommendation | 定義 :lang() , :first-child , :hover , :focus .在 CSS Level 1 並沒有明顯改變。 |
CSS Level 1 | Recommendation | 定義 :link , :visited , :active ,但沒有語意含義的定義。 |