The nth-of-type pseudo selector is certainly working here as expected.
From MDN:
The :nth-of-type(an+b) CSS pseudo-class matches an element that has
an+b-1 siblings with the same element name before it in the document
tree
So
.test3:nth-of-type(3) {height: 100px;background: black;}
'works' because the div with class test3 also happens to be the third div within the container (div with class test)
however
.test3:nth-of-type(1) {height: 100px;background: black;}
doesn't select the test3 div because it isn't the first div in the container
Is nth-of-type
work same as nth-child
in this case?
In this case they are the same because all of the children in the container are divs.
In general, it could help to think about pseudo classes as a filter which matches only a subset of the matched selector (ie the part of the selector before the colon) when it is in the state that the pseudo class describes.
So for example .test:hover
means: select all elements with class test - but only the ones which are being hovered.
Similarly here with nth-of-type: .test3:nth-of-type(3)
means:
Select all elements with class test3, but only if it's the 3rd of that kind of element within it's container.
So take the following demo:
.test:nth-of-type(3) {
color:red;
}
<div>
<ul>
<li class="test">li 1</li>
<li class="test">li 2</li>
<li class="test">li 3</li>
<li class="test">li 4</li>
<li class="test">li 5</li>
</ul>
<span class="test">span 1</span>
<span class="test">span 2</span>
<span class="test">span 3</span>
<span class="test">span 4</span>
<span class="test">span 5</span>
</div>
Notice that both the third list item and the third span are styled - that's because both have class test and are the third of that kind of element within their container
NB:
When using an nth-of-type
pseudo class without limiting it with a type selector - all types will match - as if the universal selector was used.
So for example:
.test:nth-of-type(3)
is equivalent to *.test:nth-of-type(3)
Demo:
.test:nth-of-type(3) {
color:red;
}
<div>
<fred class="test">fred 1</fred>
<div class="test">div 1</div>
<span class="test">span 1</span>
<p class="test">p 1</p>
<p class="test">p 2</p>
<p class="test">p 3</p>
<p class="test">p 4</p>
<span class="test">span 2</span>
<fred class="test">fred 2</fred>
<fred class="test">fred 3</fred>
<span class="test">span 3</span>
</div>