jQuery :first-child Selector

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

What You’ll Learn

The :first-child selector matches every element that is the first child of its parent. Unlike .first(), it can return multiple nodes — one per parent. Standard CSS since jQuery 1.1.4; equivalent to :nth-child(1).

01

Syntax

span:first-child

02

Per parent

Many matches

03

Official

div span

04

vs .first()

One global

05

nth(1)

Equivalent

06

CSS

Standard

Introduction

Layout patterns often treat the first item in a group differently — bold the lead nav link, underline the opening name in a list, or highlight the first column in a table. The CSS :first-child pseudo-class selects those elements based on sibling position within each parent.

jQuery supports :first-child in selector strings since version 1.1.4. Official documentation emphasizes that while .first() matches only a single element, :first-child can match more than one — one for each parent in the document.

Understanding the :first-child Selector

Think of :first-child as “the first sibling under each parent”:

  • <div><span>A</span><span>B</span></div> → first span matches span:first-child.
  • Second <div> with its own first span → also matches.
  • <div><p>…</p><span>…</span></div> → span is not first child — no match for span:first-child.
  • Equivalent to :nth-child(1) per official jQuery docs.
💡
Beginner Tip

Need only the very first element in a flat list? Use $("li").first(). Need the first item in every list? Use $("ul li:first-child").

📝 Syntax

Official jQuery API form (since 1.1.4):

jQuery
jQuery( ":first-child" )

// typical usage — prefix with element:
$( "div span:first-child" )
$( "ul.nav li:first-child" )
$( "tr td:first-child" )

// equivalent:
$( "p:nth-child(1)" )

Parameters

  • No arguments — matches elements that are the first child of their parent.

Return value

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

Official jQuery API example

jQuery
$( "div span:first-child" )
  .css( "text-decoration", "underline" )
  .hover( function() {
    $( this ).addClass( "sogreen" );
  }, function() {
    $( this ).removeClass( "sogreen" );
  });

⚡ Quick Reference

Goal:first-child.first()
First span in each divdiv span:first-childNot the same
First li in document orderMany (one per ul)$("li").first()
Same as nth-child:nth-child(1)N/A
First table columntr td:first-childNot the same
Native CSSSupportedjQuery method only

📋 :first-child vs .first() and :eq(0)

Sibling position per parent vs global index in a matched set.

:first-child
li:first-child

First per parent

.first()
$("li").first()

One element total

:nth-child(1)
p:nth-child(1)

Equivalent

:eq(0)
li:eq(0)

0-based set index

Examples Gallery

Example 1 follows the official jQuery div span:first-child demo. Examples 2–5 contrast with .first(), style navigation lists, verify :nth-child(1), and highlight table columns.

📚 Official jQuery Demo

Underline the first span in each div with a hover class toggle.

Example 1 — Official Demo: div span:first-child

Official jQuery demo — John and Glen underlined; hover adds green bold styling.

jQuery
$( "div span:first-child" )
  .css( "text-decoration", "underline" )
  .hover( function() {
    $( this ).addClass( "sogreen" );
  }, function() {
    $( this ).removeClass( "sogreen" );
  });
Try It Yourself

How It Works

Each div has its own first span child — two matches total from the official markup.

Example 2 — :first-child vs .first()

Show why :first-child returns one item per list while .first() returns a single node.

jQuery
$( "li:first-child" ).addClass( "fc" );
$( "li" ).first().addClass( "one" );

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

How It Works

Official docs: .first() matches only a single element; :first-child can match more than one.

📈 Practical Patterns

Navigation, equivalence, and table layouts.

Example 3 — Nav Lists: ul.nav li:first-child

Emphasize the first link in every navigation list.

jQuery
$( "ul.nav li:first-child" ).css({
  fontWeight: "bold",
  color: "#2563eb"
});
Try It Yourself

How It Works

Scoped ul.nav limits which lists are affected — each list contributes one first li.

Example 4 — Equivalence: :nth-child(1)

Official docs — :first-child is equivalent to :nth-child(1).

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

How It Works

Both use CSS 1-based child indexing — the element must be the first child of its parent among all element siblings.

Example 5 — Table Column: tr td:first-child

Style the first cell in every row — useful for row labels or sticky column emphasis.

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

How It Works

Each tr is a parent — the first td in every row matches, including the header row.

🚀 Common Use Cases

  • Navigation — style li:first-child as the home or primary link.
  • Inline name lists — official demo pattern with span:first-child.
  • Tables — highlight td:first-child label columns.
  • Card grids — remove left margin on .card:first-child per row wrapper.
  • Form rows — bold labels that are the first child in a field group.
  • CSS parity — same selector works in stylesheets and jQuery.

🧠 How jQuery Evaluates :first-child

1

Parse selector

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

Query
2

Check parent

For each candidate, verify it is the first element child of its parent.

CSS
3

Collect matches

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

Set
4

Return collection

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

📝 Notes

  • Available since jQuery 1.1.4 — standard CSS pseudo-class.
  • Equivalent to :nth-child(1) — official jQuery documentation.
  • Can match multiple elements — unlike .first() which returns one.
  • Element must be the first child of its parent — preceding siblings block a match.
  • Works in native querySelectorAll — no jQuery extension caveat.
  • Not the same as :first-of-type — that ignores siblings of other tag names.

Browser Support

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

CSS · jQuery 1.1.4+

jQuery :first-child Selector

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

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-child Universal

Bottom line: Use li:first-child or div span:first-child — scope to parents; prefer over bare :first-child.

Conclusion

The :first-child selector matches every element that is the first child of its parent — the official div span:first-child demo underlines John and Glen, not the other spans.

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

💡 Best Practices

✅ Do

  • Use ul.nav li:first-child for scoped queries
  • Choose :first-child when every parent needs a first item styled
  • Use .first() when you need one global match
  • Remember :nth-child(1) is equivalent
  • Reuse the same selector in CSS and jQuery

❌ Don’t

  • Confuse :first-child with :first-of-type without reading docs
  • Assume the first element in document order — check parent context
  • Use bare $(":first-child") on large pages
  • Expect a match when a different element is the first child
  • Replace :first-child with :eq(0) — different indexing models

Key Takeaways

Knowledge Unlocked

Five things to remember about :first-child

First child of each parent.

5
Core concepts
n 02

Many

Not .first()

Rule
nth 03

nth(1)

Equivalent

CSS
demo 04

Official

span hover

Demo
CSS 05

Standard

Native CSS

Tip

❓ Frequently Asked Questions

:first-child selects every element that is the first child of its parent. $("div span:first-child") matches the first span inside each div — so multiple elements can match. Available since jQuery 1.1.4.
.first() returns only one element — the first in the entire jQuery matched set. :first-child can return many — one per parent. Official docs highlight this distinction explicitly.
Yes. Official jQuery documentation states that :first-child is equivalent to :nth-child(1). Both select elements that are the first child of their parent (1-based child index in CSS).
Yes. :first-child is standard CSS and works in native querySelectorAll — e.g. document.querySelectorAll("li:first-child"). jQuery has supported it since 1.1.4.
If a text node or comment appears before the element, or if another element is the first child, your target will not match. :first-child checks sibling position among all child nodes of the same type rules in CSS — only element siblings count for :first-child among elements of that selector.
Yes — $("li:first-child") or $("div span:first-child") is clearer than bare $(":first-child"). Scoping to a parent class or ID avoids matching unintended first children across the page.
Did you know?

:first-child and :first-of-type differ when siblings of other element types come first. If a <p> precedes a <span>, the span is not :first-child but may still be :first-of-type among spans.

Continue to :last-child Selector

Learn how to match the last child of each parent — the mirror of :first-child with the official Sam and David demo.

:last-child selector 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