jQuery :first-child Selector

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

What Is :first-child?

:first-child (since jQuery 1.1.4) selects every element that is the first child of its parent. Unlike .first(), which returns one element from a set, :first-child can match one per parent. It is equivalent to :nth-child(1).

Remember
// Official pattern — first span in each div
$("div span:first-child");

// First list item in each ul
$("ul li:first-child");

// Same as :nth-child(1)
$("p:nth-child(1)");

// Not the same as .first() (one element only)
$("li").first();
NeedUse
First child per parent$("li:first-child")
First in a jQuery set$("li").first()
First of a tag type$("span:first-of-type")
Nth child$("li:nth-child(2)")
Last child$("li:last-child")

How to Use :first-child

Scope the selector, then style or read the matched first children.

  1. Write a typed selector such as $("li:first-child") or $("div span:first-child").
  2. Optionally scope with a parent: $("ul.nav li:first-child").
  3. Chain methods — .text(), .addClass(), events.
  4. Remember: the element must be the first element child of its parent.
  5. Use .first() only when you want one match from the whole set.
PieceRequired?Role
Tag / filterRecommendede.g. li, span, td
:first-childYesMust be the first element sibling
Parent scopeOptionalLimits which trees are searched

Pseudocode

Pseudocode
for each element matching the left side (e.g. span):
    look at its parent
    if this element is the first element child of that parent:
        include it in the result

result may contain many elements
    → one match is possible for each parent

.compare with .first():
    take the full matched set
    keep only the first item in that set
    → always 0 or 1 element

Cheat Sheet

Keep these facts handy while selecting first children.

TopicDetail
Selector$("elem:first-child")
SincejQuery 1.1.4
Matches0–many (one per parent possible)
Equivalent:nth-child(1)
vs .first()Per parent vs one from the set
vs :first-of-typeFirst of any tag vs first of same tag
CSS?Yes — standard CSS pseudo-class
Official demo$("div span:first-child")

Live Preview

Mark the first child in each group, compare with .first(), or count matches.

Sample: two lists of names. :first-child can hit both lists; .first() hits only one element total.

  • Ada
  • Grace
  • Alan
  • Linus
  • Ken
  • Dennis
Result
Ready — pick an action and click Run.

jQuery Examples

Short, copy-ready snippets. Prefer a typed selector over bare $(":first-child").

Example 1 — Official div span:first-child

Mark the first span in each div — one match per parent.

jQuery
$("div span:first-child").each(function () {
  $(this).text($(this).text() + " [first]");
});
Try It Yourself

How It Works

  1. Select. div span:first-child finds first spans only.
  2. Per parent. Each div contributes one match.
  3. Mark. Append [first] to those spans.
  4. Skip. Later siblings stay unchanged.

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

One selector can return many; the method returns a single element.

jQuery
var perParent = $("ul li:first-child").length;
var oneOnly = $("ul li").first().length;

$("#out").text(
  ":first-child → " + perParent +
  ", .first() → " + oneOnly
);
Try It Yourself

How It Works

  1. :first-child. One first li per ul.
  2. .first(). First li in the whole set only.
  3. Count. Compare the lengths.
  4. Choose. Per-parent vs single-set first.

Example 3 — First List Item

Highlight the first li in each list with a text marker.

jQuery
$("ul li:first-child").prepend("* ");
Try It Yourself

How It Works

  1. Target. ul li:first-child scopes to list items.
  2. Match. Only the first li in each ul.
  3. Mark. .prepend("* ") labels those items.
  4. Rest. Other list items stay plain.

Example 4 — Same as :nth-child(1)

Official docs: :first-child equals :nth-child(1).

jQuery
var a = $("ul li:first-child").map(function () {
  return $(this).text();
}).get().join(", ");

var b = $("ul li:nth-child(1)").map(function () {
  return $(this).text();
}).get().join(", ");

$("#out").text("first-child=[" + a + "] nth=[" + b + "]");
Try It Yourself

How It Works

  1. Run both. Collect text from each selector.
  2. Compare. Results match for the same lists.
  3. Index. CSS child indexes are 1-based.
  4. Pick. Use either form — :first-child reads clearer.

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

When the first child is not a span, only :first-of-type matches that span.

jQuery
var child = $("div span:first-child").length;
var ofType = $("div span:first-of-type").length;

$("#out").text(
  "span:first-child → " + child +
  ", span:first-of-type → " + ofType
);
Try It Yourself

How It Works

  1. first-child. Requires the span to be child #1.
  2. first-of-type. Requires the first span among siblings.
  3. Mismatch. An earlier em blocks :first-child.
  4. Choose. Pick the pseudo-class that matches your intent.

Edge Cases & Pitfalls

These mix-ups cause most :first-child surprises.

vs first

Not the same as .first()

:first-child is per parent. .first() is one element from the whole set.

of-type

Wrong pseudo for “first span”

If another tag comes first, use :first-of-type, not :first-child.

scope

Bare $(":first-child") is broad

Prefix with a tag and parent scope to avoid matching the whole document.

index

:nth-child(1) is 1-based

CSS child indexes start at 1 — same as :first-child.

Browser Support

:first-child is a standard CSS pseudo-class. jQuery has supported it since 1.1.4. Works wherever your loaded jQuery build (and the browser CSS engine) runs.

✓ Stable · jQuery 1.1.4+

jQuery :first-child

Universal CSS / jQuery support. Prefer current jQuery 3.x+ for consistent selector engines.

100% CSS + jQuery
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: Safe for everyday UI. Prefer typed, scoped selectors over bare :first-child.

Performance Notes

ApproachCostBest for
Scoped $("ul.nav li:first-child")CheapTypical UI highlighting
Bare $(":first-child")BroaderAvoid on large pages
.first() on a small setCheapWhen you need exactly one

Modern browsers implement :first-child in native CSS. Keep selectors scoped so jQuery does less work.

Key Takeaways

  • Role: select elements that are the first child of their parent.
  • Many matches: one possible first child per parent.
  • Equals: :nth-child(1) — not the same as .first().
  • Scope: prefer typed selectors like $("li:first-child").

One line: :first-child finds the first child per parent — use .first() when you want only one match from a set.

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.
Yes. Official jQuery documentation states that :first-child is equivalent to :nth-child(1).
Yes. :first-child is a standard CSS pseudo-class and works with native querySelectorAll as well. jQuery has supported it since 1.1.4.
The element must be the first element child of its parent. If another element comes before it, or you meant “first of this tag type,” use :first-of-type instead.
Yes — $("li:first-child") or $("div span:first-child") is clearer than bare $(":first-child"). Scoping to a parent avoids matching unintended first children across the page.

Next: :last-child Selector

Learn the mirror selector for last children of each parent.

:last-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.

18 people found this page helpful