CSS :first-of-type Selector

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

What You’ll Learn

The :first-of-type pseudo-class matches the first element of a given tag type among its siblings. It is ideal when a heading or other element appears before your target.

01

By tag type

First p, li…

02

Siblings

Same parent.

03

vs :first-child

Ignores other tags.

04

Lists

First li each ul.

05

Articles

First paragraph.

06

Nested

Per container.

Introduction

The :first-of-type selector in CSS is a structural pseudo-class used to target the first element of a specific type within its parent container.

Unlike :first-child, it ignores other tag types that appear earlier among siblings. This makes it perfect for styling the first paragraph after a heading, or the first list item in each list.

Definition and Usage

Write the element selector before the pseudo-class: p:first-of-type or li:first-of-type. The rule matches when that element is the first of its tag name among siblings inside the same parent.

💡
Beginner Tip

If a container has <h2> followed by three <p> tags, p:first-of-type matches the first paragraph — even though the heading is the first child overall.

📝 Syntax

The syntax for the :first-of-type pseudo-class is:

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

Here, element is the HTML tag you want to target (such as p, li, or div). Only the first sibling of that type inside each parent matches.

Basic Example

first-of-type-paragraph.css
p:first-of-type {
  font-weight: 700;
  color: #2563eb;
}
p:first-of-type li:first-of-type h3:first-of-type ul li:first-of-type

Syntax Rules

  • Matching is based on element type, not overall child position.
  • Each parent creates a separate sibling group for matching.
  • Combine with combinators: ul > li:first-of-type for direct children.
  • Different from :first-child, which requires being the first child of any type.
  • Works with any element type that can appear multiple times as siblings.

:first-of-type vs :first-child

Markupp:first-of-typep:first-child
<h2><p><p>Matches first pDoes not match (h2 is first child)
<p><p><p>Matches first pMatches first p

Related Selectors

  • :last-of-type — matches the last element of a type among siblings
  • :nth-of-type() — matches by index among elements of the same type
  • :first-child — matches only when the element is the first child overall
  • :only-of-type — matches when it is the sole element of its type among siblings

⚡ Quick Reference

QuestionAnswer
Selector typeStructural pseudo-class
When it appliesElement is the first of its tag type among siblings
Other tags before itAllowed — only same-type order matters
Common useFirst paragraph after a heading, first li in each list
Typical patternarticle p:first-of-type { font-weight: 700; }
Browser supportAll modern browsers

When to Use :first-of-type

:first-of-type is the right choice when tag type matters more than overall position:

  • Article body — Style the first paragraph even when a heading comes first.
  • Multiple lists — Highlight the first item in every <ul> on the page.
  • Mixed content blocks — Target the first <div> in a section with varied markup.
  • Table rows — Style the first <tr> in each <tbody>.
  • When :first-child fails — Use when another element type precedes your target.

👀 Live Preview

An h2 comes first, but p:first-of-type still bolds and blues the first paragraph:

Article Section

This is the first paragraph in the section.

This is the second paragraph in the section.

This is the third paragraph in the section.

Examples Gallery

Practice :first-of-type with paragraphs, list items, mixed markup, and comparison with :first-child.

📜 Core Patterns

Style the first element of a specific tag type inside a parent.

Example 1 — First paragraph after a heading

Bold and color the first <p> in a section, even when an <h2> appears first.

first-of-type-paragraph.html
<style>
  p:first-of-type {
    font-weight: 700;
    color: #2563eb;
  }
</style>

<div class="container">
  <h2>Article Section</h2>
  <p>This is the first paragraph in the section.</p>
  <p>This is the second paragraph in the section.</p>
  <p>This is the third paragraph in the section.</p>
</div>
Try It Yourself

How It Works

The h2 is the first child, so p:first-child would fail. p:first-of-type correctly targets the first paragraph of its type.

Example 2 — First list item in each list

Style the first <li> in every <ul> on the page.

first-of-type-list.css
ul li:first-of-type {
  color: #b91c1c;
  font-weight: 700;
}
Try It Yourself

How It Works

Each <ul> is its own parent. li:first-of-type matches the first list item inside every list independently.

📄 Comparison & Mixed Markup

See how :first-of-type behaves with varied sibling elements.

Example 3 — First div among mixed siblings

Target the first <div> even when paragraphs and spans appear before it.

first-of-type-div.css
.section div:first-of-type {
  border-left: 4px solid #2563eb;
  padding-left: 0.75rem;
  background: #eff6ff;
}
Try It Yourself

How It Works

The paragraph before the divs does not block div:first-of-type. Only siblings of the same tag type are counted.

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

See why p:first-child fails when a <div> is first, but p:first-of-type succeeds.

first-of-type-vs-child.css
/* Does NOT match — div is first child */
p:first-child {
  color: #ef4444;
}

/* DOES match — first p among siblings */
p:first-of-type {
  color: #2563eb;
  font-weight: 700;
}
Try It Yourself

How It Works

When another element type precedes your target, reach for :first-of-type instead of :first-child.

⚠️ Common Pitfalls

  • Confusing with :first-child — They behave differently when mixed element types are present.
  • Wrong parent scope — Matching happens among direct siblings only; nested elements have their own groups.
  • Expecting first of any typep:first-of-type never matches a div; you must specify the tag.
  • Single element of type — If only one p exists, it is both first and last of type; that is expected.
  • Dynamic DOM changes — Inserting a new sibling of the same type before your target moves which element matches.

♿ Accessibility

  • Do not rely on position styling alone — Visual emphasis on the first item should not be the only cue for importance.
  • Semantic HTML first — Use proper headings and lists; pseudo-classes enhance presentation.
  • Color contrast — Ensure styled first paragraphs and list items meet WCAG contrast requirements.
  • Screen readers — Position-based styling does not change reading order or announcements.

🧠 How :first-of-type Works

1

Browser finds candidate elements

CSS gathers elements matching the tag in your selector, such as all p elements.

Match
2

Checks type order among siblings

Within each parent, the browser asks: is this the first sibling of this tag type?

Position
3

Styles apply to matches

Only the first of each type in every sibling group receives the rule.

Render
=

Type-aware styling

The first paragraph, list item, or div of its kind stands out in each container.

🖥 Browser Compatibility

The :first-of-type pseudo-class is supported in all modern browsers and has been available since CSS3.

Baseline · Universal support

Structural selectors everywhere

:first-of-type 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-of-type pseudo-class 99% supported

Bottom line: :first-of-type is safe for lists, articles, and mixed markup in all modern projects.

🎉 Conclusion

The :first-of-type pseudo-class is a versatile structural selector for targeting the first element of a specific tag among siblings. It solves cases where :first-child fails because another element type comes first.

Use it for first paragraphs after headings, first items in each list, and any layout where tag type matters more than overall position. Pair it with :last-of-type and :nth-of-type() for complete positional control.

💡 Best Practices

✅ Do

  • Use p:first-of-type after headings in articles
  • Style ul li:first-of-type per list
  • Choose :first-of-type when mixed tags precede your target
  • Combine with child combinator for direct children
  • Compare with :first-child in DevTools when debugging

❌ Don’t

  • Confuse :first-of-type with :first-child
  • Expect it to match the first element of any type
  • Forget that each parent has its own sibling group
  • Rely on position styling alone for meaning
  • Assume nested elements share one global count

Key Takeaways

Knowledge Unlocked

Five things to remember about :first-of-type

Use these points when styling by element type.

5
Core concepts
02

vs first-child

Different rules.

Compare
📄 03

After headings

First paragraph.

Use case
📝 04

Per parent

Own group.

Scope
🌐 05

99% support

All browsers.

Compat

❓ Frequently Asked Questions

The :first-of-type pseudo-class matches an element when it is the first sibling of its tag type inside its parent. Other element types appearing before it do not prevent a match.
:first-child requires the element to be the very first child of any type. :first-of-type matches the first p, first li, or first div of that tag among siblings, even if a different tag comes first.
Yes. If the parent contains h2 then p elements, p:first-of-type matches the first p even though the h2 is the first child overall.
Yes. Each parent creates its own sibling group. ul li:first-of-type matches the first li inside each ul, not just the first li on the entire page.
Yes. All modern browsers support :first-of-type. It has been available since CSS3 and works reliably for lists, articles, and layout patterns.

Practice in the Live Editor

Open the HTML editor and experiment with :first-of-type, lists, and mixed markup.

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