English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

CSS Reference Manual

CSS @rules

Comprehensive CSS Properties

CSS3 :only-child selector

CSS pseudo-class:only-The child selector matches elements that have no siblings. Equivalent selectors can also be written as :first-child:last-child or :nth-child(1:nth-last-child(1), of course, the weight of the former will be a bit lower.

Complete CSS Selector Reference Manual

Online Example

Match the p element that is the only child of the parent element:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>Basic Tutorial(oldtoolbag.com)</title>
<style>
div p:only-child {
  color: red;
}
</style>
</head>
<body>
<div>
  <p>This paragraph is the only child of its parent element</p>
</div>
<div>
<p>This paragraph is the first child of its parent element</p>
<p>This paragraph is the second child of its parent element</p>
</div>
</body>
</html>
Test and see ‹/›

Definition and Usage

:only-child

:only-The child selector matches elements that are the only child of a parent element.

Browser Compatibility

The numbers in the table indicate the first browser version that supports this attribute.

Selector




:only-child4.09.03.53.29.6

Complete CSS Selector Reference Manual