CSS | :only-of-type Selector

The :only-of-type selector in CSS represents only those element that has no siblings of the given type. It is used to set the CSS property to that element.

Syntax:

:only-of-type {
    // CSS property
}

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>:only-of-type selector</title>
        <style
            h1 {
                color:green;
            }
            body {
                text-align:center;
            }
            p:only-of-type {
                color:green;
                font-size:25px;
            }
        </style>
    </head>
    <body>
        <h1>GeeksforGeeks</h1>
        <h2>:only-of-type selector</h2>
        <div><p>Single child of its parent</p></div>
        <div>
            <p>Child 1 of its parent</p>
            <p>Child 2 of its parent</p>
        </div>
    </body>
</html>                    

chevron_right


Output:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html> 
<html
    <head
        <title>:only-of-type selector</title
        <style
            h1 { 
                color:green; 
            
            body {
                text-align:center;
            }
            input:only-of-type {
                background-color:green;
                border:1px solid black;
                color:white;
                text-align:center;
            }
        </style
    </head
    <body>
        <h1>GeeksforGeeks</h1>
        <h2>:only-of-type selector</h2>
        <div>
            <input type="text" value="Only child">
        </div><br>
        <div>
            <input type="text" value="Child 1 of its parent">
            <input type="text" value="Child 2 of its parent">
        </div>
    </body
</html>                    

chevron_right


Output:

Supported Browser: The browser supported by :only-of-type selector are listed below:

  • Chrome 4.0
  • Opera 9.6
  • Firefox 3.5
  • Internet Explorer 9.0
  • Safari 3.2


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.