Pseudo-classes and Pseudo-elements

Beginner · 6 min

The difference between selecting an element by state or position (pseudo-classes) and styling a part that has no tag of its own (pseudo-elements).

Before thisSelectors and Combinators

Kevin Powell · 6 min

Pseudo-classes and Pseudo-elements

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Kevin PowellReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A pseudo-class (one colon) selects an existing element based on its state or position: `:hover`, `:focus-visible`, `:checked`, `:first-child`, `:nth-child(2n+1)`.
  • A pseudo-element (two colons) styles a part of an element that has no tag, or inserts generated content: `::before`, `::after`, `::first-line`, `::placeholder`, `::marker`.
  • `::before` and `::after` draw nothing until `content` is set, even to `""`. The generated box does not exist in the DOM, so JavaScript cannot select it.
  • Use `:focus-visible` for focus rings: it shows for keyboard users and usually not on mouse clicks, while plain `:focus` shows in both cases.
  • `:not()` removes elements from a selection. It adds no specificity of its own, but takes the specificity of its argument.

Two ways to reach what HTML does not name

A selector can only target what exists as an element in the DOM. But a lot of styling needs to reach something the markup does not spell out. A link changes color while you hover it. The first item in a list needs no top border. The first letter of an article gets a drop cap. None of these has its own tag.

CSS covers the gap with two ideas that look similar and are different in kind.

Pseudo-classes: an element in a certain situation

A pseudo-class starts with one colon and is a condition on an element that already exists. It never creates anything. It narrows down a selection to elements that are in a particular state or position.

State:

a:hover { text-decoration: underline; }
button:disabled { opacity: 0.5; }
input:checked + label { font-weight: 600; }
input:required { border-color: crimson; }

Position in the tree:

li:first-child { margin-block-start: 0; }
tr:nth-child(even) { background: #f4f4f4; }
li:last-of-type { border-bottom: 0; }
p:empty { display: none; }

:nth-child(an+b) takes a formula. With n counting 0, 1, 2 and so on, 2n+1 gives 1, 3, 5 (odd), and 3n gives 3, 6, 9. I checked that li:nth-child(2n+1) colours items 1 and 3 and leaves 2 alone.

:not(...) inverts a condition: a:not([href]) is an anchor with no href. It adds no weight of its own to specificity, but counts as whatever is inside its parentheses, a point specificity picks up.

Focus: :focus versus :focus-visible

:focus matches an element that has keyboard focus, including a button you just clicked with a mouse. That is why people were tempted to write outline: none globally, and why that habit broke keyboards. :focus-visible matches only when the browser decides a focus indicator is helpful, which in practice means keyboard use. Style that one, and keep an outline on it.

:focus-visible {
  outline: 3px solid royalblue;
  outline-offset: 2px;
}

A caution about :hover

Touch screens have no hover. A style that only appears on :hover is never seen there, and on some phones a tap leaves the hover state stuck. Treat hover as an extra, not as the only signal. Wrapping hover styles in @media (hover: hover) limits them to devices that can hover.

Pseudo-elements: a part that has no tag

A pseudo-element starts with two colons and refers to a piece of an element, or to extra content the browser draws for you:

p::first-line { font-variant: small-caps; }
p::first-letter { font-size: 3em; float: left; }
input::placeholder { color: gray; }
li::marker { color: tomato; }
::selection { background: gold; }

The two you will write most are ::before and ::after. They insert a generated box as the first or last child of the element, and they need a content value:

.external::after {
  content: " ↗";
}
.card::before {
  content: "";            /* required, even if empty */
  display: block;
  height: 4px;
  background: tomato;
}

I tested this in Chromium. A ::before with a size and background but no content has zero height; add content: "" and the box appears at its set size.

Two consequences follow from the generated box not being in the DOM. JavaScript cannot select it with querySelector. And text placed in content is decoration, not real content, so never put something a user must read there. Icons and flourishes belong in ::before and ::after; labels, prices and warnings belong in HTML.

The old single-colon forms :before and :after still work for historical reasons. Use two colons so it is obvious at a glance which kind you are looking at.

The rule of thumb

Ask what you are selecting. An existing element in a state or place: one colon. A fragment of an element, or content the browser draws: two colons. Pseudo-classes can also stack: a:not(.btn):hover. A pseudo-element comes last in a selector, since nothing can be chained inside a fragment.

What the video covers

Kevin Powell’s “Do you know the difference? pseudo-classes vs pseudo-elements” is under six minutes and from August 2021. By its title it is about this exact distinction. The video has no caption data I could read, so I have not described its examples; everything above is the lesson’s own explanation and was run in Chromium. The runner-up from 2025 covers less common pseudo-elements.

Where this fits

This finishes the toolbox for picking elements. Next, specificity and the cascade answers what happens when several matching rules disagree. Later, :is, :where and :has adds the selectors that finally let CSS look at what an element contains. For a broader first pass over the cascade, see the frontend roadmap’s box model and cascade lesson.

Check yourself

  1. 01`a:hover` and `a::first-letter` both have a colon. What is the real difference?

    `:hover` is a pseudo-class: it picks the existing `a` elements that are currently hovered. `::first-letter` is a pseudo-element: it styles a fragment, the first letter of the text, that is not an element in the DOM at all.

  2. 02You write `.badge::after { background: gold; width: 1rem; height: 1rem; }` and nothing appears. Why?

    `::before` and `::after` only generate a box when `content` is set. Add `content: ""` and, since the generated box is inline by default, usually `display: inline-block` or `block` so the width and height apply.

  3. 03Why prefer `:focus-visible` over `:focus` for a custom focus outline?

    `:focus` matches whenever the element has focus, including after a mouse click on a button, which makes designers want to remove the outline. `:focus-visible` matches when the browser decides a visible indicator is needed, such as keyboard focus, so you can style it without hurting keyboard users.

  4. 04Which items does `li:nth-child(2n+1)` match in a list of five?

    Items 1, 3 and 5. `2n+1` produces 1, 3, 5, and so on as n counts 0, 1, 2. It is the same as `:nth-child(odd)`.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark