Count Up

Counts a number up to its value when it scrolls into view.

zweiundeins <sb-count-up> number counter animation statistics intl Since 2026-09-23 MIT licence 918 B1 Open in playground Edit on GitHub

Playground

Counts a number to its value the first time it is on screen: for the statistics on a landing page, a total that just changed, anything that should arrive rather than appear. It takes its font, size and colour from where you put it.

Put the server-formatted number inside it. That is what search engines and people without JavaScript see, and what shows until the module loads: formatted the way the element formats it (for the page's lang), it takes the same room, so nothing on the line moves when the count takes over. Screen readers always hear the final value rather than the frames of the count, and a printed page shows the final value too.

Examples

On scroll

It counts once, the first time it is on screen. Moving it elsewhere in the page doesn't count it again.

1,113 models, 56 makes.

<p style="font-size: 1.25rem">
  <sb-count-up value="1113" style="font-size: 2.5rem; font-weight: 700">1,113</sb-count-up>
  models, <sb-count-up value="56">56</sb-count-up> makes.
</p>

Decimals and units

decimals is kept for every frame, so the width does not change while it counts. Units stay outside the element.

CHF 0.52 / km

<p style="font-size: 2rem">
  CHF <sb-count-up value="0.52" decimals="2" duration="2000">0.52</sb-count-up> / km
</p>

A new value

When the server sends a new value while the number is on screen, it counts from where it stands.

1,200 Add an order
<div data-signals="{_total: 1200}" style="display: flex; gap: 1rem; align-items: center">
  <sb-count-up data-attr:value="$_total" data-preserve-attr="value" style="font-size: 2rem">1,200</sb-count-up>
  <sb-button data-on:click="$_total += Math.round(Math.random() * 500)">Add an order</sb-button>
</div>

Styling

Style it from your page's CSS, without changing the component or importing anything into it. Custom properties, inherited properties and ::part() all reach into its shadow root.

  • Fonts and colours: it is text: it takes the font, size, weight and colour of wherever you put it, and only switches the digits to equal widths (tabular-nums) so the number doesn't jitter while it counts. It keeps room for the final value from the start, so the text around it stays put while the digits grow.
  • Parts: value, the number you see while it counts. An invisible copy of the final value holds the width; screen readers read it, and print shows it instead of value. ::part(value) rules don't reach that copy, so put what changes the width (letter-spacing, font-size) on the element itself. Your page's ::part() rules win over the component's own, without !important.
4,681
<style>
  .my-count { font-size: 2.5rem; font-weight: 700; letter-spacing: 0.05em; }
  .my-count::part(value) { color: var(--sb-brand-light); }
</style>
<sb-count-up class="my-count" value="4681">4,681</sb-count-up>

Accessibility

Screen readers get the final value as text; the counting digits are hidden from them. With prefers-reduced-motion: reduce the number is shown at its value straight away, and a new value replaces it without counting.

Installation

Add Datastar with Rocket and the Starbase autoloader once per page, then use the tag: the autoloader imports each component the first time its tag appears, including tags a Datastar morph adds later. The Datastar here is v1.0.5 with Starbase's patches. The components also run on the official v1.0.4 and v1.0.5.

<script type="importmap">
  { "imports": { "datastar": "https://starbase.zweiundeins.gmbh/c/datastar@5f98646d7d3d/datastar-rocket.js" } }
</script>
<script type="module" src="https://starbase.zweiundeins.gmbh/c/autoloader.js"></script>

<sb-count-up value="4681">4,681</sb-count-up>

No flash of undefined elements: put class="sb-cloak" on <html> and add .sb-cloak :not(:defined) { visibility: hidden } to your CSS. The autoloader removes the class once the page's components are defined.

Load just this component, pinned to this version: all its modules in one minified file with its integrity hash, so the browser refuses it if a single byte changes.

<script type="importmap">
  {
    "imports": { "datastar": "https://starbase.zweiundeins.gmbh/c/datastar@5f98646d7d3d/datastar-rocket.js" },
    "integrity": {
      "https://starbase.zweiundeins.gmbh/c/datastar@5f98646d7d3d/datastar-rocket.js": "sha384-AfXVdZ/qxhMEHK4Nz/pJT6jl9xm3HGMBLBtC9yRuU5A7AQtXHYC0x6ap034vDqBV"
    }
  }
</script>
<script type="module" src="https://starbase.zweiundeins.gmbh/c/count-up@53ad52062cea/count-up.bundle.min.js" integrity="sha384-agtGFGEoFd2B/gZY8j1OGVM4DzjKv3goak49nCEgEdXBCobGtELaUGGiluNhK1VL"></script>

<sb-count-up value="4681">4,681</sb-count-up>

Or the minified module files (one file, which the browser requests as it finds their imports), each pinned in the import map:

<script type="importmap">
  {
    "imports": { "datastar": "https://starbase.zweiundeins.gmbh/c/datastar@5f98646d7d3d/datastar-rocket.js" },
    "integrity": {
      "https://starbase.zweiundeins.gmbh/c/datastar@5f98646d7d3d/datastar-rocket.js": "sha384-AfXVdZ/qxhMEHK4Nz/pJT6jl9xm3HGMBLBtC9yRuU5A7AQtXHYC0x6ap034vDqBV",
      "https://starbase.zweiundeins.gmbh/c/count-up@53ad52062cea/count-up.min.js": "sha384-Nblr+W4VQ2swmeTv3Lm+61G/pgq+5lLOnkGI0/x/oPC/4bKmeytowEkDYfUoqYhr"
    }
  }
</script>
<script type="module" src="https://starbase.zweiundeins.gmbh/c/count-up@53ad52062cea/count-up.min.js" integrity="sha384-Nblr+W4VQ2swmeTv3Lm+61G/pgq+5lLOnkGI0/x/oPC/4bKmeytowEkDYfUoqYhr"></script>

<sb-count-up value="4681">4,681</sb-count-up>

In production, pin today's whole catalog: its autoloader and an import map with integrity hashes, so the browser refuses any file that changed. This map covers Datastar and this component; importmap.json has the hashes of every component, to merge into yours.

<script type="importmap">
  {
    "imports": { "datastar": "https://starbase.zweiundeins.gmbh/c/datastar@5f98646d7d3d/datastar-rocket.js" },
    "integrity": {
      "https://starbase.zweiundeins.gmbh/c/datastar@5f98646d7d3d/datastar-rocket.js": "sha384-AfXVdZ/qxhMEHK4Nz/pJT6jl9xm3HGMBLBtC9yRuU5A7AQtXHYC0x6ap034vDqBV",
      "https://starbase.zweiundeins.gmbh/c/count-up@53ad52062cea/count-up.bundle.min.js": "sha384-agtGFGEoFd2B/gZY8j1OGVM4DzjKv3goak49nCEgEdXBCobGtELaUGGiluNhK1VL"
    }
  }
</script>
<script type="module" src="https://starbase.zweiundeins.gmbh/c/@81f24b268adb/autoloader.js" integrity="sha384-bWIhsIq9SpNUiIZyG/lv190MgHA05m8BVYA3LyIoE/pgOF3NUt0437e++4Ht1NcS"></script>

<sb-count-up value="4681">4,681</sb-count-up>

Components import only datastar and their own files, so copying is enough: save each component's one file (and a file it loads on first use, keeping its folder), and point the import map at your own copy of datastar-rocket.js (the /js/ paths stand for yours).

  • <sb-count-up>
    • count-up.bundle.min.js one file 918 B brotli
<script type="importmap">
  { "imports": { "datastar": "/js/datastar-rocket.js" } }
</script>
<script type="module" src="/js/count-up/count-up.bundle.min.js"></script>

<sb-count-up value="4681">4,681</sb-count-up>

Or copy the module files, keeping each component's folder, and load each component's module:

  • <sb-count-up>
<script type="importmap">
  { "imports": { "datastar": "/js/datastar-rocket.js" } }
</script>
<script type="module" src="/js/count-up/count-up.min.js"></script>

<sb-count-up value="4681">4,681</sb-count-up>

In Go, render it with its typed templ wrapper: one field per prop, generated from the component's manifest (the module's README has the type rules). Add the module:

go get github.com/zweiundeins/starbase/templ@latest

Then load it, pinned with integrity hashes to the versions the module was generated with, and use it:

// In <head>: the components you list, pinned,
@sb.Imports(sb.ImportsProps{Components: []sb.Tag{sb.TagCountUp}})
// or the autoloader, which also loads tags a morph adds later: @sb.Imports(sb.ImportsProps{})
// or, around the content of <body>, the components it renders: @sb.Tracked(sb.ImportsProps{}) { … }

@sb.CountUp(sb.CountUpProps{Value: 4681}) {
	4,681
}

Size

A page loads this component as one file, its bundle, compressed the way it is served (gzip -9, brotli -11): brotli is what it downloads. Datastar and Rocket are shared by every component and not counted.

FileOriginalgzipbrotli
count-up.bundle.min.js 1.8 kB1.1 kB918 B
As module files: one request, 859 B minified with brotli

For a page that loads the module files as they are, requesting each one as it finds its import, or bundles them itself. Each file compressed on its own; the minified one is what the page downloads, and the readable source is always there too.

FileOriginalgzipbrotliminified
count-up.js 4.6 kB2.1 kB1.8 kB859 B

API reference

Props

AttributeTypeDefaultDescription
valuenumber0The number to arrive at.
fromnumber0Where the count starts the first time it is seen.
decimalsnumber0Fixed digits after the decimal mark, kept for every frame so the width stays put.
durationnumber1400Length of the count in milliseconds.
groupingtrue | falsetrueGroup thousands the way the locale does. On by default: grouping="false" turns it off.
langstring""Locale for the separators (default: the page's lang, then the browser's).