8

.test:nth-child(1),
.test:nth-child(2),
.test:nth-child(3) {
  color: #0F0
}
<div class="test">
  <p>Test</p>
</div>
<div class="reuinIt">
  <p>Test</p>
</div>
<div class="test">
  <p>Test</p>
</div>
<div class="test">
  <p>Test</p>
</div>

https://jsfiddle.net/dkfj2xzj/13/ <- UPDATED with my jQuery code if it helps

Why is it not skipping the .ruinIt class and not targeting the third .test? I'm adding <div>s dynamically and when a <div> without the .checkDrop class is added I need to skip it.

Thanks

8
  • What are you trying to do?
    – thepio
    Commented Sep 15, 2016 at 7:18
  • All you need is .test > p {color: #0F0} or .test > * {color: #0F0}. No need for nth-child selector. At least in the current state of the question.
    – thepio
    Commented Sep 15, 2016 at 7:27
  • I'll show you the Jquery code I'm using: jsfiddle.net/dkfj2xzj/13 So I need it to skip every element without the .checkDrop class. Is it possible?
    – TellJohn
    Commented Sep 15, 2016 at 7:28
  • The Divs are added dynamically
    – TellJohn
    Commented Sep 15, 2016 at 7:29
  • 1
    You want to do this with CSS or the jQuery? Here's an example where I apply a green border to every input without the checkDrop class: jsfiddle.net/thepio/eyp4duj9/2
    – thepio
    Commented Sep 15, 2016 at 7:34

7 Answers 7

10

It's because the nth-child selector does not mean it's the nth of that specific class. It means that it's the nth sibling overall.

So the nth-child(2) refers to your .reuinIt class, however, it does not also have the .test class and therefore it does not receive any styling.

Your last .test class is the nth-child(4) however that has no styling rules applied.

If you'd like to see a working example, I've updated your fiddle here.


EXAMPLES

The :nth-child

The important thing to remember here is that the :nth-child selector specifically targets HTML elements based on their index/position inside their containers/parent elements.

Have a look at the example below and take note of how the corresponding commented :nth-child selector's index continues to increment regardless of the type of element it's targeting.

<div id="container">
    <h1>Heading 1</h1>   <!-- h1:nth-child(1) -->
    <p>Paragraph 1</p>   <!-- p:nth-child(2)  -->
    <p>Paragraph 2</p>   <!-- p:nth-child(3)  -->
    <h2>Heading 2</h2>   <!-- h2:nth-child(4) -->
    <p>Paragraph 3</p>   <!-- p:nth-child(5)  -->
</div>

The :nth-of-type

The cool thing about :nth-of-type is that it ignores all of the other elements that are not of the same type, i.e. if the element you are targeting is a <p>, it will ignore all of the surrounding "non-<p>" elements when calculating its index.

The below example will provide you with a basic understanding of the indexing rules that :nth-of-type follows:

<div id="container">
    <h1>Heading 1</h1>   <!-- h1:nth-of-type(1) -->
    <p>Paragraph 1</p>   <!-- p:nth-of-type(1)  -->
    <p>Paragraph 2</p>   <!-- p:nth-of-type(2)  -->
    <h2>Heading 2</h2>   <!-- h2:nth-of-type(1) -->
    <p>Paragraph 3</p>   <!-- p:nth-of-type(3)  -->
</div>

A little more complexity with :nth-of-type

It is however very important to remember that :nth-of-type bases it's indexing values on the HTML Element Type regardless of the CSS Class you are using to call the property.

Have a look at the below example:

<div id="container">
    <h1>Heading 1</h1>                    <!--        h1:nth-of-type(1) -->
    <p class="my-class">Paragraph 1</p>   <!-- .my-class:nth-of-type(1) -->
    <p>Paragraph 2</p>                    <!--         p:nth-of-type(2) -->
    <h2 class="my-class">Heading 2</h2>   <!-- .my-class:nth-of-type(1) -->
    <p class="my-class">Paragraph 3</p>   <!-- .my-class:nth-of-type(3) -->
    <h1 class="my-class">Heading 3</h1>   <!-- .my-class:nth-of-type(2) -->
</div>

This example is a little more complex, but it helps if you see CSS Declarations as a sort of filtering rule. For example, if create a CSS declaration by typing:

p:nth-of-type(2) {
  background-color: red;
}

I am essentially telling the browser 2 things:

  1. Only <p> tags should be affected and,
  2. Only if they are the second <p> tags amidst their siblings

The difficulty comes in when I write CSS that looks like this:

.my-class:nth-of-type(1) {
  background-color: red;
}

By not specifying an element type, my rule essentially reads with the following filter:

  1. Only elements with the class my-class should be affected and,
  2. Only if those elements are the first sibling of their type of elements.

If were to apply the above CSS to the HTML in the example (see fiddle for working example), we would get an output that looks like this:

enter image description here

In the output above, you'll see that both the first <h2> and the first <p> elements were affected regardless of whether or not their siblings had the my-class class name applied.

2
  • I'm sure it's not needed, but if you'd like to see what I mean, here's your fiddle that I've corrected: jsfiddle.net/dkfj2xzj/10
    – Frits
    Commented Sep 15, 2016 at 7:22
  • Shouldn't the dot be removed in your example; .p:nth-of-type(2) { background-color: red; }?
    – feskr
    Commented Oct 22, 2019 at 10:22
2

The code .test:nth-child(2) doesn't mean "the second element of the class test in its container". It means just "element that has a test class and is the second child of its container".

The behavior you expected can be expressed with CSS Selectors 4 as :nth-child(2 of .test). Unfortunately, this syntax is currently supported only in Safari.

2

Try below code for targeting nth-child:

You can first parent div for all child div.

.parent_div .test:nth-child(1) {
  color: red;
}
.parent_div .test:nth-child(3) {
  color: red;
}
.parent_div .test:nth-child(4) {
  color: red;
}
<div class="parent_div">
<div class="test">
   <p>Test</p>
</div>
<div class="reuinIt">
   <p>Test</p>
</div>
<div class="test">
   <p>Test</p>
</div>
<div class="test">
   <p>Test</p>
</div>
</div>

1
  • Hey nice , our thought are same
    – Gowtham
    Commented Sep 15, 2016 at 7:23
1

.test:nth-child(2) selector means select an element having class .test when it is 2nd child of its parent. Similarly .test:nth-child(3) will select 3rd element of a parent if it will have .test class.

In your case 2nd element doesn't have .test class so it is not selecting it. If you wants to target them 3rd div element you need to use .test:nth-child(3)(As it is 3rd element of its parent, not 2nd).

Correct selector will be:

.test:nth-child(1), .test:nth-child(3), .test:nth-child(4) {color: #0F0;}
1

.test:nth-child(1), .test:nth-child(3), .test:nth-child(4)
{color: #0F0}
<div class="test">
  <p>Test0</p>
</div>
<div class="reuinIt">
  <p>Test1</p>
</div>
<div class="test">
  <p>Test2</p>
</div>
<div class="test">
  <p>Test3</p>
</div>

1

You can try this:

.test:nth-child(1),
.test:nth-child(2),
.test:nth-child(3) {
  color: #0F0
}
<div class="test">

  <p>Test</p>
  <p>Test</p>
  <p>Test</p>

</div>

<div class="reuinIt">
  <p>Test</p>
</div>

1

You are confused between the usage of nth-child & nth-of-type. nth-child will consider all the tags and all the elements. If you want to specifically count a certain type of element or class, don't make use of nth-child. Go for nth-of-type. But, moreover nth-of-type is not supported currently for the classes though it works fine for html tags (may be in future it will get supported). All you need to do is change the nth-child numbers to 1, 3, 4. That's the only way you can get your results.

Fiddle: https://jsfiddle.net/jw66uj1p/

.test:nth-of-type(1),
.test:nth-of-type(3),
.test:nth-of-type(4) {
  color: #0F0
}
1
  • 1
    In terms of CSS selectors, 'type' is almost a synonym for 'tag name', selectors like div are called 'type selectors'. So :nth-of-type references only elements of the same tag name by definition, it doesn't have anything to do with classes. There is a form of :nth-child in CSS Selectors 4 that takes classes/IDs/attributes into account (:nth-child(An + B of .something)), but it works only in Safari. Commented Sep 15, 2016 at 8:48

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.