W3docs

Pseudo-classe CSS :first-of-type

La pseudo-classe CSS :first-of-type seleziona il primo elemento del suo tipo tra gli altri elementi fratelli. Scopri la pseudo-classe e prova gli esempi.

La pseudo-classe CSS :first-of-type seleziona un elemento che è il primo del suo tipo tra i suoi fratelli. È equivalente a [:nth-of-type(1)](/learn-css/nth-of-type).

Esempio della pseudo-classe CSS :first-of-type

Il selettore :first-of-type viene spesso confrontato con :first-child, ma i due differiscono nell'ambito di applicazione. Mentre :first-child seleziona un elemento in base alla sua posizione tra tutti i figli indipendentemente dal tipo, :first-of-type lo seleziona solo se è il primo tra i fratelli dello stesso tipo di elemento. Entrambi i selettori hanno la stessa specificità CSS.

Pericolo

A partire da Selectors Level 4, l'elemento selezionato non richiede necessariamente un nodo genitore nel DOM per essere selezionato.

Versione

Selectors Level 3

Selectors Level 4

Sintassi

Sintassi CSS :first-of-type

:first-of-type {
  css declarations;
}

Esempio del selettore :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>

Esempio del selettore :first-of-type con il 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>

Esempio del selettore :first-of-type con alcuni tag 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à con i browser

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

Pratica

Pratica
What is the correct definition and use of the :first-of-type CSS pseudo-class?
What is the correct definition and use of the :first-of-type CSS pseudo-class?
Was this page helpful?