jQuery :first-of-type Selector

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Structural CSS

What You’ll Learn

The :first-of-type selector matches every element that is the first of its tag name among siblings. It can return multiple nodes — one per parent. Standard CSS since jQuery 1.9; equivalent to :nth-of-type(1).

01

Syntax

span:first-of-type

02

Per parent

Many matches

03

Official

span + fot

04

vs :first-child

Tag-based

05

nth-of-type

Equivalent

06

CSS

Standard

Introduction

Real markup mixes element types — a paragraph intro, then spans, then more paragraphs. You often need the first <p> or first <span> in each block, even when another tag is the overall first child. The CSS :first-of-type pseudo-class selects by element name among siblings, not absolute child position.

jQuery supports :first-of-type in selector strings since version 1.9. Official documentation states that it matches elements with no other element of the same name and parent coming before them in the document tree — so $("span:first-of-type") can match many spans, one per parent.

Understanding the :first-of-type Selector

Think of :first-of-type as “the first sibling of this tag name under each parent”:

  • <div><span>A</span><span>B</span></div> → first span matches span:first-of-type.
  • Second <div> with its own first span → also matches.
  • <div><p>…</p><span>…</span></div> → span matches span:first-of-type but not span:first-child.
  • Equivalent to :nth-of-type(1) in standard CSS.
💡
Beginner Tip

When siblings are all the same tag (e.g. only <li> in a list), :first-of-type and :first-child behave the same. They diverge when mixed element types share a parent.

📝 Syntax

Official jQuery API form (since 1.9):

jQuery
jQuery( ":first-of-type" )

// typical usage — prefix with element:
$( "span:first-of-type" )
$( "article p:first-of-type" )
$( "tr td:first-of-type" )

// equivalent:
$( "h2:nth-of-type(1)" )

Parameters

  • No arguments — matches elements that are the first of their element type among siblings.

Return value

  • A jQuery object containing every matching first-of-type element (often multiple).
  • Empty collection when no element is first-of-type in scope.

Official jQuery API example

jQuery
$( "span:first-of-type" ).addClass( "fot" );

⚡ Quick Reference

Goal:first-of-type:first-child
First span in each divspan:first-of-typeSame if span is first child
Span after a <p> siblingMatchesNo match
First paragraph in articlearticle p:first-of-typeOnly if p is first child
Equivalent pseudo-class:nth-of-type(1):nth-child(1)
Native CSSSupportedSupported

📋 :first-of-type vs :first-child and :nth-of-type(1)

Tag-based sibling position vs absolute first child vs typed nth index.

:first-of-type
span:first-of-type

First span per parent

:first-child
span:first-child

Must be first child

:nth-of-type(1)
p:nth-of-type(1)

Equivalent

Mixed markup
p then span

Only :first-of-type

Examples Gallery

Example 1 follows the official jQuery span:first-of-type demo. Examples 2–5 contrast with :first-child, style article lead paragraphs, verify :nth-of-type(1), and highlight table columns.

📚 Official jQuery Demo

Add the fot class to the first span in each matched div.

Example 1 — Official Demo: span:first-of-type

Official jQuery demo — Corey and Nobody styled red, italic, and larger; other spans unchanged.

jQuery
$( "span:first-of-type" ).addClass( "fot" );
Try It Yourself

How It Works

Each div has its own first span — two matches total from the official markup, regardless of other sibling types.

Example 2 — :first-of-type vs :first-child

When a paragraph precedes spans, only :first-of-type matches the lead span.

jQuery
$( "span:first-of-type" ).addClass( "fot" );
$( "span:first-child" ).addClass( "fc" );

console.log( ":first-of-type →", $( "span:first-of-type" ).length );
console.log( ":first-child →", $( "span:first-child" ).length );
Try It Yourself

How It Works

:first-child counts all element siblings; :first-of-type only compares elements with the same tag name.

📈 Practical Patterns

Articles, equivalence, and table layouts.

Example 3 — Article Lead: article p:first-of-type

Style the opening paragraph in every article, even when a heading comes first.

jQuery
$( "article p:first-of-type" ).css({
  fontSize: "1.125rem",
  color: "#334155"
});
Try It Yourself

How It Works

The h2 is the first child, but the first p still matches p:first-of-type.

Example 4 — Equivalence: :nth-of-type(1)

Standard CSS — :first-of-type is equivalent to :nth-of-type(1).

jQuery
console.log( ":first-of-type →", $( "li:first-of-type" ).length );
console.log( ":nth-of-type(1) →", $( "li:nth-of-type(1)" ).length );
Try It Yourself

How It Works

Both use CSS typed sibling indexing — only elements of the same tag name are counted.

Example 5 — Table Column: tr td:first-of-type

Style the first cell in every row — same result as :first-child when rows contain only td cells.

jQuery
$( "tr td:first-of-type" ).css({
  backgroundColor: "#f1f5f9",
  fontWeight: "bold"
});
Try It Yourself

How It Works

Each tr is a parent — the first td in every row matches. With only td children, :first-of-type and :first-child select the same cells.

🚀 Common Use Cases

  • Lead paragraphsarticle p:first-of-type for intro text after headings.
  • Inline name lists — official pattern with span:first-of-type.
  • Section headingssection h2:first-of-type when markup mixes tags.
  • Tables — highlight td:first-of-type label columns.
  • Definition lists — style the first dt or dd in each group.
  • CSS parity — same selector works in stylesheets and jQuery.

🧠 How jQuery Evaluates :first-of-type

1

Parse selector

Combine type selector with :first-of-type — e.g. span:first-of-type.

Query
2

Check siblings

For each candidate, verify no same-tag sibling appears earlier under the same parent.

CSS
3

Collect matches

One match per parent per tag — can be many elements document-wide.

Set
4

Return collection

Chain .addClass(), .css(), or other jQuery methods.

📝 Notes

  • Available since jQuery 1.9 — standard CSS pseudo-class.
  • Equivalent to :nth-of-type(1) — standard CSS specification.
  • Can match multiple elements — one first-of-type per parent for the chosen tag.
  • Differs from :first-child when siblings of other element types precede the target.
  • Works in native querySelectorAll — no jQuery extension caveat.
  • Always pair with an element type — p:first-of-type, not bare :first-of-type on large pages.

Browser Support

The :first-of-type pseudo-class is standard CSS and works in jQuery 1.9+. All modern browsers support document.querySelectorAll("p:first-of-type"). Same syntax in stylesheets and jQuery selectors.

CSS · jQuery 1.9+

jQuery :first-of-type Selector

Universal browser support. Native: querySelectorAll("span:first-of-type").

100% Standard CSS
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
:first-of-type Universal

Bottom line: Use p:first-of-type or article p:first-of-type — scope to parents; prefer over bare :first-of-type.

Conclusion

The :first-of-type selector matches every element that is the first of its tag name among siblings — the official span:first-of-type demo styles Corey and Nobody with the fot class, not the other spans.

Distinguish it from :first-child, remember equivalence with :nth-of-type(1), and scope your selector to the parents you care about.

💡 Best Practices

✅ Do

  • Use article p:first-of-type for scoped lead paragraphs
  • Choose :first-of-type when mixed sibling tags break :first-child
  • Remember :nth-of-type(1) is equivalent
  • Reuse the same selector in CSS and jQuery
  • Compare with :first-child when debugging unexpected matches

❌ Don’t

  • Assume :first-of-type equals :first-child without checking markup
  • Use bare $(":first-of-type") on large pages
  • Confuse :nth-of-type with :nth-child — different counting rules
  • Expect a match when an earlier same-tag sibling exists
  • Forget to scope — prefix with a parent selector when possible

Key Takeaways

Knowledge Unlocked

Five things to remember about :first-of-type

First of each tag name per parent.

5
Core concepts
02

Not :first-child

Mixed tags differ

Rule
nth 03

nth-of-type(1)

Equivalent

CSS
demo 04

Official

span + fot

Demo
CSS 05

Standard

Native CSS

Tip

❓ Frequently Asked Questions

:first-of-type selects every element that is the first among siblings with the same element name (tag). $("span:first-of-type") matches the first span in each parent — Corey and Nobody in the official demo. Available since jQuery 1.9.
:first-child requires the element to be the first child of its parent among all element types. :first-of-type only compares siblings with the same tag. If a <p> comes before a <span>, the span is not :first-child but can still be span:first-of-type.
Yes. In CSS, :first-of-type is equivalent to :nth-of-type(1). Both select the first element of that tag name among siblings under the same parent.
Yes. :first-of-type is standard CSS and works in native querySelectorAll — e.g. document.querySelectorAll("p:first-of-type"). jQuery has supported it since 1.9.
Another element type may be the first child. Example: <div><p>Intro</p><span>Lead</span></div> — the span matches span:first-of-type but not span:first-child because the paragraph is the first child.
Yes — $("p:first-of-type") or $("article h2:first-of-type") is clearer than bare $(":first-of-type"). Scoping to a parent class or ID avoids matching unintended first-of-type nodes across the page.
Did you know?

:first-of-type looks at element names only — class names and IDs do not matter. Two <span class="a"> and <span class="b"> are both spans; only the first span among siblings matches span:first-of-type.

Continue to :nth-of-type() Selector

Generalize :first-of-type with forward indexes, even/odd stripes, and An+B patterns among same-tag siblings.

:nth-of-type() 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.

6 people found this page helpful