CSS Pseudo-elements
CSS Pseudo-Elements
A CSS pseudo-element is a keyword that can be added to a selector, to style a specific part of an element.
Some common use for pseudo-elements:
- Style the first letter or first line, of an element
- Insert content before or after an element
- Style the markers of list items
- Style the user-selected portion of an element
- Style the viewbox behind a dialog box
Syntax
Pseudo-elements are denoted by a double colon (::) followed by the pseudo-element name:
CSS properties
}
The CSS ::first-line Pseudo-element
The ::first-line
pseudo-element is used to add a special style
to the first line of a text.
Note: The
::first-line
pseudo-element can only be applied to block-level
elements.
Example
Format the first line of text in all <p> elements:
color: red;
font-variant: small-caps;
font-size: 19px;
}
The CSS ::first-letter Pseudo-element
The ::first-letter
pseudo-element is used to add a special style to the first
letter of a text.
Note: The
::first-letter
pseudo-element can only be applied to block-level
elements.
Example
Format the first letter of the text in all <p> elements:
color: red;
font-size: xx-large;
}
The CSS ::before Pseudo-element
The ::before
pseudo-element
is used to insert some content before the content of a specified element.
Use the
content
property to specify the content to insert.
Example
Insert an image before the content of each <h3> element:
content: url(smiley.gif);
}
The CSS ::after Pseudo-element
The ::after
pseudo-element
is used to insert some content after the content of a specified element.
Use the
content
property to specify the content to insert.
Example
Insert an image after the content of each <h3> element:
content: url(smiley.gif);
}
The CSS ::marker Pseudo-element
The ::marker
pseudo-element
is used to style the list item markers.
Example
Style the markers of list items:
color: red;
font-size: 23px;
}
The CSS ::selection Pseudo-element
The ::selection
pseudo-element is used to style the part of a text that is selected by a user.
Example
Style the user-selected text with a red color, and a yellow background:
color: red;
background: yellow;
}
The CSS ::backdrop Pseudo-element
The ::backdrop
pseudo-element
is used to style the viewbox behind a dialog box or popover element..
Example
Style the viewbox behind a dialog box:
background-color: lightgreen;
}
Pseudo-elements and HTML Classes
Pseudo-elements can easily be combined with HTML classes.
Example
Display the first letter of <p> elements with class="intro", in red and in a larger size:
color: #ff0000;
font-size: 200%;
}
Multiple Pseudo-elements
Several pseudo-elements can also be combined.
In the following example, the first letter of <p> elements will be red and in an xx-large font size. The rest of the first line will be blue and in small-caps. The rest of the paragraph will be in the default font size and color:
Example
color: red;
font-size: xx-large;
}
p::first-line {
color: blue;
font-variant: small-caps;
}
CSS Pseudo-elements Reference
For a complete list of all CSS Pseudo-elements, visit our CSS Pseuodo-elements Reference.