:only-of-type
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
La pseudo-clase :only-of-type
CSS representa un elemento que no tiene hermanos del mismo tipo.
css
/* Selecciona cada <p>, pero solo si es el único */
/* elemento <p> dentro de su padre */
p:only-of-type {
background-color: lime;
}
Nota: Como se definió originalmente, el elemento seleccionado tenía que tener un padre. Comenzando con el Nivel 4 de Selectores, esto ya no es necesario.
Sintaxis
Error: could not find syntax for this item
Ejemplo
HTML
html
<main>
<div>Soy un `div` #1.</div>
<p>Yo soy el único `p` entre mis hermanos.</p>
<div>Soy un `div` #2.</div>
<div>
Soy un `div` #3.
<i>Yo soy el único hijo `i`.</i>
<em>Soy un `em` #1.</em>
<em>Soy un `em` #2.</em>
</div>
</main>
CSS
css
main :only-of-type {
color: red;
}
Resultado
Especificaciones
Specification |
---|
Selectors Level 4 # only-of-type-pseudo |
Compatibilidad con navegadores
Report problems with this compatibility data on GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
:only-of-type |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
- See implementation notes.
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.