webdevpuneet.com

Rating Stars Input — Free HTML CSS JS Star Rating Picker

Rating Stars Input · Forms · Plain HTML, CSS & JS · Live preview

CategoryForms

Copy & paste

Source Code

<div class="rs-card">
  <p class="rs-q">How was your experience?</p>
  <div class="rs-stars" id="rsStars" role="radiogroup" aria-label="Rating">
    <button type="button" class="rs-star" role="radio" aria-label="1 star" data-v="1" aria-checked="false"><svg viewBox="0 0 24 24"><path d="M12 2l2.9 6.3 6.9.7-5.2 4.6 1.5 6.8L12 17.5 5.9 20.4l1.5-6.8L2.2 9l6.9-.7z"/></svg></button>
    <button type="button" class="rs-star" role="radio" aria-label="2 stars" data-v="2" aria-checked="false"><svg viewBox="0 0 24 24"><path d="M12 2l2.9 6.3 6.9.7-5.2 4.6 1.5 6.8L12 17.5 5.9 20.4l1.5-6.8L2.2 9l6.9-.7z"/></svg></button>
    <button type="button" class="rs-star" role="radio" aria-label="3 stars" data-v="3" aria-checked="false"><svg viewBox="0 0 24 24"><path d="M12 2l2.9 6.3 6.9.7-5.2 4.6 1.5 6.8L12 17.5 5.9 20.4l1.5-6.8L2.2 9l6.9-.7z"/></svg></button>
    <button type="button" class="rs-star" role="radio" aria-label="4 stars" data-v="4" aria-checked="false"><svg viewBox="0 0 24 24"><path d="M12 2l2.9 6.3 6.9.7-5.2 4.6 1.5 6.8L12 17.5 5.9 20.4l1.5-6.8L2.2 9l6.9-.7z"/></svg></button>
    <button type="button" class="rs-star" role="radio" aria-label="5 stars" data-v="5" aria-checked="false"><svg viewBox="0 0 24 24"><path d="M12 2l2.9 6.3 6.9.7-5.2 4.6 1.5 6.8L12 17.5 5.9 20.4l1.5-6.8L2.2 9l6.9-.7z"/></svg></button>
  </div>
  <p class="rs-out" id="rsOut">Tap a star to rate</p>
</div>

Extend with AI

Take This Snippet Further With AI

The AI prompt for this one is for Patreon supporters ($3 a month), along with a site with no ads. Supporters enter the passcode once and it is remembered in this browser.

· Become a supporter

What's included

Features

Hover preview
Lighter fill previews before you commit.
Committed selection
Click locks in a brighter fill.
Fill-up-to logic
One count lights all prior stars.
SVG stars
Crisp, recolorable, scalable shapes.
Radiogroup ARIA
role=radio with aria-checked per star.
Keyboard control
Arrows move, Enter/Space commit.
Focus ring
:focus-visible outline for keyboard users.
Descriptive label
Shows score plus a word like Good.

About this UI Snippet

Rating Stars Input — A Hoverable, Keyboard-Friendly Star Picker

Screenshot of the Rating Stars Input snippet rendered live

The rating stars input is the five-star control people use to score a product, an order, or an experience — stars fill up to the one under your cursor as a preview, and clicking commits the score. This snippet builds an accessible version with SVG stars, CSS, and vanilla JavaScript, including a hover preview, keyboard support, and a live text label.

Two layers: hover preview and committed rating

The control tracks two separate states. Hovering a star paints a lighter "is-hover" fill on it and every star before it, previewing what you'd get if you clicked; clicking commits a brighter "is-on" fill that persists. Keeping preview and selection as different classes means moving the mouse away cleanly restores the committed rating instead of clearing it — the behaviour every star input should have but many get wrong.

Fill-up-to logic

Both states use the same idea: a paint(n, cls) helper toggles a class on stars whose index is less than n, so star three lights stars one through three. There's no per-star bookkeeping — the count drives which stars are lit. The stars themselves are inline SVGs whose fill and stroke transition, so the colour change is smooth and they scale slightly on hover for a tactile feel.

Accessible by design

The group is a role="radiogroup" and each star a role="radio" with an aria-label ("3 stars") and aria-checked that reflects the committed value, so screen readers announce it as a rating control rather than a row of buttons. A :focus-visible outline shows keyboard focus, and the live label text updates so the score is conveyed in plain text too.

Keyboard control

Arrow keys move focus between stars (Right/Up forward, Left/Down back, clamped at the ends) and Enter or Space commits the focused star — the standard radio-group keyboard model. This makes the input fully operable without a mouse, which is essential for a form field.

A descriptive label

Beyond the stars, a line below shows the numeric score and a word — "4/5 · Good" — pulled from a labels array. Pairing a number with a descriptor makes the rating clearer and gives immediate feedback that the click registered.

Customizing it

Change the star count, the colours, or the label words; pre-set a rating by calling setRating on load; or read rating on form submit. Swap the SVG path for hearts or thumbs for a different scale. Pair it with a star rating display, a review form, or a rating breakdown.

Step by step

How to Use

  1. 1
    Paste HTML, CSS, and JSFive empty stars render with a prompt.
  2. 2
    Hover the starsThey fill up to the one under the cursor.
  3. 3
    Click to commitThe score sticks and the label updates.
  4. 4
    Use arrow keysMove focus and press Enter to rate.
  5. 5
    Move the mouse awayThe preview clears, the committed score stays.
  6. 6
    Read the valueGet the rating variable on submit.

Real-world uses

Common Use Cases

Review forms
Add a score input at the top of a review form, with stars previewing a lighter fill on hover and a brighter fill once committed.
Post-order feedback
Ask for a rating after an order summary, with a live label such as Great or Poor describing the chosen score.
Display versus input
Pair with a read-only star rating so the same visual language covers both showing and collecting scores.
Aggregate rating views
Feed collected scores into a rating breakdown, with SVG stars staying crisp and recolourable at any size.
Survey and support scales
Use as a step in an NPS survey, or collect CSAT after a helpful feedback widget interaction.

Got questions?

Frequently Asked Questions

The control keeps hover preview and committed selection as separate classes. Hovering paints a lighter is-hover fill, while clicking sets a persistent is-on fill. On mouseleave only the hover class is cleared, so the committed rating stays shown — the behaviour a star input should have but many implementations get wrong.

A paint helper toggles a class on every star whose index is less than the target count, so hovering or selecting star three lights stars one through three. The count drives which stars are lit, with no per-star bookkeeping, and the same logic serves both the hover preview and the committed rating.

Yes. The group is a role=radiogroup and each star a role=radio with an aria-label like 3 stars and aria-checked reflecting the committed value, so screen readers announce a rating control. Arrow keys move focus, Enter or Space commit, a focus-visible outline shows keyboard focus, and a live text label conveys the score in words.

Call setRating with the initial value on load, which applies the is-on fill to that many stars, sets aria-checked on the correct star, and updates the label. This is useful for edit forms where a user is changing an existing score rather than rating for the first time.

Hold rating and a hover value in state. Render each star lit when its index is less than the hover value (if hovering) or the committed rating otherwise, set onMouseEnter to the hover value, onMouseLeave to clear it, and onClick to commit. Keep the radiogroup roles and aria-checked bound to state. The SVG and CSS port unchanged.