Pickle · 2026-11-22
CSS star rating with no JavaScript
A CSS star rating is five radio buttons wearing a costume. Because they're real radio buttons, the keyboard and screen readers work on their own, and the CSS only has to make them look like stars. No script, no library, nothing to install.
We took the stars apart to see how they work. Now you can too.
Try it
Click a star, or tab to the group and press the arrow keys.
Keys that work
- Tab moves into the group, and out of it again.
- → and ↓ add a star. ← and ↑ take one away. The stars are listed 1 to 5 in the HTML, so the keys follow the picture.
- Every star has a real text label, so a screen reader has something to read. We have not tested this with a screen reader, so treat that as likely, not proven.
- No script runs on this page apart from the copy buttons.
How it works
- Real radio buttons. One per star, all with the same name. The browser gives you the arrow keys and the labels for free.
- Stars in order. The HTML lists 1 to 5, left to right, so the browser's arrow keys move the way the picture does. A star is filled when it is checked or when a checked one comes after it, which is what
label:has(~ input:checked)says. - Star shape. A plain square is cut into a star with
clip-path. - Focus ring on the group. The clip also cuts off a normal focus outline, so the ring goes around the whole group with
:focus-within.
Tested in Chromium only. The earlier stars and the readout use :has(), which current browsers support. Where it is missing, the radio buttons still work and the chosen star still lights up, but the stars before it stay grey and the text line is missing. That is why the checked star has a rule of its own.
The HTML, and a readout
<fieldset class="stars">
<legend class="sr">Rate this snack</legend>
<input type="radio" name="rate" id="s1" value="1"><label for="s1"><span class="sr">1 star</span></label>
<input type="radio" name="rate" id="s2" value="2"><label for="s2"><span class="sr">2 stars</span></label>
<input type="radio" name="rate" id="s3" value="3"><label for="s3"><span class="sr">3 stars</span></label>
<input type="radio" name="rate" id="s4" value="4"><label for="s4"><span class="sr">4 stars</span></label>
<input type="radio" name="rate" id="s5" value="5"><label for="s5"><span class="sr">5 stars</span></label>
</fieldset><!-- put this right after the </fieldset> -->
<p class="out" aria-hidden="true">
<span class="r0">Pick a star. Nothing is saved or sent.</span>
<span class="r1">1 of 5. Stale cracker.</span>
<span class="r2">2 of 5. Edible, in a crisis.</span>
<span class="r3">3 of 5. A fine snack.</span>
<span class="r4">4 of 5. Would sneak back for more.</span>
<span class="r5">5 of 5. Top of the stash.</span>
</p>
<style>
/* a readout with :has(), still no JavaScript */
.out span { display: none; }
.stars:not(:has(input:checked)) ~ .out .r0,
.stars:has(#s1:checked) ~ .out .r1,
.stars:has(#s2:checked) ~ .out .r2,
.stars:has(#s3:checked) ~ .out .r3,
.stars:has(#s4:checked) ~ .out .r4,
.stars:has(#s5:checked) ~ .out .r5 { display: inline; }
</style>The CSS
/* hides text from the eye, keeps it for screen readers */
.sr { position: absolute; width: 1px; height: 1px; padding: 0; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
.stars { display: flex; gap: 4px; border: 0; padding: 0; }
.stars:focus-within { outline: 3px solid #ff7b67; outline-offset: 6px; } /* the stars are clipped, so ring the group */
.stars input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.stars label {
width: 44px; height: 44px; cursor: pointer; background: #b9b0c8;
clip-path: polygon(50% 0, 62% 35%, 98% 35%, 69% 57%, 80% 91%, 50% 70%, 20% 91%, 31% 57%, 2% 35%, 38% 35%);
}
/* the checked star */
.stars input:checked + label { background: #ff7b67; }
/* every star before it (a label that has a checked input after it).
Kept as its own rule: a browser without :has() drops only this one. */
.stars label:has(~ input:checked) { background: #ff7b67; }
/* while the pointer is over a star, show that star and the ones before it instead */
.stars:has(label:hover) label { background: #b9b0c8; }
.stars:has(label:hover) label:hover,
.stars:has(label:hover) label:has(~ label:hover) { background: #ff7b67; }The fine print, in plain words
Why this is free
We're raccoons who think simple things shouldn't cost a monthly fee. We take techniques apart, explain them in plain words, and give them away. The code on this page is MIT licensed: copy it, change it, and share it.
This is one of twelve experiments so far. Our first browser tool (Bandit Codes) is live now, and the rest sit as ideas in the stash.
Next snack
CSS resize handle: the property and a custom one
The little corner you drag to make a box bigger. One property, or one you build by hand.