5
Unfortunately the question marked as "possible duplicate" (and its responses) address something else, specifically the functioning of variable
n
in formulas passed in the:nth-child()
, which neither contemplates nor answers to the doubts set out in this question.
I did some research and saw that the pseudo-class :nth-child()
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 class .classe
does not turn red, as expected. However, if I change the value of .nth-child
for (2)
, will apply the red color to the first element of the class (not to 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>
Well, if the .nth-child()
does not work with classes, because when I change the value to (2)
the property is applied to the wrong element, and with the value (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 of (1)
for (3)
, will change the color of the last div
, the one with the class .classe
. But if I remove the class from the div
, nothing will happen.
I mean, the class is making some influence, I just couldn’t understand what that influence would be.
The question is: the style .classe:nth-child()
should not be ignored by the CSS? What is the criterion that the CSS uses in these cases, since in the second example the color was changed (even in the unwanted element)?
Possible duplicate of What do "n" means, numbers and signals on "Nth-Child" or "Nth-last-Child" selectors"?
– Guilherme Nascimento
the N specifically explains why it does not work based on classes, will understand the basis of the child/sibling elements and that the selector’s behavior is based on this, the other question that complements the explanation is probably this https://answall.com/q/290866/3635, where I explain in the answer clearly, direct sibling elements, or is independent of any other characteristic, the consideration is for the direct, then even with the class selector, the Nth-CHILD will evaluate all children.
– Guilherme Nascimento
It is basically what the Ugo answered a group of sibling elements, ie will evaluate to all siblings in the N-enisimo
– Guilherme Nascimento
@Before formulating the question I read all these topics, but for me they have not yet been able to clarify the doubts I had. Hugocss' response was very specific and clear to me. I believe it can help someone as layman as I am in the future. Thanks!
– Sam
I do not see so extra complement in the answer, both links that I mentioned completely address, here the error falls more to typo/usage error, sorry I’m just being honest about the technical approach, if you understand the technical functioning understands the problem, regardless of having examples or not, the idea is to understand the word CHILD, of course CSS is confusing at first glance, but if the technical understanding is explored there your perception changes from angle which will make better understand the behavior of DOM, box-model and other selectors in general ;)
– Guilherme Nascimento
@Guilhermenascimento It’s easier for you to understand. I’m still learning some things. But I appreciate the explanations.
– Sam
But that’s what I’m explaining, if you try to understand technical behavior you’ll understand why something fails, of course there are situations called "computing" that even having technical knowledge still sometimes we do not understand why something stops working or back to work alone (something common to anyone who works in the area of IT), but still you should not abandon or run away from technical knowledge, understanding this is the key to mastering things, unlike this is like wanting to drive a car even before learning to ride ;)
– Guilherme Nascimento
I hope you understand all the technical explanation and what I am trying to give you so that you avoid the path of stones in the future, after all for most of the things there are reasons (technical reasons) to behave in certain ways, happy new year!
– Guilherme Nascimento
@Guilhermenascimento Thanks! to you too!
– Sam