English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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
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 ‹/›
:only-child
:only-The child selector matches elements that are the only child of a parent element.
The numbers in the table indicate the first browser version that supports this attribute.
Selector | |||||
---|---|---|---|---|---|
:only-child | 4.0 | 9.0 | 3.5 | 3.2 | 9.6 |