Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4172712
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T00:35:46+00:00 2026-05-21T00:35:46+00:00

Is it possible to get the nth-child pseudo-selector to work with a specific class?

  • 0

Is it possible to get the nth-child pseudo-selector to work with a specific class?

See this example:
http://jsfiddle.net/fZGvH/

I want to have the second DIV.red turn red, but it doesn’t apply the color as expected.

Not only that, but when you specify this, it changes the 5th DIV to red:

div.red:nth-child(6)

When you specify this, it changes the 8th DIV to red:

div.red:nth-child(9)

It seems to be one DIV behind. There are only 8 DIV tags in the example so I don’t know why nth-child(9) even works. Testing using Firefox 3.6, but in my actual production code the same problem occurs in Chrome. I’m not understanding something about how this is supposed to work, would appreciate clarification.

Also, this will change the 6th DIV to red, but what I actually want is for it to change the second DIV.red to red:

div.red:nth-of-type(6)

And I don’t understand why nth-child() and nth-of-type() respond differently, since there are only eight tags of the same type in the document.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T00:35:46+00:00Added an answer on May 21, 2026 at 12:35 am

    There’s no way to filter by class in CSS because there’s no :nth-of-class() selector. One way around this is to put your two different kinds of divs into their own groups, then select based on those groups. For example:

    <div class="orange">
      <div></div>
      <div></div>
      <div></div>
      <div></div>
    </div>
    
    <div class="red">
      <div></div>
      <div></div>
      <div></div>
      <div></div>
    </div>
    

    With this selector:

    div.red div:nth-child(2) {
        background: red;
    }
    

    Regarding the last bit of your question:

    And I don’t understand why nth-child() and nth-of-type() respond differently, since there are only eight tags of the same type in the document.

    For the code you give there shouldn’t be any difference. I tested it, and the two pseudo-classes work as expected. But, in general:

    :nth-child() operates on an entire level of siblings without regard for any other simple selectors. Then if the nth child is not among what’s matched by the simple selectors, nothing is matched.

    :nth-of-type() operates on a level of siblings of the given type without regard for other simple selectors. Then if the nth element occurring of that type is not among what’s matched by the simple selectors, nothing is matched.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to get nth jQuery element I have this simple code: <ul>
Is it possible to get jQuery to do the same job as nth:child(odd)? So
Is it possible to get the 'nth' return value from a function without having
Is it possible to use nth-child() to select consecutive elements? I have an ordered
Possible Spoiler Warning I've spent ages trying to improve this algorithm and work out
Is it possible get the last visited pages by user using C# / ASP.NET
Possible Duplicate: How do I get the nth element from a Dictionary? If there's
It's possible get each row as an object like in ezSQL? For example, ezSQL
Possible Duplicate: Get first day of week in PHP? i have this script that
I am trying to work out if it is possible get JPA to persist

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.