Navigation

v2.1.0
Data display

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>
Press Add visitors: the digits that change roll up to the new count.

#How it is written

The number is written twice, once for each reader:

  • odometer-value holds 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-figures draws it, with aria-hidden="true". It holds one empty odometer-digit per 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 (&nbsp;). 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>
Press Add the stand: the total and the tax roll up; press again and they roll back down.

#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-value and nothing else. Keep it equal to the figures, with its separators and unit, and change it in the same step as the digits.
  • odometer-figures must carry aria-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 the odometer root: 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

ClassDescription
odometerThe number, in the type of the place it stands in, with tabular figures. It has no surface and no size of its own
odometer-valueThe number as text, clipped out of sight: what assistive technology reads, find in page finds and a copy takes
odometer-figuresThe 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-digitOne 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

PropertyDefaultDescription
--digitThe 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-durationcalc(var(--duration-base) * 2)How long a digit takes to roll to its new numeral
--odometer-stagger24msHow much later each digit sets off than the one before it

#Source

@master/ui/components/odometer.css
/* * Odometer: a number whose digits roll to their new value, like the wheels of a mechanical counter. * It has no surface and no size of its own: it takes the type of the place it stands in, a heading, a stat's * figure or a line of text, and sets tabular figures so every digit is as wide as the next and nothing beside * the number moves while it rolls. * The number is written twice, once for each reader. odometer-value holds it as text, clipped out of sight, so * assistive technology reads one plain string, and find in page and a copy take the number; odometer-figures, * aria-hidden, draws it. In the figures each odometer-digit is a window one line tall onto a column of * numerals drawn by the stylesheet, standing at --digit. A change of --digit eases the column to the new * numeral, and a mask fades it out over and under the line, so numerals roll in and out of sight instead of * being cut. Anything else in the figures, a comma, a point, a currency or a unit, is plain text and stands still. * A wheel goes round: --digit may count on past 9 or below 0, and the window shows it modulo ten, so 9 to 10 * rolls on to 0 and 0 to -1 rolls back to 9, one step each, as a counter does. * Platform: the roll eases a registered number (@property) and the wheel reads it with mod(). Without * @property the numeral changes in place; without mod(), write --digit from 0 to 9. Where sibling-index() is * supported, each digit sets off a little after the one before it, so a change reads left to right; elsewhere * they roll together. Under reduced motion the numerals change in place. */@property --odometer-at {  syntax: '<number>';  inherits: true;  initial-value: 0;}@layer components.ui {  .odometer {    --odometer-duration: calc(var(--duration-base) * 2);    --odometer-stagger: 24ms;    position: relative;    font-variant-numeric: tabular-nums;    white-space: nowrap;  }  /* The number as text, for assistive technology, find in page and copying; clipped out of sight. */  .odometer-value {    position: absolute;    width: 1px;    height: 1px;    margin: -1px;    padding: 0;    overflow: hidden;    clip-path: inset(50%);    white-space: nowrap;  }  /*   * The drawing. A flex row, so the whitespace between digits in the markup never becomes a space in the   * number, and left to right in any direction of text, as numbers are written.   */  .odometer-figures {    display: inline-flex;    align-items: baseline;    direction: ltr;    unicode-bidi: isolate;    user-select: none;    -webkit-user-select: none;  }  /*   * One digit. A hidden zero gives the window a numeral's width and the line's baseline. The window is the   * line itself, reaching a hair over and under it, and gives that hair back with a negative margin, so the   * line keeps its height and a rolling numeral never crosses into the line above or below. The mask is clear   * across a numeral's own height and fades out by the window's edge, as the wheel turns away. The window   * clips the column too: a mask hides the other numerals but would leave them making the page scroll.   */  .odometer-digit {    --odometer-reach: .08em;    --odometer-at: var(--digit, 0);    position: relative;    display: block;    padding-block: var(--odometer-reach);    margin-block: calc(var(--odometer-reach) * -1);    overflow: clip;    mask-image: linear-gradient(transparent, black calc(50% - .38em), black calc(50% + .38em), transparent);    transition: --odometer-at var(--odometer-duration) var(--easing-glide);    @supports (width: calc(sibling-index() * 1px)) {      transition-delay: calc((sibling-index() - 1) * var(--odometer-stagger));    }    /*     * The column: 0 to 9 and 0 again, so a roll past 9 ends on a 0 that looks like the first. Numerals are     * spaced .4em further apart than lines of text, so the next one waits outside the box, and each stands in     * the middle of its line, as the line's own numeral does.     */    &::before {      content: '0\A 1\A 2\A 3\A 4\A 5\A 6\A 7\A 8\A 9\A 0';      content: '0\A 1\A 2\A 3\A 4\A 5\A 6\A 7\A 8\A 9\A 0' / '';      position: absolute;      inset: calc(var(--odometer-reach) - .2em) 0 auto;      line-height: calc(1lh + .4em);      text-align: center;      white-space: pre;      translate: 0 calc(var(--odometer-at) * -1lh);      @supports (width: calc(mod(3px, 2px))) {        translate: 0 calc(mod(var(--odometer-at), 10) * -1lh);      }    }    &::after {      content: '0';      content: '0' / '';      visibility: hidden;    }    @media (prefers-reduced-motion: reduce) {      transition: none;    }  }}