HTML CSS examples for CSS Selector:only-child
The :only-child selector selects element that is the only child of its parent.
Select every <p> element that is the only child of its parent:
<!DOCTYPE html> <html> <head> <style> p:only-child {<!--from w w w. j av a2s.com--> background: red; } </style> </head> <body> <p>This is a paragraph.</p> <div><p>This is a paragraph.</p></div> <div><span>This is a span.</span><p>This is a paragraph.</p></div> </body> </html>