Pseudo-classes vs Pseudo-elements in CSS

A pseudo-class (:hover) targets an element in a certain state; a pseudo-element (::before) targets a sub-part of an element that doesn't exist in the DOM.

Published September 22, 2026

A pseudo-class (single colon, like :hover) selects an element in a particular state; a pseudo-element (double colon, like ::before) selects a part of an element or creates a virtual one.

The concept

Pseudo-classes depend on state or position: :hover, :focus, :first-child, :nth-child(2), :disabled.

Pseudo-elements target sub-parts: ::before and ::after insert generated content, ::first-line and ::first-letter style text pieces, ::placeholder and ::selection style UI parts.

State vs part

a:hover → the link, while hovered (state)
li:first-child → the first li (position)
p::first-line → just the first line of p (part)
h2::after → a generated node after h2 (new)

Practical examples

Hover and focus

button:hover { background: #2563eb; }
input:focus-visible { outline: 2px solid #2563eb; }

Add an icon with ::before

.external::after { content: ' ↗'; }

Zebra rows

tr:nth-child(even) { background: #f3f4f6; }

Drop cap

p::first-letter { font-size: 3em; float: left; }

Common mistakes

  • Forgetting content: '' on ::before/::after so nothing renders.
  • Using :before in new code — prefer ::before.
  • Trying to style ::before on replaced elements like img or input.

Key takeaways

  • Pseudo-class = state; pseudo-element = part.
  • ::before/::after need content.
  • Single colon vs double colon.

FAQ

Can I chain them?

Yes: a:hover::after.

Are pseudo-elements in the DOM?

No, they are rendered but not selectable or accessible as nodes.

More CSS articles