CSS | :only-child Selector

The :only-child selector in CSS is used to match every element that is the only child of its parent. It represents an element without any siblings.

Syntax:

:only-child {
    // CSS property
} 

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>:only-child selector</title>
        <style>
            h1 {
                color: green;
                text-align: center;
            }
            h2 {
                text-align: center;
            }
            div:only-child {
                color: white;
                background: green;
            }
            div {
                display: block;
                margin: 6px;
                font-size: 17px;
                padding: 5px 8px;
                border: solid 2px grey;
            }
        </style>
    </head>
    <body>
        <h1>GeeksforGeeks</h1>
        <h2>:only-child selector</h2>
        <div>
            <div>I am an only child.</div>
        </div>
        <div>
            <div>I am the 1st child.</div>
            <div>I am the 2nd child.</div>
            <div>I am the 3rd child, <div> Only 
            child of parent.</div></div>
        </div>
    </body>
</html>

chevron_right


Output:
only-child

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>:only-child selector</title>
        <style>
            h1 {
                color: green;
                text-align: center;
            }
            h2 {
                text-align: center;
            }
            li:only-child {
            color: green;
            }
            li {
                font-size: 20px;
            }
        </style>
    </head>
    <body>
        <h1>GeeksforGeeks</h1>
        <h2>:only-child selector</h2>
        <ol>
            <li>Data Structures</li>
            <ul>
                <li>Arrays </li>
            </ul>
            <li>Languages</li>
            <ul>
                <li>C++ </li>
                <li>Python</li>
            </ul>
            <li>Algorithms</li>
            <ul>
                <li>Searching Algorithms</li>
                <li>Sorting Algorithms</li>
                <ul>
                    <li>Bubble sort </li>
                </ul>
            </ul>
        </ol>
    </body>
</html>

chevron_right


Output:
only-child2

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

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


My Personal Notes arrow_drop_up


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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.