Some things you want to style are not separate elements in your HTML. A link being hovered over, the first item in a list, the first letter of a paragraph. CSS handles these with two special kinds of selector.
Pseudo-classes: a state or position
A pseudo-class starts with one colon and describes a state or a position.
<style>
a:hover { color: #d94a00; }
li:first-child { font-weight: bold; }
</style>
<ul>
<li>First item</li>
<li>Second item</li>
</ul>
<a href="#">Hover over this link</a>
Pseudo-elements: a part of an element
A pseudo-element starts with two colons and styles a part of an element, or adds generated content.
<style>
p::first-letter { font-size: 2.2em; color: #d94a00; }
h2::after { content: " →"; color: #d94a00; }
</style>
<h2>Next chapter</h2>
<p>The first letter of this paragraph is larger.</p>
Quick comparison
| Pseudo-class | Pseudo-element | |
|---|---|---|
| Colons | : | :: |
| Styles | An element in a state | A part of an element |
| Example | :hover, :nth-child(2) | ::before, ::first-line |
Later in the course there are full chapters for both: start with Hover, Active and Visited and ::before and ::after.