Unfortunately the question marked as "possible duplicate" (and your answers) address something else, specifically the variable
n
in formulas passed in:nth-child()
, which does not contemplate and do not answer the doubts exposed in this question.
I did some research and saw that the :nth-child()
pseudo-class does not work when used with classes. For example:
.classe:nth-child(1){
color: red;
}
<div>texto preto</div>
<div class="classe">este texto era pra ser vermelho :(</div>
<div class="classe">texto preto</div>
The text of the first element of the .classe
class does not turn red, as expected. However, if I change the value of .nth-child
to (2)
, it will apply the red color to the first element of the class (not the second, as expected):
.classe:nth-child(2){
color: red;
}
<div>texto preto</div>
<div class="classe">este texto é vermelho, mas não deveria :/</div>
<div class="classe">este texto era pra ser vermelho :(</div>
Now, if .nth-child()
does not work with classes, why when I change the value to (2)
is the property applied to the wrong element, and with (1)
nothing happens?
And when I remove the class from the second div
, unlike the previous example, nothing happens:
.classe:nth-child(2){
color: red;
}
<div>texto preto</div>
<div>texto preto</div>
<div class="classe">texto preto</div>
In the example above, if I change the value from (1)
to (3)
, it will change the color of the last div
, which has the .classe
class. But if I remove the class from div
, nothing will happen.
That is, the class is making some influence, I just could not understand what influence that would be.
The question is: should not the .classe:nth-child()
style be ignored by CSS? What is the CSS criterion used in these cases, since in the second example the color has been changed (even if it is in the unwanted element)?