A CSS
pseudo-element
is a keyword added to a selector that lets you style a specific part of the selected element(s). For example,
::first-line
can be used to change the font of the first line of a paragraph.
/* The first line of every <p> element. */
p::first-line {
color: blue;
text-transform: uppercase;
}
注意:
In contrast to pseudo-elements,
pseudo-classes
can be used to style an element based on its
state
.
selector::pseudo-element {
property: value;
}
You can use only one pseudo-element in a selector. It must appear after the simple selectors in the statement.
注意:
As a rule, double colons (
::
) should be used instead of a single colon (
:
). This distinguishes pseudo-classes from pseudo-elements. However, since this distinction was not present in older versions of the W3C spec, most browsers support both syntaxes for the original pseudo-elements.
Pseudo-elements defined by a set of CSS specifications include the following:
| 规范 | 状态 | 注释 |
|---|---|---|
| CSS Level 1 | 推荐 | Defined pseudo-classes and pseudo-elements. |
| Browser | Lowest Version | Support of |
|---|---|---|
| Internet Explorer | 8.0 |
:pseudo-element
|
| 9.0 |
:pseudo-element ::pseudo-element
|
|
| Firefox (Gecko) | 1.0 (1.0) |
:pseudo-element
|
| 1.0 (1.5) |
:pseudo-element ::pseudo-element
|
|
| Opera | 4.0 |
:pseudo-element
|
| 7.0 |
:pseudo-element ::pseudo-element
|
|
| Safari (WebKit) | 1.0 (85) |
:pseudo-element ::pseudo-element
|
: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