DataPremium

Counter Animation CSS

A number that climbs from 0 to its value with no script: the stylesheet does the counting, using an animated integer variable and a CSS counter.

CSS@property

Updated

This effect is part of Effect.Labs — 846 vanilla effects, some free, some premium. Data has 28 effects, including 1 free. Explore the category →

3 usage examples

Crêpe recipe quantities by number of guests — card in a kitchen notebook — Counter Animation CSS example 1

① Crêpe recipe quantities by number of guests — card in a kitchen notebook

WhenOn a recipe card, three buttons "serves 2, 4, 6" each show their own ingredient list. On every choice, the grams of flour, millilitres of milk and number of eggs count again up to the new quantity.
WhyAll three lists live in the page and only one is displayed: the one that reappears restarts its counting by itself. The reader sees that quantities changed, line by line.
SettingsFifteen numbers (five per list), --ccss-size:20px, --ccss-duration:.9s, --ccss-color:#b45309, transparent background, root set to inline-flex with no minimum height; the lists not chosen carry the hidden attribute. In the scene, a click outside the buttons moves to the next number of guests.
The champion pumpkin at 617 kg — header of a vegetable garden contest page — Counter Animation CSS example 2

② The champion pumpkin at 617 kg — header of a vegetable garden contest page

WhenAt the top of the results page, the winner's weight fills half the screen and climbs from 0 to 617, like the needle of a scale, next to the drawing of the pumpkin.
WhyA record weight tells its story better when you watch it climb than when you read it. A three-digit integer needs no separator: ideal ground for a CSS counter.
Settings--ccss-to:617, --ccss-size:112px, --ccss-duration:1.8s, --ccss-color:#fb923c, line height 1 on .ccss-num, "kg" unit in a sibling element. The scene's "Replay the weigh-in" button hides then shows the number to make it count again.
Seats booked, workshop by workshop — programme of a science festival — Counter Animation CSS example 3

③ Seats booked, workshop by workshop — programme of a science festival

WhenIn the programme of a day of workshops, each row shows "22 of 24 seats". The four numbers count together when the list scrolls into view, in orange, red or green depending on what is left.
WhyFour small numbers finishing at different moments draw the eye to the seats column without animating the whole row; the colour already says whether to hurry.
Settings--ccss-size:21px, one duration per row (.7s to 1.4s), --ccss-color by state (#15803d, #c2410c, #b91c1c), transparent background. For the capture, the list is shown again once when the pointer first moves over it; on a page, counting plays when the list enters the screen.

How it works

The number is not written anywhere in the page: .ccss-num is empty and its ::after shows counter(ccss). That CSS counter is fed at every instant by --ccss-n, a variable registered with @property as an <integer>. Because it has a type, the browser knows how to move it from 0 to the target one unit at a time; the ccss-count animation is a single line, from { --ccss-n: 0 }.

The target is set in the HTML with style="--ccss-to: 617", the duration with --ccss-duration (3 s, ease curve: brisk start, slower finish). Several numbers on one page each keep their own target, duration and size.

The script that ships with it computes no digit at all. It adds the ccss-waiting class, which pauses the animation, then removes it once the number is half visible. Without it, counting simply starts when the page loads.

A useful side effect: when a block hidden with display: none is shown again (tab, panel, form step), the browser restarts the animation and the number counts again from 0, with nothing to call. Inside a <details>, however, it only counts the first time it is opened.

Accessibility

  • Screen readers: CSS-generated content is not read reliably. The drawn number is therefore aria-hidden and the value is repeated as plain text in .ccss-sr, invisible to the eye. That text is not tied to --ccss-to: always edit both together.
  • Reduced motion: the animation: none rule applies and the final value is shown from the start.
  • Stable width: tabular-nums gives every digit the same width; the number does not jitter while counting, it only widens when it gains a digit (99 → 100).
  • Selection: like all generated content, the displayed number cannot be selected with the mouse or copied.
  • Keyboard: nothing to activate, the number is plain text and never takes focus.

Browser compatibility

@property (animatable integer variable), CSS counters, calc() inside counter-reset; IntersectionObserver for the wait. Reading taken 1.5 s into a count towards 48210: between 38,000 and 39,000 in Chromium 149, Firefox 151 and WebKit 26.5. WebKit used to give the variable a fractional value and leave "0" until the end; the sold code now rounds with calc().

Chrome 85+✓ Full (measured)
Firefox 128+✓ Full (measured); before 128, no gradual counting
Safari 16.4+✓ WebKit engine measured, not checked in real Safari
Edge 85+✓ Full (Chromium engine)
Safari iOS 16.4+✓ Not checked on a real device
Chrome Android 85+✓ Full (Chromium engine)

Without @property the variable cannot advance step by step: the number stays at 0 and then jumps to its value partway through (simulated by removing the declaration, not checked on an old browser). Without JavaScript everything works; counting starts on page load instead of waiting for the number to be visible.

The code

HTML structure to paste into your page (the CSS + JS come with your purchase):

index.html — structure
<div class="fx-0233-root">
  <div class="ccss-num" style="--ccss-to: 100" aria-hidden="true"></div>
  <span class="ccss-sr">100</span>
</div>

One-time payment, no subscription — the code is yours forever

Customize

Options passed to the API or data-* attributes:

Option / propertyDefaultEffect
--ccss-to on .ccss-num (HTML) 100 End value. Integers only: no decimals, no thousands separator.
Text of .ccss-sr (HTML) 100 Value read by screen readers; keep it identical to --ccss-to.
--ccss-duration (CSS) 3s Counting time, from 0 to the value.
--ccss-size (CSS) 3rem Size of the number.
--ccss-color (CSS) #6366f1 Colour of the number.
--ccss-bg (CSS) #0a0a0f Block background; transparent to sit the number on your page.
Root min-height and display (CSS) 300px, flex The root is a block that centres the number. Switch to min-height:0; display:inline-flex; width:auto to slip it into a sentence or a table row.
threshold (JS) 0.5 Share of the number that must be visible to start counting. Remove the whole script to count on page load.

FAQ

Animated Counter computes every value in JavaScript: it writes the number in the page language's format (8,547 or 8 547), accepts decimals and counts only once. Here the browser does the counting: no script required, a fresh count every time the block is shown again, but raw integers. To display "48,210" with its comma, pick Animated Counter.
The simplest way is a sibling element (<span>kg</span>). To build it into the number itself, extend the rule: .ccss-num::after { content: counter(ccss) "\a0kg" }. The counter style changes the same way: counter(ccss, decimal-leading-zero) shows 07 instead of 7.
Changing --ccss-to once counting is over shows the new value at once: the animation does not replay. To count again, hide the block and show it again (display: none, then back): the CSS animation restarts from 0 towards the value in place.