Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

The :first-of-type pseudo-class

:first-of-type selects the first element of its type among siblings, which is not necessarily its parent's first child.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

E:first-of-type { /* declarations */ }

:first-of-type matches an element with no preceding sibling of the same type: the same local name and namespace. In HTML, this usually means the same tag name. It is equivalent to :nth-of-type(1) and takes no arguments.

p:first-of-type can match a paragraph preceded by a heading; p:first-child requires the paragraph to be the first element among the children. The condition is evaluated separately within each sibling group, not once for the entire document.

Caution: .note:first-of-type does not mean the first element with class note. The element must have that class and be first among siblings of its own type. In the example, #b does not match because another p, namely #a, precedes it.

Examples

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>first-of-type</title>
<style>
  #demo > p:first-of-type { color: rgb(180, 0, 0); }
</style>
<section id="demo">
  <h2>Heading</h2>
  <p id="a">Alpha</p>
  <p id="b" class="note">Beta</p>
</section>
</html>

Result

#demo > p:first-of-type → #a
#demo > p:first-child → (none)
#demo > .note:first-of-type → (none)

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources