CSS :first-child Selector

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

Definition and Usage

The :first-child pseudo-class matches an element that is the first element child of its parent.

Example: li:first-child styles a list item only when that li is the first child of its parent. It is the same match as :nth-child(1). For “first of this tag,” use :first-of-type.

Baseline · Widely available.

Not deprecated. A long-standing structural pseudo-class from Selectors Level 3 / 4.

CSS Syntax

syntax.css
:first-child { /* or */ E:first-child { … }

Example: li:first-child { font-weight: bold; }

Selector Forms

FormDescriptionTry it
:first-childAny element that is the first child of its parentTry it
E:first-childElement of type E that is also the first childTry it
vs :first-of-typeFirst of that tag vs must be first child overallTry it
Nested listsEach parent has its own first childTry it

How to Use :first-child

Confirm the target is truly the first element child, then add the pseudo-class.

  1. Look at the parent’s children in the DOM (DevTools helps).
  2. Write E:first-child when you care about both the tag and first position.
  3. Style spacing, borders, or emphasis for that first item.
  4. If another tag can appear first, switch to :first-of-type.
PieceRequired?Role
Parent with childrenYesSiblings are checked relative to one parent
:first-childYesMatches only the first element child
Type selectorOptionalLimits the match to a tag (e.g. li)

Live Preview

The orange border marks li:first-child. Insert a note before the list items to see the highlight move (or disappear from Item 1).

List demo

When a <p> becomes the first child, no li matches li:first-child.

Siblings
  • Item 1
  • Item 2
  • Item 3

Item 1 matches li:first-child.

CSS Examples

Each snippet focuses on :first-child. Extra styles stay minimal for study. Use Try It Yourself for a live editor.

Example 1 — Style the First List Item

Highlight the first li in a list.

CSS
<style>
  li:first-child {
    border: 2px solid orange;
  }
</style>

<ul>
  <li>Adhemar da Silva</li>
  <li>Wang Junxia</li>
  <li>Wilma Rudolph</li>
</ul>
Try It Yourself

How It Works

  1. Pick the tag. li:first-child only considers list items.
  2. Require first place. The li must also be the parent’s first element child.
  3. Style that item. An orange border marks the match.
  4. Others stay plain. Item 2 and Item 3 are not first children.

Example 2 — When the First Child Is Not a p

p:first-child fails if another element comes first.

CSS
<style>
  p:first-child {
    color: #16a34a;
    font-weight: 700;
  }
</style>

<div>
  <p>This text is selected!</p>
  <p>This text isn't selected.</p>
</div>

<div>
  <h2>Not a p — so no p:first-child here.</h2>
  <p>This text isn't selected.</p>
</div>
Try It Yourself

How It Works

  1. First box. The first child is a p, so p:first-child matches.
  2. Second box. The first child is an h2.
  3. No match. The following p is not the first child.
  4. Need first p? Use p:first-of-type instead.

Example 3 — Nested Lists

Each list has its own first child.

CSS
<style>
  ul li {
    color: #2563eb;
  }

  ul li:first-child {
    color: #dc2626;
    font-weight: 700;
  }
</style>

<ul>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3
    <ul>
      <li>Item 3.1</li>
      <li>Item 3.2</li>
    </ul>
  </li>
</ul>
Try It Yourself

How It Works

  1. Outer list. Item 1 is the first child of the outer ul.
  2. Inner list. Item 3.1 is the first child of the nested ul.
  3. Same selector. ul li:first-child can match in every list.
  4. Independent parents. First-child is always relative to one parent.

Edge Cases & Pitfalls

Most surprises come from DOM order, not from the selector itself.

order

Another element before the target

If an h2 (or any element) comes first, p:first-child will not match that p.

vs type

Not the same as :first-of-type

Need the first p even after a heading? Use :first-of-type.

text

Whitespace does not block it

In modern CSS, leading text nodes or spaces between tags do not stop :first-child. Earlier elements do.

alias

Same as :nth-child(1)

:first-child and :nth-child(1) select the same elements.

only

Single child also matches

An only child is still the first child. For “exactly one child,” use :only-child.

Browser Support

:first-child is a browser CSS selector. Node.js, Deno, and Bun do not evaluate it.

✓ Baseline · Widely available

:first-child

Supported in all modern browsers for many years (Chrome 1+, Firefox 1+, Safari 3.1+, Edge 12+, IE 7+ with historical quirks in very old engines).

98% Global support
Google Chrome 1+
Full support
Mozilla Firefox 1+
Full support
Apple Safari 3.1+
Full support
Microsoft Edge 12+
Full support
Opera 9.5+
Full support
Samsung Internet 1+
Full support
Internet Explorer 7+
Full support*
:first-child Widely supported

Bottom line: *Very old IE had edge-case bugs with comments/anonymous boxes. Safe everywhere for modern sites. It is not a JavaScript runtime API.

Key Takeaways

  • Role: match the first element child of a parent.
  • Form: E:first-child = tag E and first position.
  • vs :first-of-type: use of-type when another tag can come first.
  • Alias: same match as :nth-child(1).

One line: :first-child styles an element only when it is the first element sibling under its parent.

Frequently Asked Questions

It matches an element that is the first element child of its parent. Other element siblings must not come before it.
Use :first-child alone or with a type, such as li:first-child. That means “an li that is also the first child,” not “the first li after other tags.”
:first-child requires the element to be the first child of any type. :first-of-type matches the first element of that tag among siblings, even if another tag comes first.
Usually another element sits before the target (for example an h2 before a p). Whitespace text nodes do not block :first-child in modern CSS—only earlier element siblings do.
Yes. :first-child matches the same elements as :nth-child(1).
No. It is a CSS selector evaluated by the browser layout engine, not a Node.js or server runtime feature.

Next: ::first-letter

Style the first letter of a block with the ::first-letter pseudo-element.

::first-letter 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.

8 people found this page helpful