AI CSS Animations

Star Rating Counter

A review score counts from 0.0 to 4.9 while five stars fill to match. One integer is animated; the whole and tenths digits are derived from it by two more registered integers.

Also known as: rating number animation, review score counter, animated star rating.

2,318 reviews

HTML

<div class="txt-rating-counter" aria-label="Rated 4.9 out of 5 from 2,318 reviews">
  <span class="txt-rating-counter-num" aria-hidden="true"></span>
  <span class="txt-rating-counter-meta">
    <span class="txt-rating-counter-stars" aria-hidden="true">★★★★★</span>
    <span>2,318 reviews</span>
  </span>
</div>

CSS

@property --txt-rating-counter-n {
  syntax: "<integer>";
  initial-value: 0;
  inherits: false;
}
@property --txt-rating-counter-whole {
  syntax: "<integer>";
  initial-value: 0;
  inherits: false;
}
@property --txt-rating-counter-tenth {
  syntax: "<integer>";
  initial-value: 0;
  inherits: false;
}
.txt-rating-counter {
  --c: var(--txt-color, #3b82f6);
  font-size: var(--txt-size, 32px);
  font-family: system-ui, sans-serif;
  font-weight: 700;
  line-height: 1.2;
  display: inline-flex;
  align-items: center;
  gap: 0.45em;
  --ease: cubic-bezier(0.22, 1, 0.36, 1);}
.txt-rating-counter-num {
  font-size: 1.9em;
  line-height: 1;
  color: var(--c);
  font-variant-numeric: tabular-nums;
  /* 49 tenths: an integer rounds to nearest, so n / 10 - 0.5 is the floor */
  --txt-rating-counter-whole: calc(var(--txt-rating-counter-n) / 10 - 0.5);
  --txt-rating-counter-tenth: calc(var(--txt-rating-counter-n) - var(--txt-rating-counter-whole) * 10);
  counter-reset: txt-rating-counter-w var(--txt-rating-counter-whole) txt-rating-counter-t var(--txt-rating-counter-tenth);
  animation: txt-rating-counter-count 2s var(--ease) forwards;
}
.txt-rating-counter-num::after {
  content: counter(txt-rating-counter-w) "." counter(txt-rating-counter-t);
}
.txt-rating-counter-meta {
  display: flex;
  flex-direction: column;
  gap: 0.15em;
  font-size: 0.5em;
  font-weight: 600;
  opacity: 0.9;
}
.txt-rating-counter-stars {
  position: relative;
  width: max-content;
  font-size: 1.6em;
  line-height: 1;
  letter-spacing: 0.05em;
  color: color-mix(in srgb, var(--c) 22%, transparent);
}
.txt-rating-counter-stars::after {
  content: "★★★★★";
  position: absolute;
  inset: 0;
  color: var(--c);
  clip-path: inset(0 100% 0 0);
  animation: txt-rating-counter-fill 2s var(--ease) forwards;
}
@keyframes txt-rating-counter-count {
  to { --txt-rating-counter-n: 49; }
}
@keyframes txt-rating-counter-fill {
  to { clip-path: inset(0 3% 0 0); }
}

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

Customizing this snippet

  • Color: set --txt-color on the element or a parent. Currently previewing #3b82f6.
  • Font size: set --txt-size. Every other dimension is in em, so the whole thing scales together. Currently previewing 40px.
  • 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 text animations