Five stars without reversing the keyboard
CaveatKeep stars in DOM order from one to five. The old row-reverse trick makes the sibling selector convenient but puts visual order and arrow-key order at odds. Here label:has(~ label :checked) finds every label before the selected one, so three checked means three gold stars without rearranging anything. Each radio has a spoken score; the star glyph is decorative. Try the arrow keys and watch the filled run follow the checked input.
FallbackWithout :has(), the stars remain grey and the visible radio still marks the selected score. Leave those inputs available rather than hiding them with display: none. This is whole-star input, not a display of fractional review averages, and nothing is saved until your application submits the selected value. If you need a separate keyboard outline, see focus-visible. Reference: MDN documentation.
<fieldset class="d-rating">
<legend>How useful was this example?</legend>
<div>
<label><input type="radio" name="wc-rating"
value="1" aria-label="1 out of 5 stars">
<span aria-hidden="true">★</span></label>
<label><input type="radio" name="wc-rating"
value="2" aria-label="2 out of 5 stars">
<span aria-hidden="true">★</span></label>
<label><input type="radio" name="wc-rating"
value="3" aria-label="3 out of 5 stars"
checked>
<span aria-hidden="true">★</span></label>
<label><input type="radio" name="wc-rating"
value="4" aria-label="4 out of 5 stars">
<span aria-hidden="true">★</span></label>
<label><input type="radio" name="wc-rating"
value="5" aria-label="5 out of 5 stars">
<span aria-hidden="true">★</span></label>
</div>
</fieldset>
.d-rating {
border: 0; padding: 0; margin: 0;
max-width: 100%;
color: var(--paper, #e6eaf1);
}
.d-rating legend { margin-bottom: 1rem; }
.d-rating > div { display: flex; gap: .35rem; }
.d-rating label {
display: grid; justify-items: center;
cursor: pointer; min-width: 2rem;
}
.d-rating input { accent-color: #d99b28; }
.d-rating span {
font-size: 2rem; line-height: 1.3;
color: #64748b;
}
.d-rating label:has(:checked) span,
.d-rating label:has(~ label :checked) span {
color: #d99b28;
}
.d-rating label:has(:focus-visible) {
outline: 2px solid currentColor;
outline-offset: 2px; border-radius: .2rem;
}