The :Only-of-Type Pseudo-Class

The :only-of-type selector matches every element that is the only child of its type, of its parent. In this example, the selector specifies a background color for every <p> element that is the only child of its type, of its parent:
HTML file: Displayed by browser:
<style>
p:only-of-type {
    background: #ff0000;
}
</style>
<body>
<div><p>This is a paragraph.</p></div>
<div><p>This is a paragraph.</p>
<p>This is a paragraph.</p></div>
</body>

This is a paragraph.

This is a paragraph.

This is a paragraph.


There is only one paragraph in the first div element, but two in the second div element.

Back button Table of Contents Next button