The javascript above seems nicer but if you're looking for a pure CSS solution,
here's a weird trick.
First you need to use flexbox to reverse the order of elements. Then you can use neighbor selectors +
in css to color all the "previous" elements (which are really the next elements).
I realize it's a little silly, maybe but it is a CSS only solution and was kind of fun to try.
And if you need it to be 6 stars or 10, it's not easily extended because you need to paste in a bunch of extra CSS rules. If you are using Sass, you could probably build a function that would generate those rules for you.
UPDATE: I saw @David's post and it's much better - using the ~
selector.
.parent {
display: flex;
flex-direction: row-reverse;
justify-content: flex-end;
}
.parent span {
display: block;
border: 1px solid black;
width: 32px;
height: 32px;
background-color: #eee;
}
span + span {
margin-right: 10px;
}
span:hover {
background-color: red;
}
span:hover + span {
background-color: red;
}
span:hover + span + span {
background-color: red;
}
span:hover + span + span + span {
background-color: red;
}
span:hover + span + span + span + span{
background-color: red;
}
<div class="parent">
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
</div>