Pseudoklasse only-of-type

Der Pseudoklasse only-of-type wählt ein Element aus, das das einzige Kindelement des angegebenen Typs ist.

Syntax

cssSelektor:only-of-type {
  
}

Beispiel

Färben wir eine h2-Überschrift rot, wenn sie die einzige h2 in ihrem übergeordneten Element ist:

html<div>
  <h2>Überschrift</h2>
  <p>Absatz</p>
  <p>Absatz</p>
  <p>Absatz</p>
</div>
<div>
  <h2>Überschrift</h2>
  <p>Absatz</p>
  
  <h2>Überschrift</h2>
  <p>Absatz</p>
  <p>Absatz</p>
</div>
cssdiv {
  border: 1px dashed black;
  padding: 15px;
  margin-bottom: 10px;
}
h2:only-of-type {
  color: red;
}

Результат: