CSS pseudo-elements are powerful selectors that allow you to style specific parts of an element without adding extra markup to your HTML. They provide a way to insert content before or after an element, or to target the first letter or line of text.
Pseudo-elements are denoted by double colons (::) followed by the pseudo-element name. The basic syntax is:
selector::pseudo-element {
property: value;
}
Common pseudo-elements include:
These pseudo-elements insert content before or after an element's content:
blockquote::before {
content: "\201C";
font-size: 3em;
color: #888;
}
blockquote::after {
content: "\201D";
font-size: 3em;
color: #888;
}
Style the first letter or line of text in an element:
p::first-letter {
font-size: 2em;
font-weight: bold;
color: #4a4a4a;
}
p::first-line {
font-style: italic;
}
Most modern browsers support CSS pseudo-elements. However, for maximum compatibility, consider using single colons (:) instead of double colons (::) for older browsers like Internet Explorer 8.
To further enhance your CSS skills, explore these related topics:
CSS pseudo-elements are a powerful tool for adding style and content to your web pages without modifying the HTML structure. By mastering these selectors, you can create more dynamic and visually appealing designs while maintaining clean, semantic markup.