Aller au contenu

Pseudo-classe CSS :only-child

La pseudo-classe :only-child sélectionne un élément s’il est le seul enfant de son parent.

L’élément est sélectionné uniquement si son parent n’a aucun autre enfant de quelque type que ce soit. La pseudo-classe :only-child est équivalente à :first-child:last-child et a la même spécificité (0,2,0).

Version

Selectors Level 4

Selectors Level 3

Syntaxe

Syntaxe de CSS :only-child

css
:only-child {
  css declarations;
}

Exemple du sélecteur :only-child :

Exemple de :only-child

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

Exemple du sélecteur :only-child pour la balise <li> :

Exemple de :only-child avec la balise li

html
<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      ul li {
        list-style-type: square;
      }
      li:only-child {
        color: blue;
        list-style-type: disc;
      }
    </style>
  </head>
  <body>
    <h2>:only-child selector example</h2>
    <ol>
      <li>
        One element
        <ul>
          <li>This list has just one element.</li>
        </ul>
      </li>
      <li>
        Two elements
        <ul>
          <li>This list has two elements.</li>
          <li>This list has two elements.</li>
        </ul>
      </li>
      <li>
        Three elements
        <ul>
          <li>This list has three elements.</li>
          <li>This list has three elements.</li>
          <li>This list has three elements.</li>
        </ul>
      </li>
    </ol>
  </body>
</html>

Exemple du sélecteur :only-child pour la balise <em> :

Exemple de :only-child avec la balise em

html
<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      em:only-child {
        background-color: #8ebf42;
      }
    </style>
  </head>
  <body>
    <h2>:only-child selector example</h2>
    <p>This is a <em>paragraph</em>.</p>
    <p>This is a paragraph.</p>
  </body>
</html>

Exemple du sélecteur :only-child pour la balise <div> :

Exemple de :only-child pour la balise div

html
<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      div:only-child {
        color: #8ebf42;
        font-weight: bold;
      }
      div {
        display: block;
        margin: 6px;
        padding: 5px;
        outline: 1px solid #1c87c9;
      }
      div div {
        display: inline-block;
      }
    </style>
  </head>
  <body>
    <h2>:only-child selector example</h2>
    <div>
      <div>I am an only child.</div>
    </div>
    <div>
      <div>I am the 1st sibling.</div>
      <div>I am the 2nd sibling.</div>
      <div>
        I am the 3rd sibling,
        <div>but this is an only child.</div>
      </div>
    </div>
  </body>
</html>

Practice

Le pseudo-sélecteur :only-child sélectionne un élément

Trouvez-vous cela utile?

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