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.
Fundamentals
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.
Foundation
📝 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;}
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;}
First paragraph gets blue border (:first-of-type), not red (:first-child).
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.
Watch Out
⚠️ 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.
A11y
♿ 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.
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.
Compatibility
🖥 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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions
Full support
OperaAll modern versions
Full support
:first-child pseudo-class99% supported
Bottom line::first-child is safe for lists, typography, and layout spacing in all modern projects.
Wrap Up
🎉 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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about :first-child
Use these points when styling by child position.
5
Core concepts
1st01
First child
No sibling before.
Purpose
📝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.