CSS :first-child Selector

Beginner
⏱️ 6 min read
📚 Updated: Jul 2026
🎯 4 Examples
Structural Selectors

What You’ll Learn

The :first-child pseudo-class matches an element only when it is the very first child inside its parent. It is essential for list styling, spacing fixes, and position-based design.

01

First child

No sibling before.

02

Any type

Tag agnostic.

03

Lists

Style first item.

04

Spacing

Remove top gap.

05

Combine

div > p:first-child.

06

vs :first-of-type

Know the diff.

Introduction

The :first-child selector in CSS is a structural pseudo-class used to match an element that is the first child of its parent container.

It lets you apply unique styles based on position in the DOM tree — for example, highlighting the first item in a list or removing extra top margin from the first paragraph in a section.

Definition and Usage

Write the element selector before the pseudo-class: li:first-child or p:first-child. The rule matches only when that element is the first child among all siblings inside its parent.

💡
Beginner Tip

:first-child cares about overall position, not tag type. If a <div> comes before your <p>, then p:first-child will not match. Use :first-of-type when you need the first paragraph regardless of what comes before it.

📝 Syntax

The syntax for the :first-child pseudo-class is:

syntax.css
element:first-child {
  /* CSS properties */
}

Here, element is the tag or selector for the child you want to style. The element must also be the first child of its parent to match.

Basic Example

first-child-list.css
ul > li:first-child {
  color: #b91c1c;
  font-weight: 700;
}
li:first-child p:first-child .card:first-child *:first-child

Syntax Rules

  • The matched element must be the first child of its parent, regardless of tag type.
  • Combine with combinators: div > p:first-child for direct children only.
  • Any preceding sibling — even a different tag — prevents a match.
  • Use *:first-child to target whatever element is first inside a container.
  • Do not confuse with :first-of-type, which ignores other tag types.

:first-child vs :first-of-type

SelectorMatches when…
p:first-childThe <p> is the first child of its parent (no element before it)
p:first-of-typeThe <p> is the first <p> among siblings, even if a <div> comes first

Related Selectors

  • :last-child — matches the last child of a parent
  • :nth-child() — matches children by index (1-based)
  • :first-of-type — first element of a given tag among siblings
  • > child combinator — limit matches to direct children

⚡ Quick Reference

QuestionAnswer
Selector typeStructural pseudo-class
When it appliesElement is the first child of its parent
Index basisDOM order among all sibling children
Common useFirst list item, remove top margin, lead styling
Typical pattern.content > *:first-child { margin-top: 0; }
Browser supportAll modern browsers

When to Use :first-child

:first-child is ideal for position-based styling:

  • Navigation lists — Style the first menu item differently.
  • Article lead — Emphasize the opening paragraph when it is truly first.
  • Spacing resets — Remove top margin or padding from the first child in a stack.
  • Card grids — Highlight the first card in a row when markup order is predictable.
  • Table rows — Style the first row in a tbody for header-like appearance.

👀 Live Preview

The first list item matches li:first-child and gets red bold styling:

  • First Item
  • Second Item
  • Third Item

Examples Gallery

Practice :first-child with lists, spacing, scoped paragraphs, and comparison with :first-of-type.

📜 Core Patterns

Style the first child element inside a parent container.

Example 1 — Style the first list item

Make the first <li> in a list stand out with bold red text.

first-child-list.html
<style>
  ul > li:first-child {
    color: #b91c1c;
    font-weight: 700;
  }
</style>

<ul>
  <li>First Item</li>
  <li>Second Item</li>
  <li>Third Item</li>
</ul>
Try It Yourself

How It Works

Only the first <li> is the first child of the <ul>. The other items have an <li> sibling before them, so they do not match.

Example 2 — Remove top margin from the first child

A common layout fix: strip extra space above the first element in a content block.

first-child-margin.css
.content > *:first-child {
  margin-top: 0;
}

.content > * {
  margin-top: 1rem;
}
Try It Yourself

How It Works

All direct children get top margin, but *:first-child resets it to zero for whichever element comes first.

📄 Specificity & Comparison

Combine with element selectors and understand when :first-child fails.

Example 3 — First paragraph inside a div

Target p:first-child only when the paragraph is truly the first element in the container.

first-child-paragraph.css
div > p:first-child {
  font-size: 1.125rem;
  font-weight: 600;
  color: #1e293b;
}
Try It Yourself

How It Works

If a heading or image appears before the paragraph, p:first-child will not match. Inspect sibling order in DevTools when styles seem missing.

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

See why p:first-child fails when a <div> is first, while p:first-of-type still matches.

first-child-vs-type.css
/* Only matches if p is the first child */
p:first-child {
  border-left: 4px solid #ef4444;
  padding-left: 0.75rem;
}

/* Matches the first p among siblings */
p:first-of-type {
  border-left: 4px solid #2563eb;
  padding-left: 0.75rem;
}
Try It Yourself

How It Works

Because a <div> is the first child, p:first-child does not match. p:first-of-type ignores the div and styles the first paragraph.

⚠️ Common Pitfalls

  • Wrong syntax explanation — The selector before :first-child is the child element, not the parent. Parent context comes from combinators like ul > li:first-child.
  • Hidden siblings — An invisible or empty element before your target still counts as a preceding sibling.
  • Text nodes — In some cases whitespace between tags can affect sibling relationships; keep markup clean.
  • Expecting first of a type — Use :first-of-type or :nth-of-type(1) when tag type matters more than overall position.
  • Dynamic content — JavaScript-inserted elements at the top can change which node is :first-child.

♿ Accessibility

  • Do not rely on position alone — Visual emphasis on the first item should not be the only way to convey importance.
  • Lists remain semantic — Use proper <ul> or <ol> markup; styling the first item does not replace list structure for screen readers.
  • Focus order — Position styling does not change tab order; keep interactive elements logically ordered.
  • Color contrast — Ensure first-child highlight colors meet WCAG contrast requirements.

🧠 How :first-child Works

1

Browser finds candidate elements

CSS gathers elements matching the tag or selector before :first-child.

Match
2

Checks sibling position

For each candidate, the browser asks: is this the first child of its parent?

Position
3

Styles apply to matches

Only elements with no preceding sibling children receive the rule.

Render
=

Position-aware styling

The first child stands out in lists, articles, and layout stacks.

🖥 Browser Compatibility

The :first-child pseudo-class is supported in all modern browsers and has been available since CSS2.

Baseline · Universal support

Structural selectors everywhere

:first-child works reliably in Chrome, Firefox, Safari, Edge, and Opera.

99% Universal support
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
Full support
Opera All modern versions
Full support
:first-child pseudo-class 99% supported

Bottom line: :first-child is safe for lists, typography, and layout spacing in all modern projects.

🎉 Conclusion

The :first-child pseudo-class is a versatile tool for styling elements based on their position as the first child inside a parent. It is widely used in lists, articles, and layout stacks.

Remember that it checks overall sibling order, not tag type. When you need the first element of a specific tag, reach for :first-of-type instead.

💡 Best Practices

✅ Do

  • Use ul > li:first-child for predictable list styling
  • Reset spacing with *:first-child { margin-top: 0; }
  • Inspect DOM order when styles do not apply
  • Combine with child combinator > for direct children
  • Choose :first-of-type when tag type matters

❌ Don’t

  • Assume the first <p> is always :first-child
  • Forget hidden or wrapper elements before your target
  • Confuse the child selector with the parent selector
  • Rely on position alone for meaning or hierarchy
  • Overuse when a class on the first item is clearer

Key Takeaways

Knowledge Unlocked

Five things to remember about :first-child

Use these points when styling by child position.

5
Core concepts
📝 02

Syntax

li:first-child.

Pattern
📄 03

Lists

Highlight first.

Use case
04

vs first-of-type

Different rules.

Compare
🌐 05

99% support

All browsers.

Compat

❓ Frequently Asked Questions

The :first-child pseudo-class matches an element only when it is the very first child of its parent — no other element or text node comes before it among the parent's children.
Write the element selector before the pseudo-class: li:first-child matches an li that is the first child of its parent. It does not mean "the first li among siblings" unless that li is also the first child overall.
:first-child requires the element to be the first child of any type. :first-of-type matches the first element of that tag name among siblings, even if another element type comes first.
Usually because another element appears before the target — even a comment sibling, whitespace text node, or a different tag like a div before your p. Check the actual DOM order inside the parent.
Yes. All modern browsers support :first-child. It has been available since CSS2 and works reliably for lists, typography, and layout spacing.

Practice in the Live Editor

Open the HTML editor and experiment with :first-child, lists, and spacing patterns.

HTML Editor →

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.

5 people found this page helpful