If I use nth-of-type
to assign styling to an element, I have to pass n
or a number into the selector: nth-of-type(1)
.
What I'm curious about is: if I wanted to write one single rule that would perform all 3 of the rules below, would it be possible?
I would have assumed the following would give me the result I needed:
input[type=radio]:nth-of-type(n):checked ~ .item-list div:nth-of-type(n)
My expectation would be that n would have to be equal on both sides of the rule. i.e. when checkbox 1 is checked, div 1 in the list would be pink. This doesn't seem to be the case though, and the only way I have found that makes this work is hardcoding the number.
input[type=radio]:nth-of-type(1):checked ~ .item-list div:nth-of-type(1),
input[type=radio]:nth-of-type(2):checked ~ .item-list div:nth-of-type(2),
input[type=radio]:nth-of-type(3):checked ~ .item-list div:nth-of-type(3) {
color: pink;
}
<input type="radio" name="items" checked />
<input type="radio" name="items" />
<input type="radio" name="items" />
<div class="item-list">
<div>item 1</div>
<div>item 2</div>
<div>item 3</div>
</div>
For the curious, I'm attempting this because of a demo I put together using Spectre CSS: https://plnkr.co/edit/XmO4wOWgDLWvoiq0XVzG?p=preview