Open In App

Difference between the “nth-child()” and “nth-of-type()” selectors in jQuery

Last Updated : 07 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

When styling elements in HTML, you often want to target specific children in a list, table, or group. Two common CSS pseudo-classes to do this are :nth-child() and :nth-of-type(). In this article, we will discuss all the differences between nth-child() and nth-of-type() selectors in jQuery. 

nth-child() Selector: The :nth-child() selector helps you target elements based on their position among all their siblings, no matter what type they are. It counts every kind of element in order like paragraphs, headings, and lists

Syntax:

:nth-child(number) {
// CSS Property
}

Example: This example describes the uses of nth-child() Selector.

HTML
<!DOCTYPE html>
<html>
  <head>
    <title>CSS nth-child() Selector</title>

    <style>
      li:nth-child(2) {
        width: 200px;
        background: green;
        color: blue;
      }
    </style>
  </head>

  <body>
    <h1 style="color: green">GeeksforGeeks</h1>

    <h2>CSS nth-child() Selector</h2>

    <p>Web Technologies Subjects</p>

    <ul>
        <li>HTML</li>
        <p>Hi Geeks</p>
        <li>CSS</li>
        <li>JavaScript</li>
        <li>Bootstrap</li>
        <li>Node.js</li>
        <li>React.js</li>
      </p>
    </ul>
  </body>
</html>

Output:


1

No <li> will be styled green because:

  • li:nth-child(2) targets the second child of <ul>, but the second child is a <p>, so no match happens.
  • All <li> elements remain in normal browser style (black text on white background).

nth-of-type() Selector: The :nth-of-type() selector in CSS is used to style elements that are the nth child of their parent among elements of the same type. In other words, it ignores different tags and only counts elements with the same tag name, letting you apply styles to a specific position within that type of element.

Syntax:

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

Example: This example describes the uses of nth-of-type() Selector.

HTML
<!DOCTYPE html>
<html>
  <head>
    <title>CSS nth-of-type() Selector</title>

    <style>
      li:nth-of-type(2) {
        width: 200px;
        background: green;
        color: white;
      }
    </style>
  </head>

  <body>
    <h1 style="color: green">GeeksforGeeks</h1>

    <h2>CSS nth-of-type() Selector</h2>

    <p>Web Technologies Subjects</p>

    <ul>
      <li>HTML</li>
      <p>Hi Geeks</p>
      <li>CSS</li>
      <li>JavaScript</li>
      <li>Bootstrap</li>
      <li>React.js</li>
    </ul>
  </body>
</html>

Output:

2

One <li> (“CSS”) will be styled green because:

  • li:nth-of-type(2) targets the second <li> element inside the <ul>, ignoring other tags like the <p>.
  • So even though the paragraph is in between, “CSS” is still counted as the second <li>.
  • That’s why “CSS” gets the green background and white text, while all other <li> elements keep their normal style (black text on white background).

Difference between the nth-child() and nth-of-type() selectors:

These two CSS selectors help you target elements based on their position, but they work differently when counting element types.

nth-child() Selector

nth-of-type() Selector

This selector is used to style only those elements that are the nth number of child of its parent element. This selector is used to style only those elements that are the nth number of child of its parent element. 
It is used to selects all elements that are the nth childIt is used to select all elements that are the nth child

Its syntax is  -:

:nth-child(n|even|odd|formula)

Its syntax is -:

:nth-of-type(n|even|odd|formula)

It takes 4 Parameters -:
1.  index of each child

2. even child element

3. odd child element

4. formula -: (an + b)

It takes 4 Parameters -:

1.  index of each child

2. even child element

3. odd child element

4. formula -: (an + b)

It does not consider type of the parent while selecting the elements.It only consider a particular type of the parent.

Similar Reads