AI CSS Animations

Animated Price Counter

A pricing-table price counts up to $49 while the cents spin through their digits and land on .99. Dollars and cents are two registered integers; the cents pad to two digits with a counter style.

Also known as: price counter animation, pricing number animation, counting price css.

HTML

<div class="txt-price-counter" aria-label="$49.99 per month">
  <span class="txt-price-counter-sign" aria-hidden="true">$</span>
  <span class="txt-price-counter-dollars" aria-hidden="true"></span>
  <span class="txt-price-counter-cents" aria-hidden="true"></span>
  <span class="txt-price-counter-per" aria-hidden="true">/mo</span>
</div>

CSS

@property --txt-price-counter-d {
  syntax: "<integer>";
  initial-value: 0;
  inherits: false;
}
@property --txt-price-counter-c {
  syntax: "<integer>";
  initial-value: 0;
  inherits: false;
}
.txt-price-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: flex-start;
  line-height: 1;
  color: var(--c);
  font-variant-numeric: tabular-nums;}
.txt-price-counter-sign,
.txt-price-counter-cents {
  margin-top: 0.2em;
  font-size: 0.95em;
}
.txt-price-counter-dollars {
  font-size: 2.6em;
  font-weight: 800;
  letter-spacing: -0.04em;
  counter-reset: txt-price-counter-d var(--txt-price-counter-d);
  animation: txt-price-counter-dollars 1.8s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}
.txt-price-counter-dollars::after {
  content: counter(txt-price-counter-d);
}
.txt-price-counter-cents {
  counter-reset: txt-price-counter-c var(--txt-price-counter-c);
  /* four fast turns, ending on 99 */
  animation: txt-price-counter-cents 0.45s linear 4 forwards;
}
.txt-price-counter-cents::after {
  content: "." counter(txt-price-counter-c, decimal-leading-zero);
}
.txt-price-counter-per {
  align-self: flex-end;
  margin: 0 0 0.65em 0.2em;
  font-size: 0.6em;
  font-weight: 600;
  opacity: 0.65;
}
@keyframes txt-price-counter-dollars {
  to { --txt-price-counter-d: 49; }
}
@keyframes txt-price-counter-cents {
  to { --txt-price-counter-c: 99; }
}

@media (prefers-reduced-motion: reduce) {
  .txt-price-counter, .txt-price-counter *, .txt-price-counter::before, .txt-price-counter::after, .txt-price-counter *::before, .txt-price-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