CSS Star Rating Hover Effect

An accessible five-star rating that fills on hover, locks on click, and works from the keyboard.


CSS Star Rating Hover Effect


A CSS star rating lights stars as the pointer moves and keeps the score after a click. The search is crowded with versions that use empty spans and a click listener to paint classes. Those look right and fail the keyboard, the form submit, and the screen reader. This one is five radio buttons. The stars are labels. CSS lights every label that follows the hovered or checked input.

The row is flex-direction: row-reverse, and the inputs are ordered from five down to one in the HTML. The general sibling combinator ~ only looks forward, so reversing the row makes “forward” point toward the lower scores visually. Hovering the third star paints three stars. Checking it keeps them painted when the pointer leaves.

HTML

<fieldset>
  <legend>Rate this snippet</legend>
  <div class="stars">
    <input type="radio" name="rate" id="s5" value="5" />
    <label for="s5">★</label>
    <input type="radio" name="rate" id="s4" value="4" />
    <label for="s4">★</label>
    <input type="radio" name="rate" id="s1" value="1" />
    <label for="s1">★</label>
  </div>
</fieldset>

The legend is the question. Each label is tied to an input with for. The group submits rate=4 like any other radio group. You do not need JavaScript to store the value. The demo’s script only writes “4 of 5” under the stars so the result is visible.

CSS

.stars { display: flex; flex-direction: row-reverse; justify-content: center; }
.stars input { position: absolute; opacity: 0; }
.stars label { font-size: 3.2rem; color: rgba(255,255,255,.28); cursor: pointer; transition: color .15s, transform .15s; }
.stars label:hover,
.stars label:hover ~ label,
.stars input:checked ~ label { color: #f5c451; }
.stars label:hover { transform: translateY(-4px) scale(1.08); }

input:focus-visible + label gets an outline. Hiding the radio with opacity: 0 and a 1px box keeps it focusable. display: none would remove it from the tab order, which is the bug in most “CSS only” ratings.

The lift on hover is the animation. Keep it to a few pixels. A star that jumps half its size makes the hit target feel like it moved out from under the pointer.

Color

Empty stars at 28% white read as available, not disabled, on the burgundy ground. Gold #f5c451 is the selected state and it is bright enough against #3f1219. If you put this widget on a white card, empty stars need a gray around #a8a29e and the selected gold needs to be darker, or the empty and filled states blend.

Keyboard

Arrow keys move between radios because that is what a radio group does. Tab enters and leaves the group. The legend is announced with the control. Do not replace this with divs and aria-pressed unless you are ready to rebuild those keys yourself.

Questions about star ratings

How do I make a star rating in pure CSS?

Use radio inputs and labels, order the inputs from high to low, lay them out in reverse, and color label:hover ~ label plus input:checked ~ label. The sibling combinator paints the hovered star and the lower ones.

Why does hovering a star only light that one star?

The labels are in the wrong order for ~, which cannot look backward. Put 5 first in the DOM and reverse the flex direction so 1 appears on the left.

Can I show half stars?

Not honestly with one radio per whole star. Half stars need either ten radios with a clipped label, or a separate visual painted from a numeric value. Whole stars are the version that stays a simple form control.