CSS only-child Selector :only-child

What is CSS :only-child Selector?

CSS :only-child selector matches only single child element of an parent element.

Syntax

General syntax of :only-child selector,

 child:only-child {   property: value;   property: value;   ... }  /* nth number of list item */ parent child:only-child {   property: value;   property: value;   ... }

Example

CSS li:only-child selector select only one <li> element child of an <ul> parent element.

 <!DOCTYPE html> <html> <head>   <title>CSS :only-child Selector</title>   <style type="text/css">     li:only-child {       background: #ff0000;     }   </style> </head> <body>   <ul>     <li>first item</li>   </ul>   <ul>     <li>first item</li>     <li>second item</li>     <li>third item</li>     <li>forth item</li>     <li>fifth item</li>   </ul> </body> </html>

Browser Compatibility

  • Google Chrome 2+
  • Mozilla Firefox 3+
  • Internet Explorer 9+
  • Opera 9.5+
  • Safari 4+

Note: Here details of browser compatibility with version number may be this is bug and not supported. But recommended to always use latest Web browser.