# Pseudo-classes & Pseudo-elements

> Style elements based on their state or position, and generate virtual content, all without touching HTML.

Subject: CSS | Lesson 4 of 44 | 5 min read
Interactive lesson: https://codewithanimation.in/css/pseudo-classes-and-elements
Previous: [Selectors & Combinators](https://codewithanimation.in/css/selectors-and-combinators.md) | Next: [The Cascade, Specificity & Inheritance](https://codewithanimation.in/css/cascade-specificity-inheritance.md)

## Two kinds of virtual targeting

**Pseudo-classes** select a real element based on its current state or its position in the document tree. They use a single colon: `:hover`, `:focus`, `:checked`, `:nth-child(2)`. No new HTML is written; the browser applies the rule dynamically as state changes.

**Pseudo-elements** create a virtual sub-part of an element that you can style or inject content into. They use a double colon: `::before`, `::after`, `::first-line`, `::placeholder`. The `::before` and `::after` pseudo-elements insert a generated content box directly before or after an element's existing content. They require a `content:` declaration (even `content: ''` for decorative shapes).

The historical single-colon syntax for pseudo-elements (`:before`, `:after`) still works in most browsers but `::` is the standard going forward. Pseudo-classes always use a single colon.

## State and structure without extra HTML

Without pseudo-classes you would need JavaScript to add a `.hovered` class on every mouseover. Pseudo-classes let CSS handle state natively: `:hover`, `:focus`, `:active`, `:checked` are browser-native and always accurate.

Pseudo-elements let you add decorative content (arrows, badges, underline bars) entirely in CSS without polluting the HTML with presentational `<span>` tags. A CSS-only tooltip, a custom list bullet, or a quote decoration are all achievable with `::before` / `::after`.

## Common pseudo-classes and pseudo-elements

**State pseudo-classes:** `:hover` (mouse over), `:focus` (keyboard or click focus), `:active` (mouse button held), `:visited` (link visited), `:checked` (checkbox or radio), `:disabled`, `:required`, `:valid`, `:invalid`.

**Structural pseudo-classes:** `:first-child`, `:last-child`, `:nth-child(n)` (e.g. `li:nth-child(2n)` for even items), `:nth-of-type(n)`, `:only-child`, `:not(selector)` (e.g. `li:not(:last-child)`).

**Pseudo-elements:** `::before` and `::after` (generated content, require `content:`), `::first-line` (first line of text), `::first-letter` (drop cap), `::placeholder` (input placeholder text), `::selection` (user-selected text highlight), `::marker` (list item bullet or number).

Combine with selectors freely: `a:hover::after` adds content after a link while it is hovered. `input:focus::placeholder` styles the placeholder only while the input is focused.

## Common mistake: Forgetting `content:` on ::before / ::after

A `::before` or `::after` pseudo-element with no `content:` property does not render at all. The most common fix is to add `content: ''` (empty string), which creates a zero-width inline box that you can then size and position. The property is mandatory even when you have no text to inject.

## Tip: Use :is() and :where() to group selectors

`:is(h1, h2, h3):hover` is equivalent to `h1:hover, h2:hover, h3:hover` but is shorter and easier to maintain. `:where()` works the same way but contributes zero specificity, making it useful for resets and defaults that should be easy to override.

## Try it: Try it

Toggle pseudo-class and pseudo-element chips to see each one applied to a live sample element.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/css/pseudo-classes-and-elements

## Check yourself

### Question 1

Which pseudo-element creates a box before an element's content without adding HTML?

- A. `::before`
- B. `:first-child`
- C. `::placeholder`
- D. `:hover`

Correct answer: A

`::before` inserts a generated content box immediately before the element's real content. It requires a `content:` property (even `content: ''`) to render.

- Why not B: `:first-child` is a pseudo-class that selects the first child element. It does not create new content.
- Why not C: `::placeholder` styles the placeholder text of an `<input>`, it does not insert content before the element.
- Why not D: `:hover` selects an element when the pointer is over it. It does not create content.

### Question 2

Which CSS targets every even `<li>` in a list?

- A. `li:nth-child(2n)`
- B. `li:even`
- C. `li:last-child`
- D. `li:nth-child(odd)`

Correct answer: A

`li:nth-child(2n)` matches every second `<li>` (2nd, 4th, 6th...). The keyword `even` is shorthand for the same thing: `li:nth-child(even)`.

- Why not B: `:even` is not a valid CSS pseudo-class. The correct syntax is `:nth-child(even)` or `:nth-child(2n)`.
- Why not C: `:last-child` only targets the final `<li>`, not all even ones.
- Why not D: `:nth-child(odd)` targets 1st, 3rd, 5th... items, not even ones.

### Question 3

What does `:not(.active)` select?

- A. Every element that does NOT have the class `active`
- B. Every element with the class `active`
- C. Only `<button>` elements without `active`
- D. It is invalid CSS

Correct answer: A

`:not(selector)` is the negation pseudo-class. It selects elements that do NOT match the argument. `li:not(.active)` targets all `<li>` elements that lack the `active` class.

- Why not B: That would be `.active` (without `:not`). `:not()` inverts the match.
- Why not C: `:not(.active)` applies to all elements. Narrow it with a type selector: `button:not(.active)`.
- Why not D: `:not()` is valid CSS3+ and widely supported. It accepts any simple selector as its argument.

## Remember this

- Pseudo-classes (`:hover`, `:focus`, `:nth-child`) select real elements by state or position.
- Pseudo-elements (`::before`, `::after`) create virtual sub-parts. Always include `content:` on `::before`/`::after`.
- Double colon `::` is the modern standard for pseudo-elements; single colon is legacy.
- `:not()`, `:is()`, `:where()` group or negate selectors cleanly without extra HTML.
