The :nth-of-type() selector matches elements by forward position among same-tag siblings — the general form of :first-of-type. Available since jQuery 1.9; counting is 1-based. Pick the second span, stripe every other paragraph, or mark group starts with 3n+1 — even when headings sit between blocks.
01
Syntax
:nth-of-type(n)
02
By tag
Same type only
03
1-based
First = 1
04
Official
span:nth-of-type(2)
05
vs child
Mixed tags
06
An+B
even, 3n+1
Fundamentals
Introduction
After learning :first-of-type and :last-of-type, you often need positions in between — the second span in a row of names, every other paragraph in an article, or the first item in each repeating group. CSS provides :nth-of-type() for that, and jQuery has supported it since version 1.9.
The selector counts only among siblings that share the same element name (tag). A heading or paragraph of a different type does not shift the index for spans. Official docs describe it as the nth child in relation to siblings with the same element name — still 1-indexed per the CSS specification.
Concept
Understanding the :nth-of-type() Selector
Read span:nth-of-type(2) as: “Among all span siblings under this parent, select the second one.” Other tags in the same parent are ignored for counting purposes.
span:nth-of-type(1) → same as :first-of-type for spans.
span:nth-of-type(2) → second span (Kim, Ann, Richard in the official demo).
p:nth-of-type(even) → even-position paragraphs among p only.
li:nth-of-type(3n+1) → 1st, 4th, 7th list items of type li.
💡
Beginner Tip
Always include the tag in the selector — p:nth-of-type(2), not bare :nth-of-type(2). The element name defines which sibling group gets counted.
Foundation
📝 Syntax
Official jQuery API form (since 1.9):
jQuery
jQuery( ":nth-of-type(index/even/odd/equation)" )
// fixed forward index among same tag (1-based):
$( "span:nth-of-type(2)" )
$( "p:nth-of-type(1)" ) // = :first-of-type for p
// keywords and equations (same-type siblings only):
$( "p:nth-of-type(even)" )
$( "p:nth-of-type(odd)" )
$( "li:nth-of-type(3n)" )
$( "li:nth-of-type(3n+1)" )
Parameters
index — positive integer from the first same-type sibling, even/odd, or an An+B CSS equation.
Return value
A jQuery object containing every element matching the tag and nth-of-type rule within its parent.
Multiple matches when several parents each contain a qualifying element.
Official jQuery API example
jQuery
// Find each span that is second in relation to its sibling spans
$( "span:nth-of-type(2)" )
.append( " is 2nd sibling span " )
.addClass( "nth" );
Cheat Sheet
⚡ Quick Reference
Goal
Selector
First of tag
p:nth-of-type(1)
Second span
span:nth-of-type(2)
Even paragraphs
p:nth-of-type(even)
Every 3rd li
li:nth-of-type(3n)
Counts all child types
Use :nth-child() instead
Compare
📋 :nth-of-type() vs :nth-child() and :first-of-type
Type-scoped forward counting vs all-sibling counting — essential on mixed markup.
Of-type
span:nth-of-type(1)
First span only
Nth-child
span:nth-child(1)
Must be 1st child
:first-of-type
p:first-of-type
= nth-of-type(1)
.eq(1)
$("span").eq(1)
0-based flat set
Hands-On
Examples Gallery
Example 1 follows the official jQuery demo. Examples 2–5 cover first-of-type equivalence, the difference from :nth-child(), paragraph stripes, and 3n+1 group patterns. Use Try-it links to run each snippet live.
📚 Official jQuery Demo
Highlight the second span among sibling spans in each container.
Example 1 — Official Demo: span:nth-of-type(2)
Append is 2nd sibling span and add class nth to every second span — Kim, Ann, and Richard in the official markup.
First paragraph gets .lead styling
Heading above does not block the match
How It Works
:nth-of-type(1) is the general form; :first-of-type is the readable shorthand for the common case.
Example 3 — Mixed Tags: nth-of-type(1) vs nth-child(1)
When a p is the first child, only the of-type selector matches the first span.
jQuery
$( "span:nth-of-type(1)" ).addClass( "of-type" ); // Span A
$( "span:nth-child(1)" ).addClass( "of-child" ); // no match
// Intro p is first child — span fails nth-child(1)
Items 1, 4, 7 — green lead border
Repeating 3-item cycle visually marked
How It Works
The An+B formula generates a sequence. With pure li lists it often matches :nth-child(3n+1); with mixed tags inside the list, of-type is safer.
Applications
🚀 Common Use Cases
Second label — span:nth-of-type(2) in name lists (official demo).
Lead paragraph — p:nth-of-type(1) after a section heading.
Article stripes — p:nth-of-type(even) with headings between blocks.
Grouped lists — li:nth-of-type(3n+1) for cycle starts.
Table columns — td:nth-of-type(2) for second column cells by type.
Mixed markup — prefer of-type over nth-child when sibling tags differ.
🧠 How jQuery Evaluates :nth-of-type()
1
Find candidates
Gather elements matching the tag — e.g. all span or p in scope.
Query
2
Group by type
Within each parent, count only siblings sharing the same element name.
Filter
3
Test formula
Keep elements whose 1-based type index satisfies the number, even/odd, or An+B rule.
Match
4
📦
Return collection
Matching elements become a jQuery object — often one per parent container.
Important
📝 Notes
Available since jQuery 1.9 — follows the W3C CSS specification strictly.
Counting is 1-indexed among same-type siblings.
:first-of-type equals :nth-of-type(1) for a given tag.
Only siblings with the same tag name participate — unlike :nth-child().
Class names and IDs do not affect of-type counting — only element names matter.
Native querySelectorAll("span:nth-of-type(2)") uses identical syntax in modern browsers.
Compatibility
Browser Support
The :nth-of-type() pseudo-class is standard CSS3 and works in jQuery 1.9+. An+B equations and even/odd are supported in evergreen browsers via native querySelectorAll.
✓ CSS3 · jQuery 1.9+
jQuery :nth-of-type() Selector
Supported in all modern browsers. Native: document.querySelectorAll("p:nth-of-type(even)"). Forward 1-based indexing among same-tag siblings.
100%Standard + jQuery
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
:nth-of-type()Universal
Bottom line: Safe for modern apps. Use of-type when sibling tags differ; pair with :nth-last-of-type() for reverse patterns.
Wrap Up
Conclusion
The :nth-of-type() selector is the forward, type-scoped counterpart to :first-of-type and :last-of-type. The official span:nth-of-type(2) demo shows how each parent is evaluated independently for the second span among span siblings.
Remember :nth-of-type(1) = :first-of-type, prefer it over :nth-child() when headings or other tags sit between repeated elements, and explore An+B patterns for grouped layouts.
Start with :first-of-type then generalize to nth-of-type
Reuse selectors in CSS and jQuery
❌ Don’t
Confuse with :nth-child() on mixed content
Use bare $(":nth-of-type(2)") without a tag
Assume classes change of-type index — only tags count
Swap in .eq(1) expecting parent-aware matching
Forget that each parent evaluates its own type group
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about :nth-of-type()
Forward index among same-tag siblings only.
5
Core concepts
📝01
By tag
Same type only
Scope
202
Official
2nd span
Demo
=03
:first-of-type
nth-of-type(1)
Equiv
≠04
≠ nth-child
Mixed tags
Rule
3n05
An+B
Group patterns
Formula
❓ Frequently Asked Questions
:nth-of-type(index) selects every element that is the nth sibling of its tag name under the same parent. $("span:nth-of-type(2)") matches the second span among span siblings — Kim, Ann, and Richard in the official demo — regardless of other tag types nearby.
:nth-child() counts among all sibling element types. :nth-of-type() counts only among siblings with the same tag. A leading p before spans means span:nth-child(1) fails but span:nth-of-type(1) still matches the first span.
Yes. :first-of-type is equivalent to :nth-of-type(1) for a given element type. Both select the first element of that tag name among siblings under the same parent.
Yes. jQuery follows the CSS specification: counting starts at 1. This differs from jQuery methods like .eq() and .first(), which use 0-based indexing on a flat matched collection.
Official jQuery docs support :nth-of-type(even), :nth-of-type(odd), :nth-of-type(3n), and :nth-of-type(3n+1) — evaluated among same-type siblings only, so headings or other tags between paragraphs do not shift paragraph indexes.
jQuery 1.9 added :nth-of-type() alongside :first-of-type, :last-of-type, and related structural pseudo-classes. It works in native querySelectorAll in modern browsers.
Did you know?
The official jQuery span:nth-of-type(2) demo matches Kim, Ann, and Richard — one second-span per parent div — even though each container has a different total number of spans. That is the power of per-parent, type-scoped indexing.