W3docs

Pseudo-classe CSS :first-child

Usa la pseudo-classe CSS :first-child per selezionare e applicare stili al primo figlio tra gli altri elementi fratelli. Scopri la pseudo-classe ed esercitati con gli esempi.

La pseudo-classe CSS :first-child seleziona l'elemento se è il primo figlio tra gli altri elementi.

 Esempio della pseudo-classe CSS :first-child

Il selettore :first-of-type può essere usato se vuoi selezionare e applicare lo stile al primo paragrafo. Il selettore :first-child è in realtà simile a :first-of-type, ma c'è una differenza: i due corrispondono a condizioni diverse. :first-child seleziona un elemento solo se è il primo figlio del suo genitore, mentre :first-of-type seleziona il primo elemento del suo tipo specifico tra i suoi fratelli.

Versione

Selectors Level 4

Selectors Level 3

CSS Level 2

Sintassi

Esempio di sintassi CSS :first-child

:first-child {
  css declarations;
}

Esempio della pseudo-classe :first-child con il tag <p>:

Esempio di codice CSS :first-child

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      p:first-child {
        background-color: #1c87c9;
        color: #fff;
      }
    </style>
  </head>
  <body>
    <p>Lorem ipsum is simply dummy text...</p>
    <h2>First-child selector example</h2>
    <p>Lorem Ipsum is simply dummy text...</p>
  </body>
</html>

Esempio della pseudo-classe :first-child con il tag <li>:

Altro esempio di codice CSS :first-child

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      li:first-child {
        background: #8ebf42;
      }
    </style>
  </head>
  <body>
    <h2>:first-child selector example</h2>
    <ul>
      <li>Paris</li>
      <li>Moscow</li>
      <li>Rome</li>
    </ul>
    <ol>
      <li>Paris</li>
      <li>Moscow</li>
      <li>Rome</li>
    </ol>
  </body>
</html>

Esempio della pseudo-classe :first-child con il tag <ol>:

Esempio del selettore :first-child con il tag HTML ol

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      ol:first-child {
        background: #8ebf42;
      }
    </style>
  </head>
  <body>
    <ol>
      <li>London</li>
      <li>Paris</li>
      <li>Rome</li>
    </ol>
    <ol>
      <li>London</li>
      <li>Paris</li>
      <li>Rome</li>
    </ol>
  </body>
</html>

Esempio della pseudo-classe :first-child con il tag <em>:

Esempio del selettore :first-child con il tag HTML em

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      p em:first-child {
        background: #82b534;
      }
    </style>
  </head>
  <body>
    <h2>:first-child selector example</h2>
    <article>
      <p>Here is a <em>some</em> text. This is a <em>example</em>.</p>
      <p>Here is a <em>some</em> text. This is a <em>example</em>.</p>
      <p>Here is a <em>some</em> text. This is a <em>example</em>.</p>
    </article>
  </body>
</html>

Esempio della pseudo-classe :first-child con il tag <ul>:

Esempio del selettore :first-child con il tag HTML ul

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      ul li {
        color: blue;
      }
      ul li:first-child {
        color: #8ebf42;
        font-weight: bold;
      }
    </style>
  </head>
  <body>
    <h2>:first-child selector example</h2>
    <ul>
      <li>List Item 1</li>
      <li>List Item 2</li>
      <li>
        List Item 3
        <ul>
          <li>List Item 3.1</li>
          <li>List Item 3.2</li>
          <li>List Item 3.3</li>
        </ul>
      </li>
    </ul>
  </body>
</html>

Pratica

Pratica
What does the :first-child pseudo-class represent in CSS?
What does the :first-child pseudo-class represent in CSS?
Was this page helpful?