Star Rating
A five-star input with hover preview, click to set, and native keyboard support.
AI prompt
Create a star rating with five 32px star icons inside a fieldset with a legend "Rate this pattern" (visually hidden), implemented as five radio inputs named rating with visually hidden inputs and star labels. Hovering a star previews the rating by filling it and all stars before it in a warm gold (#f5b301) and leaving the others as outlines; leaving the group restores the chosen value. Clicking sets the rating; the clicked star pops with a scale 1 to 1.2 to 1 over 220ms. Show text next to the stars: "Not rated" or "4 of 5". Arrow keys change the value natively; focus-visible draws a 2px ring around the focused star. Add a small Clear link that resets the rating. Disable hover preview under hover:none. Under prefers-reduced-motion remove the pop animation and transitions.About this pattern
Definition
Five stars that act as a single choice. Moving over the stars previews a rating, clicking sets it, and the chosen value stays filled.
Purpose & UX principles
- A quick, familiar way to collect a score.
- Built on radio inputs, so arrow keys, form submission and screen reader announcements ("3 of 5") work natively.
- The text value is displayed next to the stars, so the rating is never conveyed by fill alone.
Interaction states
- Empty, preview (hover), set, focus-visible (ring on the star), cleared with the Clear button.
Motion specification
- Fill colour: 120ms ease. A set star pops with a scale 1 to 1.2 to 1 over 220ms.
- Reduced motion: no pop, instant fill.
Responsive behaviour
- Stars are 32px with a 44px tall hit row on touch.
- Hover preview is skipped on touch devices.
Code
<fieldset class="rt" id="rt"><legend class="sr">Rate this pattern</legend>
<div class="stars" id="stars"></div><span class="val" id="val" aria-live="polite">Not rated</span>
<button class="clr" id="clr" type="button" hidden>Clear</button></fieldset>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
.rt{border:0;display:flex;align-items:center;gap:12px}
.sr{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0)}
.stars{display:flex}
.stars label{display:grid;place-items:center;width:34px;height:44px;cursor:pointer;color:#b8b8b3;transition:color .12s}
.stars input{position:absolute;opacity:0;pointer-events:none}
.stars svg{width:30px;height:30px;fill:none;stroke:currentColor;stroke-width:1.6;stroke-linejoin:round;transition:fill .12s}
.stars label.on{color:#f5b301}.stars label.on svg{fill:currentColor}
.stars label.pop svg{animation:pop .22s ease-out}
.stars input:focus-visible+svg{outline:2px solid var(--accent);outline-offset:2px;border-radius:6px}
@keyframes pop{50%{transform:scale(1.2)}}
.val{font-size:13px;color:var(--muted);min-width:64px}
.clr{background:none;border:0;font:inherit;font-size:12px;color:var(--muted);text-decoration:underline;cursor:pointer}
@media (prefers-reduced-motion:reduce){.stars label.pop svg{animation:none}.stars label,.stars svg{transition:none}}const stars=document.getElementById('stars'),val=document.getElementById('val'),clr=document.getElementById('clr');let cur=0;
const path='<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M12 3.500l2.700 5.600 6.100.8-4.500 4.200 1.100 6.100L12 17.300 6.600 20.200l1.100-6.100L3.200 9.900l6.100-.8z"/></svg>';
for(let i=1;i<=5;i++){const l=document.createElement('label');l.innerHTML='<input type="radio" name="rating" value="'+i+'" aria-label="'+i+' of 5">'+path;stars.append(l);}
const ls=[...stars.children];
const paint=n=>ls.forEach((l,i)=>l.classList.toggle('on',i<n));
const set=n=>{cur=n;paint(n);val.textContent=n?n+' of 5':'Not rated';clr.hidden=!n;};
ls.forEach((l,i)=>{const inp=l.querySelector('input');
inp.addEventListener('change',()=>{set(i+1);l.classList.remove('pop');void l.offsetWidth;l.classList.add('pop');});
if(matchMedia('(hover:hover)').matches)l.addEventListener('pointerenter',()=>paint(i+1));});
stars.addEventListener('pointerleave',()=>paint(cur));
clr.addEventListener('click',()=>{ls.forEach(l=>l.querySelector('input').checked=false);set(0);});One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Star Rating — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
.rt{border:0;display:flex;align-items:center;gap:12px}
.sr{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0)}
.stars{display:flex}
.stars label{display:grid;place-items:center;width:34px;height:44px;cursor:pointer;color:#b8b8b3;transition:color .12s}
.stars input{position:absolute;opacity:0;pointer-events:none}
.stars svg{width:30px;height:30px;fill:none;stroke:currentColor;stroke-width:1.6;stroke-linejoin:round;transition:fill .12s}
.stars label.on{color:#f5b301}.stars label.on svg{fill:currentColor}
.stars label.pop svg{animation:pop .22s ease-out}
.stars input:focus-visible+svg{outline:2px solid var(--accent);outline-offset:2px;border-radius:6px}
@keyframes pop{50%{transform:scale(1.2)}}
.val{font-size:13px;color:var(--muted);min-width:64px}
.clr{background:none;border:0;font:inherit;font-size:12px;color:var(--muted);text-decoration:underline;cursor:pointer}
@media (prefers-reduced-motion:reduce){.stars label.pop svg{animation:none}.stars label,.stars svg{transition:none}}
</style></head>
<body><div class="stage">
<fieldset class="rt" id="rt"><legend class="sr">Rate this pattern</legend>
<div class="stars" id="stars"></div><span class="val" id="val" aria-live="polite">Not rated</span>
<button class="clr" id="clr" type="button" hidden>Clear</button></fieldset>
</div>
<script>
const stars=document.getElementById('stars'),val=document.getElementById('val'),clr=document.getElementById('clr');let cur=0;
const path='<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M12 3.500l2.700 5.600 6.100.8-4.500 4.200 1.100 6.100L12 17.300 6.600 20.200l1.100-6.100L3.200 9.900l6.100-.8z"/></svg>';
for(let i=1;i<=5;i++){const l=document.createElement('label');l.innerHTML='<input type="radio" name="rating" value="'+i+'" aria-label="'+i+' of 5">'+path;stars.append(l);}
const ls=[...stars.children];
const paint=n=>ls.forEach((l,i)=>l.classList.toggle('on',i<n));
const set=n=>{cur=n;paint(n);val.textContent=n?n+' of 5':'Not rated';clr.hidden=!n;};
ls.forEach((l,i)=>{const inp=l.querySelector('input');
inp.addEventListener('change',()=>{set(i+1);l.classList.remove('pop');void l.offsetWidth;l.classList.add('pop');});
if(matchMedia('(hover:hover)').matches)l.addEventListener('pointerenter',()=>paint(i+1));});
stars.addEventListener('pointerleave',()=>paint(cur));
clr.addEventListener('click',()=>{ls.forEach(l=>l.querySelector('input').checked=false);set(0);});
</script>
</body></html>