:has(~ :checked)
CSS star rating with accessible radio order
Five stars without reversing the keyboard
Live demo
Caveat and fallback
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.
Source code
<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;
}
Published
Questions
How do I fill the stars before the selected radio with CSS?
Put the labels in ascending score order and use label:has(~ label :checked) to match labels before the selected input. Add label:has(:checked) for the selected star itself. The radios keep their normal DOM and keyboard order.
Should a CSS star rating use row-reverse?
Reversing the visual row can separate the visible order from the order of the inputs. Keeping scores one through five in DOM order avoids that mismatch. A parent selector can fill earlier stars without moving any controls.
Can I hide the radio inputs in a star rating?
Do not use display: none on an input that people need to operate. This example keeps the radios visible so selection and keyboard focus still work when the star styling is unavailable. Each input also has a spoken score.