CSS Pseudo-elements
A CSS pseudo-element is used to style specified parts of an element.
For example, a pseudo-element can be used to:
- Style the first letter or line of an element.
- Insert content before or after the content of an element.
The syntax of a pseudo-element is: selector::pseudo-element { property: value; }. Note the double colon :: which distinguishes pseudo-elements from pseudo-classes.
::first-line and ::first-letter
The ::first-line pseudo-element is used to add a special style to the first line of a text. The ::first-letter pseudo-element is used to add a special style to the first letter of a text.
Example
css
p::first-line {
font-weight: bold;
}
p::first-letter {
color: #ff0000;
font-size: xx-large;
}::before and ::after
The ::before and ::after pseudo-elements can be used to insert some content before or after the content of an element. The content property is required for these to work.
Example: Adding an image before a heading
css
h1::before {
content: url(smiley.gif);
}Test Yourself with an Exercise
Which pseudo-element is used to insert content before the content of an element?