1
Look at that..
div .p:nth-of-type(2) {
font-weight: bold;
}
DIV 1
<div div="div1">
<p class="p">p1</p>
<p>p2</p>
<p>p3</p>
<p class="p">p4</p>
</div>
DIV 2
<div id="div2">
<p class="p">p1</p>
<p>p2</p>
<p>p3</p>
<p class="p">p4</p>
</div>
In the above example, I want to select the second element of the class P(.p) daughter of a parent(div), and it is not what results..
if I make use of nth-of-type(1
) it works, which to me translated would be, select the first element of the paragraphe type son of a father(div). But why nth-of-type(2)
does not go there for the fourth paragraph of both Divs, giving the bold effect..
If I write it,
.p:nth-of-type(2)
I am wanting to access the element whose class is . p, whose daughter is a father( that in the case is a div ), wouldn’t that be the interpretation? That is, selecting the second element of the class(.p), and as I indicated that the father is the div, so in my view, both Divs would have to have the 4° bold paragraph.– Ale
@Alexandrec.Caus is pseudo-class does not work the way you are seeking, take a look at this one documentation, what you seem to be looking for is That: Nth-Child
– Fernando Leal
To work with :Nth-of-type you would have to move the <p class="p">P4</p> up the ps, but in your case it would be better to use the same :Nth-Child
– haykou
A question, is it worth just using this Nth-of-type, for me to select 9 paragraphs, within a div with 15 paragraphs, or would it be better if I identify by IDS and directly apply in the ID the effect? I say better, in the term of speed, because it is simpler to apply an ID and put a style in it, I’m thinking that delays more using pseudo-selector, browser has to think more.. someone has some comment on this?
– Ale