Pseudo-classe CSS :first-of-type
A pseudo-classe CSS :first-of-type seleciona o primeiro elemento do seu tipo entre os outros elementos irmãos. Saiba mais sobre a pseudo-classe e experimente os exemplos.
A pseudo-classe CSS :first-of-type seleciona um elemento que é o primeiro do seu tipo entre os seus irmãos. É equivalente a [:nth-of-type(1)](/learn-css/nth-of-type).

O seletor :first-of-type é frequentemente comparado com :first-child, mas diferem no âmbito. Enquanto :first-child seleciona um elemento com base na sua posição entre todos os filhos, independentemente do tipo, :first-of-type só o seleciona se for o primeiro entre os irmãos do mesmo tipo de elemento. Ambos os seletores partilham a mesma especificidade CSS.
A partir do Selectors Level 4, o elemento selecionado não exige estritamente um nó pai no DOM para ser selecionado.
Versão
Sintaxe
Sintaxe CSS :first-of-type
:first-of-type {
css declarations;
}Exemplo do seletor :first-of-type:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
p:first-of-type {
background: #8ebf42;
}
</style>
</head>
<body>
<h2>:first-of-type selector example</h2>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
<p>Paragraph 3</p>
</body>
</html>Exemplo do seletor :first-of-type com a tag <article>:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
p:first-of-type {
font-size: 22px;
color: #777777;
}
</style>
</head>
<body>
<h2>:first-of-type selector example</h2>
<article>
<h2>This is a title.</h2>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. </p>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. .</p>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. .</p>
</article>
</body>
</html>Exemplo do seletor :first-of-type com algumas tags HTML:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
p {
margin: 0;
}
article:first-of-type {
background-color: #777777;
color: #ffffff;
}
</style>
</head>
<body>
<h2>:first-of-type selector example</h2>
<article>
<p>This is a first element!</p>
<p>This <em>nested 'em' is first</em>!</p>
<p>This <strong>nested 'strong' is first</strong>, but this <strong>nested 'strong' is last</strong>!</p>
<p>This <span>nested 'span' gets styled</span>!</p>
<q>This is a 'q' element!</q>
<p>This is a last element.</p>
</article>
<article>
<p>This is a second article.</p>
</article>
</body>
</html>Compatibilidade com browsers
| Browser | Versão |
|---|---|
| Chrome | 1.0 |
| Firefox | 1.0 |
| Safari | 1.0 |
| Edge | 12.0 |
| Opera | 7.0 |
| IE | 9.0 |