I have the following HTML code. Suppose I can add the selected-day
class by clicking on each span. My goal is to select the next 6 siblings, starting from the element with the selected-day
class.
Here is what I have so far:
#calendar span.day.selected-day {
background-color: red;
~ span:nth-child(n + 6):nth-child(-n + 15) {
background-color: red;
}
}
<div id="calendar">
<span class="day">1</span>
<span class="day">2</span>
...
<span class="day selected-day">6</span>
<span class="day">7</span>
<span class="day">8</span>
...
<span class="day">16</span>
</div>
It highlights the selected days correctly at first like this:
https://i.sstatic.net/6U1ky.png
However, when I change the selection, it doesn't behave as expected:
https://i.sstatic.net/ckQuo.png
Is there a better way to always choose the next 6 siblings relative to the element with the specified class?