Abakada: Back to Basics > Language References > CSS Properties > Pseudo-Classes > only-child Pseudo-class
Represents an element without any siblings.
selector:only-child { property: value; }
property |
Any valid CSS property. |
value |
Any of the range of values available to the corresponding property. |
The :only-child pseudo-class applies styles to elements that have no siblings.
The syntax for using the :only-child pseudo-class is straightforward. A selector immediately precedes the pseudo-class.
For example, to change the background color of only-child elements on a page.
div:only-child { background-color: beige }
The following demonstrates use of the :only-child pseudo-class in an embedded stylesheet to set the style of only-child elements on a page.
:first-child( ) :last-child( ) :nth-child( ) :nth-last-child( )