Odometer
A number whose digits roll to their new value, like the wheels of a mechanical counter.
On this page
An odometer is a number whose digits roll to their new value, like the wheels of a mechanical counter. Each digit is a window one line tall onto a column of numerals that the stylesheet draws; when the page changes a digit's --digit, its column rolls to the new numeral and fades in and out of sight over and under the line. It has no surface and no size of its own, so it can be a stat's figure, a heading or a word in a sentence. The number is also written once as plain text, which is what a screen reader reads.
Odometer
<div class="card w-full max-w(320)"> <div class="stat"> <p class="stat-label">Visitors today</p> <div class="stat-row"> <p class="stat-value"> <span class="odometer" id="visitors"> <span class="odometer-value">12,847</span> <span class="odometer-figures" aria-hidden="true"> <span class="odometer-digit" style="--digit: 1"></span> <span class="odometer-digit" style="--digit: 2"></span>, <span class="odometer-digit" style="--digit: 8"></span> <span class="odometer-digit" style="--digit: 4"></span> <span class="odometer-digit" style="--digit: 7"></span> </span> </span> </p> <p class="stat-delta stat-delta-up"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M13.6362 7.5189A0.4947 0.4947 0 0 0 13.5 7.5L2.4755 7.5006A0.5 0.5 0 0 0 2.4266 8.4946L2.5 8.5H13.5A0.4947 0.4947 0 0 0 13.6362 8.4811A0.7009 0.7009 0 0 0 13.6362 7.5189Z" fill="currentColor" stroke="none" fill-rule="evenodd" transform="rotate(-90 8 8)" /><path d="M8 4.5L13.2348 7.8313A0.2 0.2 0 0 1 13.2348 8.1687L8 11.5" transform="rotate(-90 8 8)" /> </svg>3.1% </p> </div> </div> <button class="btn btn-sm self-start" type="button" id="visitors-add">Add visitors</button></div><script>function roll(odometer, text, up) { odometer.querySelector('.odometer-value').textContent = text var numerals = text.split('').filter(function (character) { return character >= '0' && character <= '9' }) odometer.querySelectorAll('.odometer-digit').forEach(function (digit, index) { var at = Number(digit.style.getPropertyValue('--digit')) || 0 var step = ((numerals[index] - at) % 10 + 10) % 10 if (!up && step) step -= 10 digit.style.setProperty('--digit', at + step) })}var visitors = 12847document.getElementById('visitors-add').addEventListener('click', function () { visitors += 1 + Math.floor(Math.random() * 240) roll(document.getElementById('visitors'), visitors.toLocaleString('en-US'), true)})</script>#How it is written
The number is written twice, once for each reader:
odometer-valueholds it as text, clipped out of sight. A screen reader reads it as one plain string, find in page finds it and a copy takes it.odometer-figuresdraws it, witharia-hidden="true". It holds one emptyodometer-digitper digit, its numeral in--digit, and between them, as plain text, the marks that stand still: a comma, a point, a currency or a unit.
The figures are a flex row, so the line breaks and indentation between digits in the markup never turn into spaces in the number. A mark that is a space, such as the one before a unit, is a no-break space ( ). Digits are always laid out left to right, as numbers are written, even in a right-to-left page.
Each digit shows a hidden zero, so its window is as wide as a numeral and stands on the line's baseline. Figures are tabular, every one as wide as the next, so nothing beside the number moves while it rolls.
#Any type
The odometer sets no size, weight or ink. It takes them from the place it stands in, and the window, the spacing of the column and the fade are measured in that type, so it is the same mechanism in a heading, in a stat and in a line of text.
In a heading and a sentence
<h3 class="m(0) text-4xl font-semibold fg-text-strong"> <span class="odometer" id="total"> <span class="odometer-value">$1,280.50</span> <span class="odometer-figures" aria-hidden="true"> $<span class="odometer-digit" style="--digit: 1"></span>, <span class="odometer-digit" style="--digit: 2"></span> <span class="odometer-digit" style="--digit: 8"></span> <span class="odometer-digit" style="--digit: 0"></span>. <span class="odometer-digit" style="--digit: 5"></span> <span class="odometer-digit" style="--digit: 0"></span> </span> </span></h3><p class="m(0) text-sm fg-text-muted"> Includes <span class="odometer" id="tax"> <span class="odometer-value">$106.71</span> <span class="odometer-figures" aria-hidden="true"> $<span class="odometer-digit" style="--digit: 1"></span> <span class="odometer-digit" style="--digit: 0"></span> <span class="odometer-digit" style="--digit: 6"></span>. <span class="odometer-digit" style="--digit: 7"></span> <span class="odometer-digit" style="--digit: 1"></span> </span> </span> in tax.</p><button class="btn btn-sm mt-sm" type="button" id="stand" aria-pressed="false"> Add the stand</button><script>function roll(odometer, text, up) { odometer.querySelector('.odometer-value').textContent = text var numerals = text.split('').filter(function (character) { return character >= '0' && character <= '9' }) odometer.querySelectorAll('.odometer-digit').forEach(function (digit, index) { var at = Number(digit.style.getPropertyValue('--digit')) || 0 var step = ((numerals[index] - at) % 10 + 10) % 10 if (!up && step) step -= 10 digit.style.setProperty('--digit', at + step) })}var stand = document.getElementById('stand')stand.addEventListener('click', function () { var added = stand.getAttribute('aria-pressed') !== 'true' stand.setAttribute('aria-pressed', String(added)) stand.textContent = added ? 'Remove the stand' : 'Add the stand' roll(document.getElementById('total'), added ? '$1,329.50' : '$1,280.50', added) roll(document.getElementById('tax'), added ? '$110.79' : '$106.71', added)})</script>#Rolling the way the number moved
A column holds the numerals 0 to 9, and a digit rolls straight to the numeral it is given, up or down the column. To make every digit roll the way the number moved, as the wheels of a counter do, let --digit count on past 9 or below 0: the window shows it modulo ten. From 9, writing 10 rolls one step on to 0; from 0, writing -1 rolls one step back to 9.
The script in the previews above does this. For each digit it reads the value written last, and steps forward to the new numeral when the number went up, or back when it went down, never more than nine steps:
var at = Number(digit.style.getPropertyValue('--digit')) || 0var step = ((numeral - at) % 10 + 10) % 10if (!up && step) step -= 10digit.style.setProperty('--digit', at + step)Without a script the odometer is simply the number. A page that only renders it once never needs more than numerals from 0 to 9.
#Timing
A digit takes --odometer-duration, twice the theme's base duration, on the theme's one easing curve: it sets off quickly and settles gently, so the new numeral is legible early. Each digit sets off --odometer-stagger after the one before it, so a change reads from left to right. The window's fade is clear across a numeral's own height and the next numeral waits outside it, so at rest only the number is seen.
#Accessibility
- A screen reader reads
odometer-valueand nothing else. Keep it equal to the figures, with its separators and unit, and change it in the same step as the digits. odometer-figuresmust carryaria-hidden="true". The numerals of the column are generated content with empty alternative text, but the marks between digits are real text and would be read a second time.- The odometer is not a live region. When the reader needs to hear a new value, such as the total after their own choice, add
aria-live="polite"to theodometerroot: the clipped value is its only text, so the new number is announced once. Never do this for a figure that changes by itself every few seconds. - Under reduced motion the numerals change in place, with no roll. In forced colors the numerals take the system's text color.
#Platform
The roll eases a registered custom property (@property), and the window reads it modulo ten with mod(). In a browser without @property the numerals change in place; without mod(), keep --digit between 0 and 9. The delay from one digit to the next uses sibling-index(), where it is supported (Chromium); elsewhere the digits roll together.
#Classes
| Class | Description |
|---|---|
odometer | The number, in the type of the place it stands in, with tabular figures. It has no surface and no size of its own |
odometer-value | The number as text, clipped out of sight: what assistive technology reads, find in page finds and a copy takes |
odometer-figures | The number as drawn, hidden from assistive technology: digits and, as plain text between them, the marks that stand still, such as a comma, a point, a currency or a unit |
odometer-digit | One digit: an empty element, a window one line tall onto a column of numerals that stands at --digit and rolls when it changes |
#Custom properties
| Property | Default | Description |
|---|---|---|
--digit | The numeral, from 0 to 9. A value past 9 or below 0 is shown modulo ten, so counting on from 9 to 10 rolls one step to 0. The stylesheet reads 0 when it is not setSet on odometer-digit | |
--odometer-duration | calc(var(--duration-base) * 2) | How long a digit takes to roll to its new numeral |
--odometer-stagger | 24ms | How much later each digit sets off than the one before it |