The pseudo-class CSS :only-of-type
representa un element que no té germans del mateix tipus.
/* Selecciona cada <p>, però només si és l'únic element <p> dins del seu pare */
p:only-of-type {
background-color: lime;
}
Nota: Com es va definir originalment, l'element seleccionat havia de tenir un pare. A partir dels Selectores Nivell 4, això ja no és necessari.
Sintaxi
:only-of-type
Exemple
HTML
<main>
<div>I am `div` #1.</div>
<p>I am the only `p` among my siblings.</p>
<div>I am `div` #2.</div>
<div>I am `div` #3.
<i>I am the only `i` child.</i>
<em>I am `em` #1.</em>
<em>I am `em` #2.</em>
</div>
</main>
CSS
main :only-of-type {
color: red;
}
Resultat
Especificacions
Especificació | Estat | Comentari |
---|---|---|
Selectors Level 4 The definition of ':only-of-type' in that specification. |
Working Draft | No es requereix que els elements coincidents tinguin un pare. |
Selectors Level 3 The definition of ':only-of-type' in that specification. |
Recommendation | Definició inicial. |
Navegadors compatibles
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help!
Descripció | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Suport bàsic | 1.0 | (Yes) | 3.5 (1.9.1) | 9.0 | 9.5 | 3.2 |
Descripció | Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Suport bàsic | 2.1 | (Yes) | 1.0 (1.9.1) | 9.0 | 10.0 | 3.2 |