In a recent project, we needed to implement a rating star component.
Our requirements were:
- Fully accessible via radio buttons, because we care about accessibility
- No JS, because CSS is powerful
- Minimum HTML and CSS while keeping it flexible and readable
- Maximum layout flexibility without "quirks"
- Should work with any SVG for the icon
We researched some approaches (see below). But none fit our needs, so I created one.
The main challenge was to achieve the hover behaviour with a mouse. There’s no "all previous siblings" selector in CSS. But you can use the :has and :is selectors to filter from the parent. They will get you the previous siblings from the hovered element.
With a keyboard, the radio buttons work as expected: The arrow keys indicate the current selection via the focus-visible pseudo selector.
Let’s break it down.
The <label> with its <svg> child is the centerpiece here.
This is the default state of the SVG:
.rating-options label svg {
fill: light-dark(var(--color-graydark), var(--color-graylight));
}For a checked radio button, the <label> gets an outline:
.rating-options [type="radio"]:checked:is(:focus-visible) + label {
outline: 1px dotted light-dark(var(--color-graydark), var(--color-graylight));
}During hover, the SVG gets a scale transform and a pointer cursor:
.rating-options label:hover svg {
transform: scale(1.4);
cursor: pointer;
}When does the fill attribute need to change?
- For the
<label>which is being hovered on:
.rating-options label:hover svg { ... }- For all
<label>which are previous siblings of the hovered label:
.rating-options label:has(~ label:hover) svg { ... }- For a
<label>which is the next sibling of a checked radio
.rating-options [type='radio']:checked + label svg { ... }- For all
<labels>which are previous siblings of a checked radio button:
.rating-options:has([type='radio']:checked) label:has(~ [type='radio']:checked) svg { ... }So, let’s combine the selectors and apply the fill attribute:
.rating-options label:hover svg,
.rating-options label:has(~ label:hover) svg,
.rating-options [type='radio']:checked + label svg,
.rating-options:has([type='radio']:checked) label:has(~ [type='radio']:checked) svg {
fill: var(--color-yellow);
}Here are some other approaches we found, but didn’t fit our needs:
-
A single-element star rating component (without JS)
- Doesn’t fit to our needs to switch to another icon easily as it requires the icon to be “drawn” in CSS
- The
maskattribute "kills" any outline or border as focus indicator - It doesn’t show the hover for the elements preceding the one being hovered
-
- The gap between the stars requires to be defined as pixels and in the icon sprite (used repeatedly to achieve the hover display), so any spacing defined in CSS doesn’t work
- The reversed order of the radio options in the markup and the
flex-direction: row-reverseto make use of the next-sibling selector in CSS is a bit confusing
-
Rating component of Shoelace lib
- Although I like this framework-agnostic UI web component library a lot, it’s out of scope because it’s JS-based (to register the component as a custom element)
The one solution who is closest to our needs and embraces the :has selector, too, is Styling a meter element with CSS and SVG (seems January was a popular month for rating stars ;)). However, it has hover unfortunately not built in (might be easy to add, though). So, at least you have a choice now :-)
I’m happy for any feedback or suggestions for further improvements. I’d wonder if I did not miss anything ;)
Can I :has some resources? Sure!
- CSS :has() selector ishadeed.com
- The Undeniable Utility Of CSS :has joshwcomeau.com
- Level Up Your CSS Skills With The :has() Selector smashingmagazine.com