Sub topic 7
Pseudo-elements
This chapter assumes you know basic selectors (type, class, ID), how a CSS rule is built, and how pseudo-classes such as :hover work. Pseudo-elements look similar but do a different job.
What a pseudo-element is
A pseudo-element lets you style a part of an element that has no tag of its own in the HTML. Examples are the first line of a paragraph or a decorative marker you add before a heading. You write it with a double colon: ::first-line, ::before.
A pseudo-class selects an element in a certain state, such as a:hover. A pseudo-element selects a piece of an element, such as p::first-line. The browser treats that piece as if it were an extra element.
Older CSS used a single colon for four of them (:before, :after, :first-line, :first-letter). Browsers still accept that form, but use the double colon in new code. It keeps pseudo-elements visibly separate from pseudo-classes.
Common pseudo-elements
| Pseudo-element | What it selects |
|---|---|
| `::before` | Generated content placed before an element's content |
| `::after` | Generated content placed after an element's content |
| `::first-line` | The first formatted line of a block |
| `::first-letter` | The first letter of a block |
| `::marker` | The bullet or number of a list item |
| `::placeholder` | Placeholder text in an input |
| `::selection` | Text the user has highlighted |
Generated content with ::before and ::after
::before and ::after create a new box inside the element, as its first or last child. They only appear if you set the content property. Without content, nothing is rendered.
a[target="_blank"]::after {
content: " ↗";
color: #555;
}
h2::before {
content: "";
display: inline-block;
width: 0.5rem;
height: 0.5rem;
margin-right: 0.5rem;
background: teal;
}The first rule adds an arrow after external links. The second uses an empty string, which is the usual way to draw a purely decorative shape. Generated content is not part of the HTML, so do not use it for meaningful text. Screen readers may or may not announce it, and users cannot select or search it reliably.
Styling parts of text
p::first-line { font-weight: bold; }
p::first-letter { font-size: 2em; }
li::marker { color: teal; }
input::placeholder { color: #888; }
::selection { background: gold; }Only a limited set of properties works on some of these. ::first-line and ::first-letter accept mainly font, color, and text properties, not arbitrary layout properties.
Specificity
A pseudo-element counts in the third column of the specificity triple, the same as a type selector. A pseudo-class counts in the second column, the same as a class.
p { color: black; } /* (0,0,1) */
p::first-line { color: green; } /* (0,0,2) */
p:hover::first-line { color: red; } /* (0,1,2) */An element can match only one pseudo-element of a given kind at a time, and a selector can contain only one pseudo-element, which must come last. A rule like p::first-line:hover is invalid for ordinary use, while p:hover::first-line is fine.
Common errors
- Forgetting
contenton::beforeor::after, so the rule does nothing. - Using
::beforeon elements that cannot have children, such asimgorinput. Those replaced elements usually do not render generated content. - Putting the pseudo-class after the pseudo-element.
- Expecting
::first-letterto style an inline element or the first letter of a flex container's text.
Exercise
Why does .tag::before { color: red; } display nothing, and what is the specificity of ul li.active::after?
Show answer
It displays nothing because ::before generates a box only when content is set, so you need something like content: ""; or a string. The specificity of ul li.active::after is (0,1,3): one class, plus two type selectors and one pseudo-element.