I am confused about how the selector nth-of-type
works in conjunction with the selector. In the example below, I would expect squares 3, 4, and 5 to be red, but that is not the case. How to select a selector .bar ~ div:nth-of-type(-n+3)
to select items?
If I wanted to get the first three elements after .bar
, what would be the right selector for this?
.foo {
background-color: blue;
color: white;
width: 50px;
height: 50px;
display: inline-block;
border: solid 1px black;
}
.bar {
background: green;
}
.bar ~ div {
background-color: orange;
}
.bar ~ div:nth-of-type(-n+3) {
background-color: red;
}
<div>
<div class="foo">0</div>
<div class="foo">1</div>
<div class="foo bar">2</div>
<div class="foo">3</div>
<div class="foo">4</div>
<div class="foo">5</div>
<div class="foo">6</div>
<div class="foo">7</div>
</div>
Run codeHide result
source
share