I need to add styling between all the components that are between two selectors.
The first selector has an activity class, and the second hover has a state.
When you hover over the sixth element, all elements between the first and sixth should turn gray, as in the screenshot
.day {
border: 1px solid;
}
.day:hover {
background: #CCC;
}
.active {
background: blue;
}
<div class="day">-1</div>
<div class="day">0</div>
<div class='day active'>1</div>
<div class="day">2</div>
<div class="day">3</div>
<div class="day">4</div>
<div class="day">5</div>
<div class="day">6</div>
<div class="day">7</div>
2
Answers
An easy way is to catch hover on the parent, give the background to all chidren, then exclude every one behind the one hovered.
selectors that you can use for this is :
~
andnot()
the idea is to style anything in between
.active
and the one hovered.here is an example:
selector explanation :
ressources :
https://developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/Selectors/Combinators#subsequent-sibling_combinator
https://developer.mozilla.org/en-US/docs/Web/CSS/:not
You can use Javascript for this purpose. You can get the days elements and add
mouseenter
andmouseleave
events for them to set all lines after the active one until the current one, including the current one to gray.