Components

Number counter animation: counting stats in CSS and JavaScript

Animate stat numbers counting up: a pure CSS @property counter, a requestAnimationFrame version with easing and separators, rolling digits and a React hook.

20 November 2026 · 9 min read

Stat rows on landing pages often count up when they scroll into view: 0 to 12,480 customers, 0 to 98.6% uptime. Done well, it draws the eye to the numbers for a second and then gets out of the way. Done badly, the row jitters sideways, the numbers start counting before anyone can see them, and a screen reader reads out forty intermediate values.

This guide covers three ways to build a number counter: pure CSS with @property, a JavaScript version with requestAnimationFrame, easing and locale-aware separators, and a rolling odometer where each digit slides into place. All of them start when scrolled into view, keep a steady width, and announce only the final value.

animations captured
% uptime
4.9 rolling digits
Counts when scrolled into view

How the animation is put together

PartPropertyDurationDelayEasing
Large count (12,480)text, 0 → 124801800 ms0, on 50% visibilityeaseOutCubic
Percentage (98.6)text, 0 → 98.6, one decimal1500 ms0, on 50% visibilityeaseOutCubic
Odometer digit 1transform: translateY1400 ms0cubic-bezier(.2, .8, .2, 1)
Odometer digit 2transform: translateY1400 ms120 mscubic-bezier(.2, .8, .2, 1)
CSS-only variant--n (<integer>)2000 ms0cubic-bezier(.2, .8, .2, 1)

Every counter uses an ease-out curve. Fast at the start and slow at the end means the last few values, the ones people actually read, stay on screen longest. A linear count spends as long on 3,000 as on 12,000 and stops abruptly. The easing functions guide shows the curves side by side.

Pure CSS with @property

CSS cannot interpolate text, but it can interpolate a registered custom property, and a counter can print an integer property as text.

<span class="cnt-css" style="--to: 1280">1,280</span>
@property --n {
  syntax: '<integer>';
  initial-value: 0;
  inherits: false;
}

.cnt-css {
  font-size: 0;                         /* hide the fallback text */
  counter-reset: n var(--n);
  animation: cnt-count 2s cubic-bezier(.2, .8, .2, 1) forwards;
}
.cnt-css::after {
  content: counter(n);
  font-size: 32px;
  font-variant-numeric: tabular-nums;
}

@keyframes cnt-count { to { --n: var(--to); } }

@media (prefers-reduced-motion: reduce) {
  .cnt-css { animation: none; --n: var(--to); }
}

Because --n is registered as an <integer>, the browser interpolates it and rounds each frame to a whole number. counter-reset copies it into a counter, and counter() prints it. Without the registration, the property would flip from start to end halfway through instead of counting.

The limits are real. @property is supported in all current major browsers, but older ones ignore it: the number then jumps to the end value. counter() prints plain integers, so there are no thousands separators and no decimals. Text generated in ::after is not selectable, and screen reader support for it is inconsistent, which is why the real value stays in the element as text. Use this version for small, unformatted numbers; use JavaScript for anything else. You can also tie it to scroll position instead of time with scroll-driven animations.

JavaScript with requestAnimationFrame

const easeOutCubic = t => 1 - Math.pow(1 - t, 3);

function countUp(el, to, { duration = 1600, decimals = 0 } = {}) {
  const fmt = new Intl.NumberFormat(undefined, {
    minimumFractionDigits: decimals,
    maximumFractionDigits: decimals,
  });
  if (matchMedia('(prefers-reduced-motion: reduce)').matches) {
    el.textContent = fmt.format(to);
    return;
  }
  let start = null;
  const frame = now => {
    if (start === null) start = now;
    const t = Math.min((now - start) / duration, 1);
    el.textContent = fmt.format(to * easeOutCubic(t));
    if (t < 1) requestAnimationFrame(frame);
  };
  requestAnimationFrame(frame);
}

// start once, when half the row is visible
const row = document.querySelector('.cnt-row');
const io = new IntersectionObserver(([entry]) => {
  if (!entry.isIntersecting) return;
  io.disconnect();
  row.querySelectorAll('[data-count]').forEach(el =>
    countUp(el, +el.dataset.count, { decimals: +(el.dataset.decimals || 0) }));
}, { threshold: 0.5 });
io.observe(row);

The value is computed from elapsed time, not incremented per frame, so the count finishes on time even if frames are dropped. Intl.NumberFormat adds separators and fixes the decimal places for the visitor's locale, so 12480 prints as 12,480 or 12.480 as appropriate. Pass a locale such as 'en-US' instead of undefined if your page copy assumes one format.

The IntersectionObserver waits until half the row is on screen and then disconnects, so the count plays once, when someone can see it.

Stopping the width jitter

In most fonts the digit 1 is narrower than 8, so a counting number changes width every frame and anything centered next to it shakes. Two fixes, used together:

  • font-variant-numeric: tabular-nums gives every digit the same width.
  • Reserve the final width. 0 is one character and 12,480 is six. Stack an invisible copy of the final value and the counting value in the same grid cell, and the box is sized by the final value from the start.
<span class="cnt-value">
  <span class="cnt-final">12,480</span>
  <span class="cnt-num" aria-hidden="true" data-count="12480">0</span>
</span>
.cnt-value { display: inline-grid; font-variant-numeric: tabular-nums; }
.cnt-final, .cnt-num { grid-area: 1 / 1; }
.cnt-final { opacity: 0; }

The final copy uses opacity: 0 rather than visibility: hidden, so it stays in the accessibility tree. That doubles as the accessibility fix below.

Rolling digits

An odometer gives each digit its own column of 0 to 9 inside a one-line window, then slides the column to the right digit.

.cnt-digit { display: inline-block; height: 1em; line-height: 1; overflow: hidden; }
.cnt-reel {
  display: flex; flex-direction: column;
  transform: translateY(calc(var(--d, 0) * -10%));
  transition: transform 1.4s cubic-bezier(.2, .8, .2, 1) var(--delay, 0s);
}
.cnt-reel > span { height: 1em; }

The reel is ten lines tall, so -10% of its own height is exactly one digit. Set --d on each digit to its target and the transition does the rest; a 120 ms stagger per digit makes it read as a mechanism. To replay, turn transitions off, reset --d to 0, force a reflow, then set the targets again on the next frame, as the demo script does. This version animates transform only, so it stays smooth even when the main thread is busy.

The React version

import { useEffect, useRef } from 'react';

const easeOutCubic = t => 1 - Math.pow(1 - t, 3);

export function Counter({ to, decimals = 0, duration = 1600, locale }) {
  const numRef = useRef(null);
  const fmt = new Intl.NumberFormat(locale, {
    minimumFractionDigits: decimals, maximumFractionDigits: decimals });

  useEffect(() => {
    const el = numRef.current;
    const reduce = matchMedia('(prefers-reduced-motion: reduce)').matches;
    let raf = 0;
    const io = new IntersectionObserver(([entry]) => {
      if (!entry.isIntersecting) return;
      io.disconnect();
      if (reduce) { el.textContent = fmt.format(to); return; }
      let start = null;
      const frame = now => {
        if (start === null) start = now;
        const t = Math.min((now - start) / duration, 1);
        el.textContent = fmt.format(to * easeOutCubic(t));
        if (t < 1) raf = requestAnimationFrame(frame);
      };
      raf = requestAnimationFrame(frame);
    }, { threshold: 0.5 });
    io.observe(el);
    return () => { io.disconnect(); cancelAnimationFrame(raf); };
  }, [to, decimals, duration, locale]);

  return (
    <span className="cnt-value">
      <span className="cnt-final">{fmt.format(to)}</span>
      <span ref={numRef} className="cnt-num" aria-hidden="true">{fmt.format(0)}</span>
    </span>
  );
}

The count writes to textContent through a ref instead of state, so a two-second count does not cause a hundred React renders. The rendered output is the same width-reserving markup as above.

Accessibility notes

  • The counting element is aria-hidden="true"; the final value sits beside it as real text. Screen readers read "12,480" once, never the values in between.
  • Do not put aria-live on a counter. A live region that changes sixty times a second either floods the user or gets throttled into nonsense. If the number really is live data, announce the settled value once, after the count ends.
  • Under prefers-reduced-motion: reduce, every version shows the final value straight away, and the replay button is hidden. See the reduced motion guide for the general pattern.
  • Keep counts under about two seconds. The number is the content; the animation is only there to point at it.

Match a counter you have seen

If a site's counter has a feel you want, open it and run the Capture Motion extension while it plays. When the count runs on a CSS animation, the Web Animations API or GSAP, it records the duration, delay and easing and exports them as CSS, React, GSAP or JSON. For ready-made animated components you can customize and copy, browse the Capture Motion gallery.

Questions

Can I animate a number with only CSS?

Yes. Register a custom property with @property as an <integer>, animate it in @keyframes, and print it with counter-reset and counter() in a pseudo-element. Browsers without @property support skip the count and show the end value once the animation finishes.

Why does my counter jitter sideways while it counts?

Most fonts give digits different widths, so the number changes width on every frame. Set font-variant-numeric: tabular-nums and reserve the width of the final value, for example by stacking an invisible copy of it in the same grid cell.

How do I make screen readers announce only the final number?

Hide the counting element with aria-hidden and keep the final value in the DOM as real text next to it. Do not put aria-live on the counting element, or it may try to announce every intermediate value.

How can I match the counter timing from another website?

If the count is driven by a CSS animation, the Web Animations API or GSAP, the Capture Motion Chrome extension records its duration, delay and easing and exports them as CSS, React, GSAP or JSON. For a counter that changes only its text from a script, compare the feel against the easing curves in this guide.