CSS :nth-of-type() Pseudo-Class

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The :nth-of-type() pseudo-class matches elements by their 1-based position among siblings of the same tag name. Other element types are skipped in that count.

Example: p:nth-of-type(2) is the second p, even if a div sits between paragraphs. Contrast with :nth-child(), which counts every sibling.

Baseline — widely available.

Supported across modern browsers since mid-2015 (CSS3 Selectors). Same odd / even / An+B patterns as the other :nth-* selectors.

CSS Syntax

syntax.css
:nth-of-type( <An+B> | even | odd)

Examples: p:nth-of-type(1), dd:nth-of-type(even), span:nth-of-type(-n+3).

Pattern Values

Pass one pattern; indices count same-tag siblings from the start:

PatternMatches (same type)Try it
odd / 2n+11st, 3rd, 5th… of that typeTry it
even / 2n2nd, 4th, 6th… of that typeTry it
2 (number)Only the 2nd of that typeTry it
-n+3First three of that typeTry it
1 / :first-of-typeFirst element of that typeShortcut

How to Use :nth-of-type()

Pick a tag, count only that tag from the start, then style.

  1. Use mixed content under one parent when other tags would break :nth-child().
  2. Choose the element type (p, span, dd, …).
  3. Add :nth-of-type(pattern) with odd, a number, or An+B.
  4. Remember: this is not “nth of class” — combine a class only as an extra filter on the type match.
PieceRequired?Role
Element typeYesDefines which siblings enter the count
PatternYesodd, even, number, or An+B
Shared parentYesSiblings must share one parent
Other tags—Present but ignored in the type count

Live Preview

Mixed siblings: only span nodes are counted from the start. Other tags stay unmarked.

Rule shown: span:nth-of-type(odd)

Mixed siblings
span A em (skipped) span B span C strong (skipped) span D

Highlighting odd spans (A, C)

CSS Examples

Same-type counting from the start, with mixed siblings where it matters. Use Try It Yourself for a live editor.

Example 1 — Odd of Type (Zebra)

Stripe paragraphs by type index — other tags do not matter.

CSS
<style>
  p:nth-of-type(odd) {
    background: #dbeafe;
  }
</style>

<article>
  <h2>Title</h2>
  <p>One</p>
  <p>Two</p>
  <p>Three</p>
</article>
Try It Yourself

How It Works

  1. Only p tags count. The h2 is ignored for type indices.
  2. One=1 (odd), Two=2 (even), Three=3 (odd).
  3. One and Three get the blue background.
  4. Same as 2n+1 among paragraphs.

Example 2 — Exact Type Index

A divider between paragraphs does not change the second p.

CSS
<style>
  p:nth-of-type(2) {
    color: #b91c1c;
    font-weight: 700;
  }
</style>

<div>
  <p>First</p>
  <hr>
  <p>Second</p>
  <p>Third</p>
</div>
Try It Yourself

How It Works

  1. Paragraphs: First=1, Second=2, Third=3. The hr is skipped.
  2. 2 matches “Second” — still the 2nd p.
  3. p:nth-child(2) would fail here because child 2 is the hr.
  4. Use of-type whenever mixed markup sits between like elements.

Example 3 — First Three of Type

Match a range with -n+3 among the same tag.

CSS
<style>
  span:nth-of-type(-n+3) {
    border-bottom: 3px solid #2563eb;
  }
</style>

<div>
  <span>A</span>
  <em>skip</em>
  <span>B</span>
  <span>C</span>
  <span>D</span>
</div>
Try It Yourself

How It Works

  1. Spans: A=1, B=2, C=3, D=4. The em is ignored.
  2. -n+3 matches A, B, and C.
  3. D stays plain — fourth of type.
  4. For the last three of type, use :nth-last-of-type(-n+3).

Edge Cases & Pitfalls

Most mix-ups come from expecting class-based counting or child indices.

vs child

Type ≠ child position

p:nth-of-type(2) is not always p:nth-child(2). Prefer of-type when other tags sit between matches.

no nth-class

Not “nth of class”

Counting uses the tag name. For filtered counting by class, use :nth-child(… of .class) where supported.

combine

Class + type still works

p.fancy:nth-of-type(odd) means “odd p that also has .fancy” — the odd count is still among all p tags.

shortcut

(1) equals :first-of-type

Prefer :first-of-type when you only need the first same-tag sibling.

Browser Support

:nth-of-type() is a browser CSS pseudo-class. Node.js, Deno, and Bun do not apply it.

✓ Baseline · Widely available

:nth-of-type()

odd, even, and An+B among same-type siblings have been available across browsers for years (CSS3 Selectors). Internet Explorer 9+ supports this selector.

98% Widely available
Google Chrome Supported
Full support
Mozilla Firefox Supported
Full support
Apple Safari Supported
Full support
Microsoft Edge Supported
Full support
Opera Supported
Full support
Samsung Internet Supported
Full support
Internet Explorer 9+
Supported
:nth-of-type() Baseline · Widely available

Bottom line: Safe for everyday same-type patterns in mixed markup. This is not a JavaScript runtime API.

Key Takeaways

  • Role: same-tag siblings, counted from the start (1 = first of that type).
  • Skips other tags in the count — unlike :nth-child().
  • Start simple: p:nth-of-type(odd) { … }.
  • From the end? Use :nth-last-of-type().

One line: :nth-of-type() paints by same-tag position — ignore other elements, count from the start.

Frequently Asked Questions

It matches elements by their 1-based position among siblings of the same tag name. Other element types are skipped in the count.
:nth-child() counts every sibling. :nth-of-type() counts only matching tags, so a div between two paragraphs does not change p:nth-of-type(2).
Yes for same-type positions. odd and 2n+1 both match the 1st, 3rd, 5th… of that type. even matches the same set as 2n.
Not with :nth-of-type alone — it keys off the tag name. Combine a class with the pattern (p.fancy:nth-of-type(odd)), or use :nth-child(… of .class) where supported.
Yes. Both select the first sibling of that element type.
No. It is a browser CSS selector. Node.js, Deno, and Bun do not apply it.

Next: :only-child

Learn how to style an element when it is the sole child of its parent.

:only-child tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful