Table of Contents
New CSS Solution
Can we animate decimals?
Other tips
Home Web Front-end CSS Tutorial Animating Number Counters

Animating Number Counters

Apr 02, 2025 am 03:24 AM

Animating Number Counters

CSS digital animation, for example, imagine a number changing from 1 to 2, then changing from 2 to 3, then changing from 3 to 4, etc., lasting a specified time. It's like a counter, but controlled by the same animation we use to design animations on the web. This can be useful when designing things like dashboards and can add some vitality to the numbers. Surprisingly, this is now possible in CSS without much skill. You can just jump straight to the new solution if you like, but first let's see how we did it in the past .

A rather logical way to animation numbers is to change numbers through JavaScript. We could do a fairly simple setInterval, but here is a more advanced answer that uses a function that accepts the start value, the end value, and the duration, so you can treat it more like an animation:

If we only use CSS, we can use the CSS counter to animate the numbers by adjusting the counts in different keyframes:

Another way is to line all the numbers in a row and animate their positions, showing only one number at a time:

Some of the duplicate code in these examples can use preprocessors like Pug (for HTML) or SCSS (for CSS) that provide loops to make them easier to manage, but deliberately use native code for purposes so that you can see the basic idea.

New CSS Solution

With the latest support for CSS.registerProperty and @property, we can animate CSS variables . The trick is to declare CSS custom properties as integers; this allows you to interpolate them like any other integer (e.g. in a conversion).

 @property --num {
  syntax: '<integer> ';
  initial-value: 0;
  inherits: false;
}

div {
  transition: --num 1s;
  counter-reset: num var(--num);
}
div:hover {
  --num: 10000;
}
div::after {
  content: counter(num);
}</integer>
Copy after login

Important Note: At the time of writing, this @property syntax is supported only by Chrome (and other Chromium kernel browsers such as Edge and Opera), so it is not cross-browser compatible. If you are building a Chrome-only app (such as the Electron app), you can use it right away, otherwise wait. The above mentioned demo has wider support.

The CSS content property can be used to display numbers, but we still need to use counter to convert numbers to strings, because content can only output<string></string> value.

See if we can ease the animation like any other animation? Super cool!

Typed CSS variables can also be used in @keyframes:

A disadvantage? Counters only support integers. This means that decimals and fractions are not considered. We have to display the integer part and the decimal part separately in some way.

Can we animate decimals?

You can convert decimals (such as --number) into integers. Here is how it works:

  1. Register one<integer></integer> CSS variable (for example --integer) and specify initial-value.
  2. Then use calc() to round: --integer: calc(var(--number))

In this case, --number will round to the nearest integer and store the result in --integer.

 @property --integer {
  syntax: "<integer> ";
  initial-value: 0;
  inherits: false;
}
--number: 1234.5678;
--integer: calc(var(--number)); /* 1235 */</integer>
Copy after login

Sometimes we only need integer parts. There is a clever way to do this: --integer: max(var(--number) - 0.5, 0). This applies to positive numbers. This method doesn't even require calc().

 /* @property --integer */
--number: 1234.5678;
--integer: max(var(--number) - 0.5, 0); /* 1234 */
Copy after login

We can extract the fractional part in a similar way and then convert it to a string using a counter (but remember that the content value must be a string). To display the connected string, use the following syntax:

 content: "string1" var(--string2) counter(--integer) ...
Copy after login

Here is a complete example of animating with decimal percentages:

Other tips

Because we are using CSS counters, the format of these counters can be in other formats than numbers. Here is an example of animating the letter "CSS" into "YES"!

Oh, and another trick: we can debug the value of the custom attribute through JavaScript:

 getComputedStyle(element).getPropertyValue('--variable')
Copy after login

That's it! Today's CSS features are amazing.

The above is the detailed content of Animating Number Counters. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Working With GraphQL Caching Working With GraphQL Caching Mar 19, 2025 am 09:36 AM

If you’ve recently started working with GraphQL, or reviewed its pros and cons, you’ve no doubt heard things like “GraphQL doesn’t support caching” or

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

Creating Your Own Bragdoc With Eleventy Creating Your Own Bragdoc With Eleventy Mar 18, 2025 am 11:23 AM

No matter what stage you’re at as a developer, the tasks we complete—whether big or small—make a huge impact in our personal and professional growth.

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That&#039;s like this.

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

Let's use (X, X, X, X) for talking about specificity Let's use (X, X, X, X) for talking about specificity Mar 24, 2025 am 10:37 AM

I was just chatting with Eric Meyer the other day and I remembered an Eric Meyer story from my formative years. I wrote a blog post about CSS specificity, and

See all articles