CSS :only-child pseudo-class

Definition and usage

CSS :only-child Pseudo-classes are used to select and set the styles of any element that is the only child of its parent element.

Example

Specify styles for any <p>, <li>, and <b> element that is the only child of its parent element:

p:only-child {
  background-color: red;
}
li:only-child {
  color: salmon;
}
b:only-child {
  color: green;
}

Try it yourself

CSS syntax

:only-child {
  css declarations;
}

Technical details

Version: CSS3

Browser support

The numbers in the table specify the first browser version to fully support the pseudo-class.

Chrome Edge Firefox Safari Opera
4.0 9.0 3.5 3.2 9.6