CSS Reference ยท Pseudo-classes
The :first-of-type pseudo-class
Reference for the CSS :first-of-type pseudo-class: select the first sibling with a given tag name, with syntax, examples, comparison with :first-child and support.
The :first-of-type pseudo-class matches an element that is the first among its siblings with the same tag name. In a parent holding a heading, three paragraphs and an image, p:first-of-type picks the first paragraph, even though it is not the first child.
That is the key difference from :first-child, which cares only about position, not tag.
p:first-of-type { font-size: 1.25rem; }
Try it
Syntax
:first-of-type { /* declarations */ }
No arguments; specificity (0,1,0). Used alone (:first-of-type) it matches the first of each tag name in every parent.
Syntax and arguments
| Part | Description |
|---|---|
:first-of-type |
Matches an element with no preceding sibling of the same element type. |
| No arguments | The pseudo-class is not functional. |
| Equivalent | :nth-of-type(1) |
| Type | The tag name (and namespace); classes do not count. |
Usage notes
- Classes are ignored.
p.note:first-of-typemeans "apwith class note that is also the firstpamong its siblings", not "the first.note". If the firstphas nonoteclass, nothing matches. - Different tags don't interfere. A
<div>before your<p>does not stopp:first-of-type. - Typical uses. Lead paragraphs, removing margin from the first heading or figure in a block, and styling the first image in an article.
- Parent matters. The count restarts inside every parent, so each section can have its own first paragraph.
- Choosing the right tool. Use
:first-childfor "first thing in the box",:first-of-typefor "first of this kind".
Browser support
Supported in every browser.
Related
:last-of-typeโ last of its tag name:first-childโ first sibling of any tag:nth-of-type()โ position among siblings of the same tag- Lesson: Structural pseudo-classes