CSS :first-of-type Selector

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

Definition and Usage

The :first-of-type pseudo-class matches an element that is the first of its tag name among sibling elements.

Example: p:first-of-type styles the first p under a parent, even if an h2 comes before it. Same match as :nth-of-type(1). For “must be first child of any type,” use :first-child.

Baseline · Widely available.

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

CSS Syntax

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

Example: p:first-of-type { color: red; font-style: italic; }

Selector Forms

FormDescriptionTry it
E:first-of-typeFirst sibling of type E under a parentTry it
dd:first-of-typeFirst dd among siblings in a dlTry it
vs :first-childFirst of that tag vs must be first child overallTry it
Nestedarticle :first-of-type matches inside each parentTry it

How to Use :first-of-type

Pick the tag you care about, then select the first sibling of that type.

  1. Look at siblings under one parent (DevTools helps).
  2. Write E:first-of-type for the first element of type E.
  3. Style that first item (color, border, spacing).
  4. If it must also be the overall first child, use :first-child.
PieceRequired?Role
Parent with siblingsYesMatching is relative to one parent
:first-of-typeYesFirst sibling of that element’s tag
Type selectorOptional*Usually written as p:first-of-type; bare :first-of-type implies *

*MDN: with no type written, the universal selector is implied.

Live Preview

Toggle between p:first-of-type and p:first-child on the same markup (heading before paragraphs).

Compare selectors

With an h2 first, only p:first-of-type highlights a paragraph.

Article

Heading

Paragraph 1

Paragraph 2

p:first-of-type matches Paragraph 1.

CSS Examples

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

Example 1 — First Paragraph After a Heading

Style the first p even when an h2 comes first.

CSS
<style>
  p:first-of-type {
    color: red;
    font-style: italic;
  }
</style>

<h2>Heading</h2>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
Try It Yourself

How It Works

  1. Siblings. The h2 and both p elements share one parent.
  2. First of type. Among p siblings, Paragraph 1 is first.
  3. Heading ignored for type. The h2 does not stop p:first-of-type.
  4. Contrast. p:first-child would match nothing here.

Example 2 — First dd After Each Term

Highlight the first definition under each term.

CSS
<style>
  dt {
    font-weight: bold;
  }

  dd:first-of-type {
    border: 2px solid orange;
  }
</style>

<dl>
  <dt>Vegetables:</dt>
  <dd>Tomatoes</dd>
  <dd>Cucumbers</dd>
  <dt>Fruits:</dt>
  <dd>Apples</dd>
  <dd>Mangos</dd>
</dl>
Try It Yourself

How It Works

  1. Same parent. All dt/dd share the dl.
  2. First dd only. dd:first-of-type matches the first dd in that sibling list (Tomatoes).
  3. Later dds. Cucumbers, Apples, and Mangos are not the first of type.
  4. Note. There is only one “first dd” under the whole dl—not one per dt.

Example 3 — Nested First-of-Type

Each parent can have its own first-of-type match.

CSS
<style>
  article :first-of-type {
    background-color: pink;
  }
</style>

<article>
  <div>This div is first!</div>
  <div>This <span>nested span is first</span>!</div>
  <p>This p qualifies!</p>
  <div>This is the final div.</div>
</article>
Try It Yourself

How It Works

  1. Descendant selector. article :first-of-type looks for first-of-type under article.
  2. Outer siblings. The first div and the only p match among article children.
  3. Nested. The span is the first (and only) of its type inside its parent div.
  4. Implied *. Writing :first-of-type without a tag means any type.

Edge Cases & Pitfalls

Confusion with :first-child causes most mistakes.

vs child

Not the same as :first-child

Need the absolute first child of any tag? Use :first-child.

dl

One first dd per parent

dd:first-of-type matches the first dd among all siblings in the dl, not the first under each dt.

alias

Same as :nth-of-type(1)

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

scope

Per parent, not per page

Each parent has its own sibling list. Nested containers can each match.

only

Single of that type still matches

One p among siblings is still first of type. For “exactly one of that type,” use :only-of-type.

Browser Support

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

✓ Baseline · Widely available

:first-of-type

Supported in all modern browsers (Chrome 1+, Firefox 3.5+, Safari 3.1+, Edge 12+, IE 9+).

98% Global support
Google Chrome 1+
Full support
Mozilla Firefox 3.5+
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 9+
Full support
:first-of-type Widely supported

Bottom line: Safe for production structural styling. It is not a JavaScript runtime API.

Key Takeaways

  • Role: match the first sibling of a given tag type.
  • Form: E:first-of-type = first E among siblings.
  • vs :first-child: of-type allows other tags before the match.
  • Alias: same match as :nth-of-type(1).

One line: :first-of-type styles the first element of that tag among siblings—even if another tag sits before it.

Frequently Asked Questions

It matches an element that is the first sibling of its tag type inside its parent. Other element types before it do not block the match.
:first-child requires the element to be the first child of any type. :first-of-type matches the first p, first li, or first div of that tag among siblings, even if another tag comes first.
Yes. If the parent contains an h2 then paragraphs, p:first-of-type matches the first p even though the h2 is the overall first child.
Yes. :first-of-type matches the same elements as :nth-of-type(1).
Yes. Each parent has its own sibling group. article :first-of-type can match inside every nested parent, not only once on the page.
No. It is a CSS selector evaluated by the browser layout engine, not a Node.js or server runtime feature.

Next: :focus

Style elements when they receive keyboard or pointer focus.

:focus 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