jQuery :first-child Selector

Beginner
5 min read
Updated: Nov 2026
3 examples
Selectors

Definition

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

Syntax

Start with these common calls. Keep them short and readable.

jQuery
// 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();
CallWhat it does
:first-childLearn jQuery :first-child: select elements that are the first child of their parent, compare with .first() and :nth-child(1), and style first items in lists and groups.

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Write a typed selector such as $("li:first-child") or $("div span:first-child").
Step 2Optionally scope with a parent: $("ul.nav li:first-child").
Step 3Chain methods — .text(), .addClass(), events.

Return Value

Return ValueDescription
See examplesCheck the examples below for what this API returns in common cases.

jQuery Examples

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.

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.

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.

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 Selector tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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