AI CSS Animations

Star Rating Hover Effect

Hovering a star lights it and every star before it, previews a word for that score and pops the star with a spring. The trick is one :has() rule to light all stars, then ~ to unlight the rest.

Also known as: css star rating hover, rating stars on hover, 5 star rating css.

HTML

<div class="hfx-star-rating" role="group" aria-label="Rate this recipe">
  <button aria-label="1 star"><svg viewBox="0 0 24 24" aria-hidden="true"><path fill="currentColor" stroke="currentColor" stroke-width="1.5" stroke-linejoin="round" d="M12 3.2l2.7 5.6 6.1.8-4.5 4.2 1.1 6.1L12 17l-5.4 2.9 1.1-6.1-4.5-4.2 6.1-.8z"/></svg></button>
  <button aria-label="2 stars"><svg viewBox="0 0 24 24" aria-hidden="true"><path fill="currentColor" stroke="currentColor" stroke-width="1.5" stroke-linejoin="round" d="M12 3.2l2.7 5.6 6.1.8-4.5 4.2 1.1 6.1L12 17l-5.4 2.9 1.1-6.1-4.5-4.2 6.1-.8z"/></svg></button>
  <button aria-label="3 stars"><svg viewBox="0 0 24 24" aria-hidden="true"><path fill="currentColor" stroke="currentColor" stroke-width="1.5" stroke-linejoin="round" d="M12 3.2l2.7 5.6 6.1.8-4.5 4.2 1.1 6.1L12 17l-5.4 2.9 1.1-6.1-4.5-4.2 6.1-.8z"/></svg></button>
  <button aria-label="4 stars"><svg viewBox="0 0 24 24" aria-hidden="true"><path fill="currentColor" stroke="currentColor" stroke-width="1.5" stroke-linejoin="round" d="M12 3.2l2.7 5.6 6.1.8-4.5 4.2 1.1 6.1L12 17l-5.4 2.9 1.1-6.1-4.5-4.2 6.1-.8z"/></svg></button>
  <button aria-label="5 stars"><svg viewBox="0 0 24 24" aria-hidden="true"><path fill="currentColor" stroke="currentColor" stroke-width="1.5" stroke-linejoin="round" d="M12 3.2l2.7 5.6 6.1.8-4.5 4.2 1.1 6.1L12 17l-5.4 2.9 1.1-6.1-4.5-4.2 6.1-.8z"/></svg></button>
  <span class="hfx-star-rating-label" aria-hidden="true"></span>
</div>

CSS

.hfx-star-rating {
  --c: var(--hfx-color, #3b82f6);
  font-size: var(--hfx-size, 16px);
  display: flex;
  align-items: center;
  font-family: system-ui, sans-serif;
}
.hfx-star-rating button {
  padding: 0.15em;
  border: 0;
  background: none;
  color: color-mix(in srgb, var(--c) 22%, transparent);
  cursor: pointer;
  transition: color 0.15s ease, transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.hfx-star-rating svg {
  display: block;
  width: 1.9em;
  height: 1.9em;
}
/* the saved rating: three stars */
.hfx-star-rating button:nth-child(-n + 3) { color: var(--c); }
/* hovering: light every star, then unlight the ones after the hovered star */
.hfx-star-rating:has(button:is(:hover, :focus-visible)) button { color: var(--c); }
.hfx-star-rating button:is(:hover, :focus-visible) ~ button { color: color-mix(in srgb, var(--c) 22%, transparent); }
.hfx-star-rating button:is(:hover, :focus-visible) { transform: scale(1.25) rotate(-10deg); }
.hfx-star-rating-label {
  width: 5em;
  margin-left: 0.6em;
  font-weight: 600;
  color: var(--c);
}
.hfx-star-rating-label::after { content: "3.0"; }
.hfx-star-rating:has(button:nth-child(1):is(:hover, :focus-visible)) .hfx-star-rating-label::after { content: "Awful"; }
.hfx-star-rating:has(button:nth-child(2):is(:hover, :focus-visible)) .hfx-star-rating-label::after { content: "Meh"; }
.hfx-star-rating:has(button:nth-child(3):is(:hover, :focus-visible)) .hfx-star-rating-label::after { content: "Good"; }
.hfx-star-rating:has(button:nth-child(4):is(:hover, :focus-visible)) .hfx-star-rating-label::after { content: "Great"; }
.hfx-star-rating:has(button:nth-child(5):is(:hover, :focus-visible)) .hfx-star-rating-label::after { content: "Loved it!"; }

@media (prefers-reduced-motion: reduce) {
  .hfx-star-rating, .hfx-star-rating *, .hfx-star-rating::before, .hfx-star-rating::after, .hfx-star-rating *::before, .hfx-star-rating *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

Customizing this snippet

  • Color: set --hfx-color on the element or a parent. Currently previewing #3b82f6.
  • Base size: set --hfx-size. Every other dimension is in em, so the whole thing scales together. Currently previewing 16px.
  • Speed: change the duration on the transition or animation shorthand. Staggered delays are relative, so they keep their rhythm.
  • Reduced motion: the rule at the end of the CSS turns the motion off for people who asked their OS for less of it. Keep it.

Similar hover effects