:only-of-type
Quick Summary for :only-of-type
The
:only-of-type
CSS pseudo-class represents an element that has no siblings of the same type.
Code Usage for :only-of-type
/* Selects each <p>, but only if it is the */ /* only <p> element inside its parent */ p:only-of-type { background-color: lime; }
More Details for :only-of-type
:only-of-type
The :only-of-type
CSS pseudo-class represents an element that has no siblings of the same type.
/* Selects each <p>, but only if it is the */ /* only <p> element inside its parent */ p:only-of-type { background-color: lime; }
Note: As originally defined, the selected element had to have a parent. Beginning with Selectors Level 4, this is no longer required.
Syntax
:only-of-type
Examples
Styling elements with no siblings of the same type
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; }
ResultSpecifications
Specification |
---|
Selectors Level 4 # only-of-type-pseudo |
See also
:only-child
:first-of-type
:last-of-type
:nth-of-type
Last modified: Aug 12, 2021, by MDN contributors
Select your preferred language English (US)EspañolFrançais日本語Português (do Brasil)Русский中文 (简体) Change language Posted in
12:22 am, February 22, 2022
Add Comment
Other Items in CSS
rgb()
rgba()
:right
right
@right-bottom
:root
rotate
rotate()
rotate3d()
rotateX()
rotateY()
rotateZ()
row-gap
ruby-align
ruby-position
saturate()
scale
scale()
scale3d()
scaleX()
scaleY()
scaleZ()
:scope
scroll-behavior
scroll-margin
scroll-margin-block
scroll-margin-block-end
scroll-margin-block-start
scroll-margin-bottom
scroll-margin-inline
scroll-margin-inline-end
scroll-margin-inline-start
scroll-margin-left
scroll-margin-right
scroll-margin-top
scroll-padding
scroll-padding-block
scroll-padding-block-end
scroll-padding-block-start
scroll-padding-bottom
scroll-padding-inline
scroll-padding-inline-end
scroll-padding-inline-start
scroll-padding-left
scroll-padding-right
scroll-padding-top
scroll-snap-align
scroll-snap-stop
scroll-snap-type
@scroll-timeline