Skip to content
IndexRequest a UIAboutContact

Star Rating

A five-star input with hover preview, click to set, and native keyboard support.

Open alone ↗

AI prompt

PROMPT: PASTE INTO YOUR AI CODING TOOL
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

HTML
<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>

Related patterns

Radio Button

↗
Radio ButtonInputs & Forms

Icon Button

↗
Icon ButtonButtons & Actions

Range Slider

↗
Range SliderInputs & Forms