A
CSS
pseudo-class
is a keyword added to a selector that specifies a special state of the selected element(s).
例如,
:hover
can be used to change a button's color when the user's pointer hovers over it.
/* Any button over which the user's pointer is hovering */
button:hover {
color: blue;
}
Pseudo-classes let you apply a style to an element not only in relation to the content of the document tree, but also in relation to external factors like the history of the navigator (
:visited
, for example), the status of its content (like
:checked
on certain form elements), or the position of the mouse (like
:hover
, which lets you know if the mouse is over an element or not).
注意: In contrast to pseudo-classes, pseudo-elements can be used to style a specific part of an element.
selector:pseudo-class {
property: value;
}
Like regular classes, you can chain together as many pseudo-classes as you want in a selector.
Pseudo-classes defined by a set of CSS specifications include the following:
| 规范 | 状态 | 注释 |
|---|---|---|
| 全屏 API | 实时标准 |
Defined
:fullscreen
.
|
| HTML 实时标准 | 实时标准 | Defines when particular selectors match HTML elements. |
| Selectors Level 4 | 工作草案 |
Defined
:any-link
,
:blank
,
:local-link
,
:scope
,
:drop
,
:current
,
:past
,
:future
,
:placeholder-shown
,
:user-invalid
,
:nth-col()
,
:nth-last-col()
,
:is()
and
:where()
.
Changed
:empty
to behave like
:-moz-only-whitespace
.
No significant change for other pseudo-classes defined in Selectors Level 3 and HTML5 (though semantic meaning not taken over). |
| HTML5 | 推荐 | Copies the relevant section from the canonical (WHATWG) HTML spec. |
| CSS Basic User Interface Module Level 3 | 推荐 |
Defined
:default
,
:valid
,
:invalid
,
:in-range
,
:out-of-range
,
:required
,
:optional
,
:read-only
and
:read-write
, but without the associated semantic meaning.
|
| Selectors Level 3 | 推荐 |
Defined
: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
and
:not()
.
Defined the syntax of
:enabled
,
:disabled
,
:checked
,和
:indeterminate
, but without the associated semantic meaning.
No significant change for pseudo-classes defined in CSS Level 2 (Revision 1) . |
| CSS Level 2 (Revision 1) | 推荐 |
Defined
:lang()
,
:first-child
,
:hover
,和
:focus
.
No significant change for pseudo-classes defined in CSS Level 1 . |
| CSS Level 1 | 推荐 |
Defined
:link
,
:visited
and
:active
, but without the associated semantic meaning.
|
:active
:any-link
:checked
:blank
:default
:defined
:dir()
:disabled
:empty
:enabled
:first
:first-child
:first-of-type
:fullscreen
:focus
:focus-visible
:focus-within
:has()
:host()
:host-context()
:hover
:indeterminate
:in-range
:invalid
:is() (:matches(), :any())
: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
:placeholder-shown
:read-only
:read-write
:required
:right
:root
:scope
:target
:valid
:visited
:where()
::-moz-progress-bar
::-moz-range-progress
::-moz-range-thumb
::-moz-range-track
::-webkit-progress-bar
::-webkit-progress-value
::-webkit-slider-runnable-track
::-webkit-slider-thumb
::after (:after)
::backdrop
::before (:before)
::cue
::cue-region
::first-letter (:first-letter)
::first-line (:first-line)
::grammar-error
::marker
::part()
::placeholder
::selection
::slotted()
::spelling-error