日本語

CSS :first-child vs :first-of-type

​​CSS​HTML,Playwright

:first-child matches an element that is the first among its sibling elements. :first-of-type counts only the siblings with the same element name (such as p or li) and matches the first of those. These two pseudo-classes are what you use when you want to remove the top margin of only the first paragraph in an article, or remove the line from only the first item of a list. If another element, such as a heading, comes before the paragraph, p:first-child does not match. Changing it to :first-of-type makes it match.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and Chromium 153. I opened the HTML with Playwright, added outline through a selector, and checked which elements received it with getComputedStyle(). I also checked the counts from document.querySelectorAll(). I did not test Firefox or Safari.

The result differs after a heading

I compared the two selectors on HTML that has one heading and three paragraphs inside an article. The text inside the examples is Japanese. In the explanations below, "Paragraph 1" means the first p in the HTML, "Paragraph 2" the second, and so on.

html
<article>
  <h2>見出し</h2>
  <p>段落1</p>
  <p>段落2</p>
  <p>段落3</p>
</article>
css
article p:first-child { /* matches none */ }
article p:first-of-type { /* matches the first paragraph */ }
Two screens stacked vertically. In the top one, with p:first-child, none of the h2 and the three p elements inside article change color. In the bottom one, with p:first-of-type, only the p for Paragraph 1 has a pink border and background. The labels in the image are in Japanese.
Result of applying the two selectors to HTML where a paragraph follows a heading

p:first-child did not match any paragraph. This selector means "an element that is a p and is also the first among its siblings." The first child is h2, which does not satisfy the p condition. p:first-of-type counts only the p siblings, so it matched Paragraph 1.

When the first element is a paragraph, both selectors match the same element. With <article><p>段落1</p><p>段落2</p></article>, both p:first-child and p:first-of-type matched Paragraph 1. This difference is why a :first-child rule stops working as soon as you add a heading or another element before the paragraph.

The MDN page for :first-child describes it as "the first element in a group of sibling elements," and the MDN page for :first-of-type describes it as "the first element of its type (tag name) in a group of sibling elements."

"Type" means the element name, not the class name

The "type" in :first-of-type is the element name. Combining it with a class name does not mean "first within that class." I checked this with the following HTML.

html
<div class="list">
  <p>前書き</p>
  <p class="note">注意A</p>
  <p class="note">注意B</p>
  <div class="note">注意C</div>
</div>

.note:first-of-type matched Note C, not Note A. This selector means "an element that has the note class and is the first of its element name among its siblings." The first p is the intro paragraph, which does not have the note class. The first div is Note C, which has the note class, so it matches. .note:first-child matched none, because the first child, the intro paragraph, does not have the note class.

Two screens stacked vertically. In the top one, with .note:first-of-type, the last of the four children, the div with class note for Note C, is pink. In the bottom one, with :nth-child(1 of .note), the second child, the p with class note for Note A, is pink. The labels in the image are in Japanese.
Applying .note:first-of-type and :nth-child(1 of .note) to the same HTML

To select the first element by class name, use of with :nth-child(). :nth-child(1 of .note) counts only the siblings that match .note and matches the first of them.

css
.list > :nth-child(1 of .note) {
  /* matches Note A */
}

In the Chromium version I tested, this matched Note A. This syntax is defined in the Selectors Level 4 spec for :nth-child(). The spec's examples show that :nth-child(-n+3 of li.important) matches the first three li elements that have the important class, while li.important:nth-child(-n+3) matches only when the first three children happen to be important. You can check browser support in the compatibility table on the MDN page for :nth-child().

Without an element name, it matches the first of each element name

If you write :first-of-type without an element name, it matches the first element of each element name among the siblings. As the MDN description says, omitting the element name is the same as having the universal selector (*).

html
<article>
  <h2>見出し</h2>
  <p>段落1</p>
  <p>段落2</p>
  <ul>
    <li>項目1</li>
    <li>項目2</li>
  </ul>
</article>
Selector Matched elements
article > :first-child h2
article > :first-of-type h2, the p for Paragraph 1, ul
article :first-of-type (all descendants) h2, the p for Paragraph 1, ul, the li for Item 1

When I wrote a descendant selector without >, Item 1 inside ul also matched, because it is the first li within its parent. :first-child and :first-of-type count only among siblings that share the same parent.

In nested lists, the first item of the inner list also matches

Since counting happens only among siblings, in a nested list the first item of an inner list also matches.

html
<ul class="menu">
  <li>親1
    <ul>
      <li>子1</li>
      <li>子2</li>
    </ul>
  </li>
  <li>親2</li>
</ul>

.menu li:first-child matched both Parent 1 and Child 1. If you want only the first item of the outer list, limit the parent with a child combinator (>), as in .menu > li:first-child. This matched only Parent 1.

Text and comments are not counted

Text or comments before an element are not included when :first-child counts. When I tested span:first-child on <div>テキスト<!-- コメント --><span>A</span><span>B</span></div>, it matched A. The structural pseudo-classes section of the Selectors Level 4 spec also says that when counting a position among a parent's children, things that are not elements, such as text, are not counted.

The same difference appears for the second or the last element

The spec's definition of :first-child says :first-child is the same as :nth-child(1), and its definition of :first-of-type says :first-of-type is the same as :nth-of-type(1). So the same difference appears when you select the second or the last element. I compared them on the first HTML, with a heading followed by three paragraphs, and on HTML with two paragraphs followed by a footer.

Selector HTML Matched element
p:nth-child(2) h2, p×3 Paragraph 1
p:nth-of-type(2) h2, p×3 Paragraph 2
p:last-child p×2, footer none
p:last-of-type p×2, footer Paragraph 2

p:nth-child(2) counts h2 as well, so it is the second child and matched Paragraph 1. If you want the second paragraph among only the paragraphs, use p:nth-of-type(2). If you want to remove the bottom margin of the last paragraph but p:last-child does not match because a footer follows it, changing it to p:last-of-type makes it match.

If you are unsure which to use, decide whether you want to count "all children of the parent" or "only the elements with the same element name." If you want to count only the elements with the same class rather than the same element name, use :nth-child(1 of .クラス名).

​Posted by 86