Pseudo-classe only-of-type
La pseudo-classe only-of-type sélectionne
un élément qui est le seul enfant
de son type.
Syntaxe
csssélecteur:only-of-type {
}
Exemple
Colorons le titre h2 en rouge si
il est le seul h2 dans son parent :
html<div>
<h2>titre</h2>
<p>paragraphe</p>
<p>paragraphe</p>
<p>paragraphe</p>
</div>
<div>
<h2>titre</h2>
<p>paragraphe</p>
<h2>titre</h2>
<p>paragraphe</p>
<p>paragraphe</p>
</div>cssdiv {
border: 1px dashed black;
padding: 15px;
margin-bottom: 10px;
}
h2:only-of-type {
color: red;
}
Результат: