Aller au contenu

La pseudo-classe CSS :first-of-type

La pseudo-classe CSS :first-of-type sélectionne un élément qui est le premier de son type parmi ses frères et sœurs. Elle est équivalente à [:nth-of-type(1)](/learn-css/nth-of-type.html).

Exemple de la pseudo-classe CSS :first-of-type

Le sélecteur :first-of-type est souvent comparé à :first-child, mais ils diffèrent par leur portée. Alors que :first-child correspond à un élément en fonction de sa position parmi tous les enfants, quel que soit leur type, :first-of-type ne correspond que si l'élément est le premier parmi les frères et sœurs du même type d'élément. Les deux sélecteurs partagent la même spécificité CSS.

DANGER

À partir du niveau 4 des sélecteurs, l'élément sélectionné ne nécessite pas strictement un nœud parent dans le DOM pour correspondre.

Version

Selectors Level 3

Selectors Level 4

Syntaxe

Syntaxe CSS :first-of-type

css
:first-of-type {
  css declarations;
}

Exemple du sélecteur :first-of-type :

html
<!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>

Exemple du sélecteur :first-of-type avec la balise <article> :

html
<!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>

Exemple du sélecteur :first-of-type avec certaines balises HTML :

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>

Compatibilité navigateurs

NavigateurVersion
Chrome1.0
Firefox1.0
Safari1.0
Edge12.0
Opera7.0
IE9.0

Pratique

Quelle est la définition et l'utilisation correctes de la pseudo-classe CSS :first-of-type ?

Trouvez-vous cela utile?

Aperçu dual-run — comparez avec les routes Symfony en production.