Open In App

CSS :nth-of-type() Selector

The :nth-of-type() in css Selector is used to style only those elements which are the nth number of child of its parent element. An n may be a number, a keyword, or a formula. 

Syntax:



:nth-of-type(number) {
      // CSS Property;
} 

Where number is the argument that represents the pattern for matching elements. It can be odd, even or in a functional notation.

Example 1: 






<!DOCTYPE html>
<html>
 
<head>
    <style>
        h1 {
            color: green;
            font-size: 35px;
        }
 
        p:nth-of-type(2) {
            background: green;
            color: white;
            font-weight: bold;
            width: 70%;
        }
    </style>
</head>
 
<body>
    <center>
        <h1>GeeksForGeeks</h1>
        <h2>:nth-of-type() Selector </h2>
        <p class="geeksFORGEEKS">geeksforgeeks</p>
        <p class="forgeeks">A computer science portal for geeks.</p>
        <p class="geeks">Sudo placement.</p>
        <p class="SUDO">GFG STANDS FOR GEEKSFORGEEKS.</p>
    </center>
</body>
 
</html>

Output:

 

Supported Browsers:


Article Tags :