I have a grid of elements that I want to appear in their unfiltered form when the page loads. When a user mouses over one of them, it should stay highlighted while the other elements fade to grey. I was able to get this working fine for subsequent elements but not the previous. I then came up with the following solution:
.child:hover {
filter: grayscale(0);
/*forces the 'active' element to stay unfiltered. */
}
.parent:hover>div {
filter: grayscale(1);
transition: filter 0.5s;
}
.parent>div {
filter: grayscale(0);
transition: filter 0.5s;
/* so the transition back out is smooth */
}
<div class="parent">
<div class="child">One</div>
<div class="child">of</div>
<div class="child">any</div>
<div class="child">number</div>
<div class="child">of</div>
<div class="child">children</div>
</div>
The problem is that the fadeout occurs when a user places the cursors in the gaps between the elements as well - so if their placement is unlucky it will show everything in grayscale and look off.
I'd like to achieve this without using JavaScript if possible. I know that CSS doesn't have a built-in "previous siblings" selector, and I've tried various combinations of "has" and not gotten anywhere. Is this possible?