Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 139 ยท Selectors

Structural Pseudo-classes

Master :first-child, :last-child, :only-child, :nth-child() with an+b formulas and "of S", :nth-of-type() and :nth-last-child(), with an interactive explorer.

Some styles depend on position: the first item in a list, every other table row, the last card in a group, every third photo in a gallery. You could add classes like first or odd by hand, but they break the moment content is added, removed or reordered.

Structural pseudo-classes select elements by their position among their siblings, and the browser recalculates them automatically. This lesson covers the simple ones (:first-child, :last-child, :only-child), the powerful :nth-child() formula, the of S filter, and the -of-type variants that count only elements of one tag. An interactive explorer lets you test any formula.

:first-child, :last-child and :only-child

  • :first-child matches an element that is the first child of its parent.
  • :last-child matches an element that is the last child of its parent.
  • :only-child matches an element that has no siblings at all.

Only elements count as children here. Text and comments between them are ignored.

First, last and only

The second list's item matches all three pseudo-classes at once, because a lone child is both first and last.

A common mistake is reading li:first-child as "the first child of the li". It means "an li that is a first child". To style the first item inside a list, write ul > li:first-child (or just li:first-child), not ul:first-child.

:nth-child(): selecting by formula

:nth-child() takes an argument that describes which positions to match. Positions are counted from 1.

The simplest arguments are a number or a keyword:

Selector Matches
li:nth-child(3) the third child
tr:nth-child(odd) children 1, 3, 5, โ€ฆ
tr:nth-child(even) children 2, 4, 6, โ€ฆ

The an+b formula

For anything more interesting, use the form an+b. The browser tries n = 0, 1, 2, 3, โ€ฆ and matches each position the formula produces that is 1 or greater.

  • b is the starting position (an offset).
  • a is the step size: how far apart the matches are.

Some worked examples:

Formula n = 0, 1, 2, 3 โ€ฆ Matches
2n 0, 2, 4, 6 every 2nd: 2, 4, 6 (same as even)
2n+1 1, 3, 5, 7 1, 3, 5 (same as odd)
3n 0, 3, 6, 9 every 3rd: 3, 6, 9
3n+2 2, 5, 8, 11 2, 5, 8
n+4 4, 5, 6, 7 the 4th onward
-n+3 3, 2, 1, 0 only the first three

The negative form is the clever one. -n+3 counts down from 3 as n grows, so it matches positions 3, 2 and 1, which means "the first three". Combine two formulas to select a range:

CSS
/* Items 3 through 6 */
li:nth-child(n+3):nth-child(-n+6) { โ€ฆ }

The first part says "3 or later", the second says "6 or earlier". Both must be true.

Try the :nth-child() explorer

Type any argument and watch which of the 20 boxes match. Try the presets, then make up your own formulas.

:nth-child() explorer

The explorer passes your formula straight to the browser's own selector engine with querySelectorAll(), so what you see is exactly how CSS would match it.

Filtering with "of S"

Normally :nth-child() counts all siblings, whatever they are. Since 2023, every major browser also supports :nth-child(an+b of S), which counts only the siblings that match the selector S.

That matters when some items are hidden or filtered:

CSS
/* Zebra stripes on visible rows only */
tr:nth-child(odd of :not(.hidden)) {
  background: #f1f5f9;
}

With plain tr:nth-child(odd), hiding a row would throw the stripes out of step, since hidden rows still count. With of :not(.hidden), the counting skips them.

Counting only some siblings with of S

Notice :nth-child(1 of :not(.done)): "the first item that isn't done". That is something :first-child alone can't express.

:nth-of-type() and friends

The -of-type variants count only siblings with the same tag name as the element:

  • :first-of-type and :last-of-type
  • :only-of-type
  • :nth-of-type(an+b)

They help when a parent has mixed children. In an article, p:first-child fails if a heading comes first. p:first-of-type matches the first paragraph, whatever comes before it.

nth-child vs nth-of-type

The -of-type selectors only look at the tag name. .intro:first-of-type does not mean "the first element with class intro". It means "an element with class intro that is also the first of its tag type". For "the first .intro", use :nth-child(1 of .intro).

Counting from the end

Every nth pseudo-class has a twin that counts backwards from the last sibling:

  • :nth-last-child(an+b)
  • :nth-last-of-type(an+b)
CSS
li:nth-last-child(-n+2) { โ€ฆ }   /* the last two items */
li:nth-last-child(2) { โ€ฆ }      /* the second to last */

A neat trick combines both directions to style a group by how many items it has. li:first-child:nth-last-child(4) matches a first item that is also fourth from the end, which only happens when there are exactly four items. Pair it with the ~ combinator to reach the rest:

CSS
/* When a list has exactly four items, lay them out 2 ร— 2 */
li:first-child:nth-last-child(4),
li:first-child:nth-last-child(4) ~ li {
  width: 50%;
}

Today, :has() can express this more readably (ul:has(> li:nth-child(4):last-child)), as you'll see in the selector functions lesson.

Specificity

Each structural pseudo-class counts like a class: li:nth-child(2n) is (0, 1, 1). With of S, the most specific selector inside S is added on top, so li:nth-child(odd of .done) is (0, 2, 1).

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which positions does :nth-child(3n+1) match?

    With n = 0, 1, 2, 3 the formula gives 1, 4, 7, 10. The match at n = 0 is position 1.
  2. 2. How do you select only the first three items of a list?

    -n+3 produces 3, 2, 1, 0, โ€ฆ so it matches positions 1 to 3.
  3. 3. An <article> starts with an <h2>, then two <p> elements. Which selector matches the first paragraph?

    The paragraph isn't the first child (the h2 is), but it is the first <p> among its siblings.
  4. 4. What does tr:nth-child(even of .visible) do?

    The of S filter changes what is counted: only siblings matching .visible are numbered.
  5. 5. What does li:nth-last-child(1) match?

    Counting from the end, position 1 is the last child, so it is equivalent to :last-child.

Summary

  • :first-child, :last-child and :only-child match elements by position among element siblings.
  • :nth-child(an+b) tests positions aยทn + b for n = 0, 1, 2โ€ฆ; odd, even and plain numbers also work.
  • Negative a (as in -n+3) selects "the first few"; chaining two formulas selects a range.
  • :nth-child(an+b of S) counts only siblings that match S.
  • -of-type variants count siblings with the same tag name only, never classes.
  • :nth-last-child() and :nth-last-of-type() count from the end.

Next: Pseudo-elements โ€” styling parts of elements, and adding decorative content with ::before and ::after.