A CSS pseudo-element is used to style specified parts of an element. For example, it can be used to style the first letter, or line, of an element, or to insert content before or after an element.
In CSS3, pseudo-elements use a double colon (::) to distinguish them from pseudo-classes (:). However, most browsers still support the single colon notation for backward compatibility.
These are used to create special typographical effects for paragraphs and text blocks.
The first letter and first line of this paragraph have special styling applied to them using CSS. This is great for an editorial or blog-style aesthetic.
p::first-letter {
font-size: 30px;
color: green;
}
p::first-line {
font-style: italic;
}
These are the most powerful pseudo-elements. They can be used to insert content before or after an element's actual content. You must include the content property for them to appear.
This paragraph has a star icon added before it and some text added after it using only CSS.
h1::before {
content: "Icon ";
color: red;
}
h1::after {
content: " [Link]";
color: blue;
}
This pseudo-element matches the portion of an element that is selected by the user (highlighted with the mouse).
::selection {
background: #ffb7b7;
}
::after with clear: both; is the famous way to implement a clearfix to prevent parent containers from collapsing when they contain floated children.