259 of 313 menu

The only-of-type pseudo-class

The only-of-type pseudo-class selects an element that is the only child of a given type.

Syntax

selector:only-of-type { }

Example

Let's color the h2 heading red if it is the only h2 in its parent:

<div> <h2>header</h2> <p>paragraph</p> <p>paragraph</p> <p>paragraph</p> </div> <div> <h2>header</h2> <p>paragraph</p> <h2>header</h2> <p>paragraph</p> <p>paragraph</p> </div> div { border: 1px dashed black; padding: 15px; margin-bottom: 10px; } h2:only-of-type { color: red; }

:

byenru