Navigation

v2.1.0
Data display

Countdown

The time left until something, in days, hours, minutes and seconds.

On this page

A countdown is the time left until something: a launch, the end of a sale, the start of an event. Each unit is a figure over a small quiet label, in equal columns, with a colon in the middle of each gap. The root is a <time> whose datetime holds the same time left as a duration, so the figures are a time for the platform as well as for the eye. Without a script it is the time left when the page was made; a few lines of script make it tick.

Countdown

<time class="countdown" id="launch" datetime="P2DT14H5M9S">  <span class="countdown-unit">    <span class="countdown-value">02</span>    <span class="countdown-label">days</span>  </span>  <span class="countdown-unit">    <span class="countdown-value">14</span>    <span class="countdown-label">hours</span>  </span>  <span class="countdown-unit">    <span class="countdown-value">05</span>    <span class="countdown-label">minutes</span>  </span>  <span class="countdown-unit">    <span class="countdown-value">09</span>    <span class="countdown-label">seconds</span>  </span></time><script>var launch = document.getElementById('launch')var figures = launch.querySelectorAll('.countdown-value')var start = Array.prototype.map.call(figures, function (figure) { return Number(figure.textContent) })var end = Date.now() + (((start[0] * 24 + start[1]) * 60 + start[2]) * 60 + start[3]) * 1000function tick() {  var left = Math.max(0, Math.ceil((end - Date.now()) / 1000))  var units = [Math.floor(left / 86400), Math.floor(left / 3600) % 24, Math.floor(left / 60) % 60, left % 60]  units.forEach(function (unit, index) { figures[index].textContent = String(unit).padStart(2, '0') })  launch.setAttribute('datetime', left ? 'P' + units[0] + 'DT' + units[1] + 'H' + units[2] + 'M' + units[3] + 'S' : 'PT0S')  if (left) setTimeout(tick, (end - Date.now()) % 1000 || 1000)}tick()</script>
The script ticks it once a second.

#Ticking

The countdown is markup: the figures and the datetime say the same thing, and the stylesheet draws them. A script that ticks it finds the end once, from the time left when the page loaded, and then once a second writes each figure and the datetime again. It sets the next tick for the moment the next second begins, so the figures do not drift from the clock. When it reaches zero it writes datetime="PT0S" and stops.

A page that knows the end as a date and time, such as a sale that closes at midnight in one time zone, should count down to that instant instead of to the time left at load, so the figures agree on every device.

#Tiles

countdown-tiles sets each figure in a small raised sheet, twice as tall as the figure, with the large control's corner and the label under it. The tiles stand close together and need no colons. Use it where the countdown is the point of the view, such as the page before a launch.

Tiles

<time class="countdown countdown-tiles" datetime="P2DT14H5M9S">  <span class="countdown-unit">    <span class="countdown-value">02</span>    <span class="countdown-label">days</span>  </span>  <span class="countdown-unit">    <span class="countdown-value">14</span>    <span class="countdown-label">hours</span>  </span>  <span class="countdown-unit">    <span class="countdown-value">05</span>    <span class="countdown-label">minutes</span>  </span>  <span class="countdown-unit">    <span class="countdown-value">09</span>    <span class="countdown-label">seconds</span>  </span></time>
<time className="countdown countdown-tiles" dateTime="P2DT14H5M9S">  <span className="countdown-unit">    <span className="countdown-value">02</span>    <span className="countdown-label">days</span>  </span>  <span className="countdown-unit">    <span className="countdown-value">14</span>    <span className="countdown-label">hours</span>  </span>  <span className="countdown-unit">    <span className="countdown-value">05</span>    <span className="countdown-label">minutes</span>  </span>  <span className="countdown-unit">    <span className="countdown-value">09</span>    <span className="countdown-label">seconds</span>  </span></time>
<time class="countdown countdown-tiles" datetime="P2DT14H5M9S">  <span class="countdown-unit">    <span class="countdown-value">02</span>    <span class="countdown-label">days</span>  </span>  <span class="countdown-unit">    <span class="countdown-value">14</span>    <span class="countdown-label">hours</span>  </span>  <span class="countdown-unit">    <span class="countdown-value">05</span>    <span class="countdown-label">minutes</span>  </span>  <span class="countdown-unit">    <span class="countdown-value">09</span>    <span class="countdown-label">seconds</span>  </span></time>
<template>  <time class="countdown countdown-tiles" datetime="P2DT14H5M9S">    <span class="countdown-unit">      <span class="countdown-value">02</span>      <span class="countdown-label">days</span>    </span>    <span class="countdown-unit">      <span class="countdown-value">14</span>      <span class="countdown-label">hours</span>    </span>    <span class="countdown-unit">      <span class="countdown-value">05</span>      <span class="countdown-label">minutes</span>    </span>    <span class="countdown-unit">      <span class="countdown-value">09</span>      <span class="countdown-label">seconds</span>    </span>  </time></template>

#Rolling figures

A figure may be an odometer, so its digits roll as they change instead of being replaced. The countdown draws the unit and the odometer draws the digits; neither knows about the other. A countdown's digits go down, so the script steps each one back to its new numeral, and from 0 one step back is -1, which the odometer shows as 9: the seconds roll down past zero as a counter's wheel does.

Rolling figures

<time class="countdown countdown-tiles" id="doors" datetime="PT4H12M30S">  <span class="countdown-unit">    <span class="countdown-value">      <span class="odometer">        <span class="odometer-value">04</span><span class="odometer-figures" aria-hidden="true">          <span class="odometer-digit" style="--digit: 0"></span><span class="odometer-digit" style="--digit: 4"></span>        </span>      </span>    </span>    <span class="countdown-label">hours</span>  </span>  <span class="countdown-unit">    <span class="countdown-value">      <span class="odometer">        <span class="odometer-value">12</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>      </span>    </span>    <span class="countdown-label">minutes</span>  </span>  <span class="countdown-unit">    <span class="countdown-value">      <span class="odometer">        <span class="odometer-value">30</span><span class="odometer-figures" aria-hidden="true">          <span class="odometer-digit" style="--digit: 3"></span><span class="odometer-digit" style="--digit: 0"></span>        </span>      </span>    </span>    <span class="countdown-label">seconds</span>  </span></time><script>var doors = document.getElementById('doors')var figures = doors.querySelectorAll('.odometer')var start = Array.prototype.map.call(figures, function (figure) { return Number(figure.querySelector('.odometer-value').textContent) })var end = Date.now() + ((start[0] * 60 + start[1]) * 60 + start[2]) * 1000function roll(digit, numeral) {  var at = Number(digit.style.getPropertyValue('--digit')) || 0  var step = ((numeral - at) % 10 + 10) % 10  if (step) digit.style.setProperty('--digit', at + step - 10)}function tick() {  var left = Math.max(0, Math.ceil((end - Date.now()) / 1000))  var units = [Math.floor(left / 3600), Math.floor(left / 60) % 60, left % 60]  units.forEach(function (unit, index) {    var digits = figures[index].querySelectorAll('.odometer-digit')    figures[index].querySelector('.odometer-value').textContent = String(unit).padStart(2, '0')    roll(digits[0], Math.floor(unit / 10))    roll(digits[1], unit % 10)  })  doors.setAttribute('datetime', left ? 'PT' + units[0] + 'H' + units[1] + 'M' + units[2] + 'S' : 'PT0S')  if (left) setTimeout(tick, (end - Date.now()) % 1000 || 1000)}tick()</script>
The seconds roll down once a second; the minutes when a minute ends.

#Inline

countdown-inline sets the units on one baseline, each label after its figure, at the size of the sentence around it, for a deadline mentioned in passing. The figures take the strong ink and the labels stay quiet, so the time stands out of the sentence without a weight of its own. A short label such as h is read by a screen reader as the letter, so hide it from assistive technology and give the word beside it, clipped out of sight with sr-only.

Inline

<p class="m(0) text-sm fg-text-body">  Early pricing ends in <time class="countdown countdown-inline" datetime="P2DT14H5M">    <span class="countdown-unit">      <span class="countdown-value">2</span><span class="countdown-label" aria-hidden="true">d</span><span class="sr-only">days</span>    </span>    <span class="countdown-unit">      <span class="countdown-value">14</span><span class="countdown-label" aria-hidden="true">h</span><span class="sr-only">hours</span>    </span>    <span class="countdown-unit">      <span class="countdown-value">05</span><span class="countdown-label" aria-hidden="true">m</span><span class="sr-only">minutes</span>    </span>  </time>.</p>
<p className="m(0) text-sm fg-text-body">  Early pricing ends in <time className="countdown countdown-inline" dateTime="P2DT14H5M">    <span className="countdown-unit">      <span className="countdown-value">2</span><span className="countdown-label" aria-hidden="true">d</span><span className="sr-only">days</span>    </span>    <span className="countdown-unit">      <span className="countdown-value">14</span><span className="countdown-label" aria-hidden="true">h</span><span className="sr-only">hours</span>    </span>    <span className="countdown-unit">      <span className="countdown-value">05</span><span className="countdown-label" aria-hidden="true">m</span><span className="sr-only">minutes</span>    </span>  </time>.</p>
<p class="m(0) text-sm fg-text-body">  Early pricing ends in <time class="countdown countdown-inline" datetime="P2DT14H5M">    <span class="countdown-unit">      <span class="countdown-value">2</span><span class="countdown-label" aria-hidden="true">d</span><span class="sr-only">days</span>    </span>    <span class="countdown-unit">      <span class="countdown-value">14</span><span class="countdown-label" aria-hidden="true">h</span><span class="sr-only">hours</span>    </span>    <span class="countdown-unit">      <span class="countdown-value">05</span><span class="countdown-label" aria-hidden="true">m</span><span class="sr-only">minutes</span>    </span>  </time>.</p>
<template>  <p class="m(0) text-sm fg-text-body">    Early pricing ends in <time class="countdown countdown-inline" datetime="P2DT14H5M">      <span class="countdown-unit">        <span class="countdown-value">2</span><span class="countdown-label" aria-hidden="true">d</span><span class="sr-only">days</span>      </span>      <span class="countdown-unit">        <span class="countdown-value">14</span><span class="countdown-label" aria-hidden="true">h</span><span class="sr-only">hours</span>      </span>      <span class="countdown-unit">        <span class="countdown-value">05</span><span class="countdown-label" aria-hidden="true">m</span><span class="sr-only">minutes</span>      </span>    </time>.  </p></template>

#When time is up

When the time left is zero, datetime="PT0S", the figures turn quiet and tiles sink to a flat well: there is nothing left to count. Say what happened next to it in words, such as "Doors are open", and replace the countdown when the page has something better to show.

Time is up

<time class="countdown countdown-tiles" datetime="PT0S">  <span class="countdown-unit">    <span class="countdown-value">00</span>    <span class="countdown-label">hours</span>  </span>  <span class="countdown-unit">    <span class="countdown-value">00</span>    <span class="countdown-label">minutes</span>  </span>  <span class="countdown-unit">    <span class="countdown-value">00</span>    <span class="countdown-label">seconds</span>  </span></time>
<time className="countdown countdown-tiles" dateTime="PT0S">  <span className="countdown-unit">    <span className="countdown-value">00</span>    <span className="countdown-label">hours</span>  </span>  <span className="countdown-unit">    <span className="countdown-value">00</span>    <span className="countdown-label">minutes</span>  </span>  <span className="countdown-unit">    <span className="countdown-value">00</span>    <span className="countdown-label">seconds</span>  </span></time>
<time class="countdown countdown-tiles" datetime="PT0S">  <span class="countdown-unit">    <span class="countdown-value">00</span>    <span class="countdown-label">hours</span>  </span>  <span class="countdown-unit">    <span class="countdown-value">00</span>    <span class="countdown-label">minutes</span>  </span>  <span class="countdown-unit">    <span class="countdown-value">00</span>    <span class="countdown-label">seconds</span>  </span></time>
<template>  <time class="countdown countdown-tiles" datetime="PT0S">    <span class="countdown-unit">      <span class="countdown-value">00</span>      <span class="countdown-label">hours</span>    </span>    <span class="countdown-unit">      <span class="countdown-value">00</span>      <span class="countdown-label">minutes</span>    </span>    <span class="countdown-unit">      <span class="countdown-value">00</span>      <span class="countdown-label">seconds</span>    </span>  </time></template>

#Accessibility

  • A countdown is never a live region. Announcing a figure that changes every second would talk over everything else the reader is doing, and nothing a reader needs is in any single second. A screen reader reads the countdown when the reader comes to it: the figures and their labels, such as "02 days 14 hours 05 minutes 09 seconds".
  • When the moment itself matters, such as a reservation that is about to expire, announce it once, in words, in a separate role="status" region: "Your seats are held for one more minute."
  • When the countdown is a time limit on the reader's own task, such as seats held while they pay, the server sets the limit, not the countdown. Warn at least 20 seconds before it ends and offer to extend it, and keep what the reader has entered if it does end.
  • The colons are drawn by the stylesheet with empty alternative text, so they are not read.
  • Write the labels as words. Where a label is a letter, as in the inline countdown, hide the letter and give the word with sr-only.
  • Under reduced motion an odometer inside the countdown changes its numerals in place. In forced colors the tiles keep their edge as a system-color outline.

#Classes

ClassDescription
countdownThe units in equal columns, as wide as the widest, with a colon in the middle of each gap. On a <time> whose datetime is the time left as a duration
countdown-tilesEach figure in a small raised sheet, 2em square, with the label under it and no colonsApply with countdown
countdown-inlineThe units on one baseline, each label after its figure, at the size of the sentence around it: the figures in strong ink, the labels quietApply with countdown
countdown-unitOne unit: its figure and its label
countdown-valueThe figure, in tabular numerals. It may hold an odometer
countdown-labelWhat the unit counts, small and quiet: days, hours, minutes, seconds

#Custom properties

PropertyDefaultDescription
--countdown-size1.75remSize of the figures; tiles are twice as tall. The inline countdown takes the size of its sentence instead
--countdown-gap1.25remSpace between units, where the colons stand

#Source

@master/ui/components/countdown.css
/* * Countdown: the time left until something, in days, hours, minutes and seconds. * The root is a <time> whose datetime holds the time left as a duration (P2DT14H5M9S), so the figures are a * time for the platform as well as for the eye. Each unit is a figure over a small quiet label. The units take * equal columns, as wide as the widest, so the figures stand evenly apart whatever their labels say, and a * colon drawn by the stylesheet stands in the middle of each gap, level with the figures and hidden from * assistive technology. Figures are tabular, so a ticking value never moves its neighbors. * countdown-tiles sets each figure in a small raised sheet, 2em square with the large control's corner, and * drops the colons; countdown-inline sets the units on one baseline at the size of the sentence around it. * When the time is up, datetime="PT0S", the figures turn quiet and the tiles sink to a flat well. * Without a script a countdown is the time left when the page was made; a script may tick it. A figure may * be an odometer, so a changing digit rolls. */@layer components.ui {  .countdown {    --countdown-size: 1.75rem;    --countdown-gap: 1.25rem;    display: inline-grid;    grid-auto-columns: 1fr;    grid-auto-flow: column;    column-gap: var(--countdown-gap);    /* As wide as its units, even where the parent would stretch it, so the columns never spread apart. */    width: fit-content;    margin: 0;    color: var(--color-text-strong);    font-variant-numeric: tabular-nums;    vertical-align: middle;    /* Tiles: each figure in a small raised sheet, closer together and without colons. */    &.countdown-tiles {      --countdown-gap: .625rem;      .countdown-unit::before {        display: none;      }      .countdown-value {        display: grid;        place-items: center;        box-sizing: border-box;        min-width: 2em;        height: 2em;        padding: 0 .3125em;        border-radius: calc(var(--radius-control-lg) * var(--corner-scale));        corner-shape: var(--corner-shape);        @apply --surface-sheet;        box-shadow: var(--shadow-raised);        line-height: 1.15;        overflow: clip;        @media (forced-colors: active) {          outline: 1px solid CanvasText;          outline-offset: -1px;        }      }    }    /* Inline: the units on one baseline at the size of the sentence they stand in, the figures in strong ink. */    &.countdown-inline {      --countdown-gap: .375em;      display: inline-flex;      align-items: baseline;      color: inherit;      vertical-align: baseline;      white-space: nowrap;      .countdown-unit {        display: inline-flex;        align-items: baseline;        gap: .125em;      }      .countdown-unit::before {        display: none;      }      .countdown-value {        font: inherit;        font-variant-numeric: tabular-nums;        letter-spacing: inherit;      }      .countdown-label {        color: var(--color-text-muted);        font: inherit;        font-size: .875em;      }    }    /* Time is up: the figures are quiet and the tiles have sunk to a flat well. */    &[datetime="PT0S"] {      .countdown-value {        color: var(--color-text-muted);      }      &.countdown-tiles .countdown-value {        background: var(--color-surface-inset);        box-shadow: var(--shadow-flat);      }      /* Forced colors would make the quiet figures the text color again: they take the system's disabled gray. */      @media (forced-colors: active) {        .countdown-value {          color: GrayText;        }        &.countdown-tiles .countdown-value {          outline-color: GrayText;        }      }    }  }  /* One unit: its figure over its label. */  .countdown-unit {    position: relative;    display: grid;    justify-items: center;    align-content: start;    gap: .375rem;    min-width: 0;    /* The colon in the gap before every unit but the first, level with the figures. */    & + .countdown-unit::before {      content: ':' / '';      position: absolute;      top: 0;      inset-inline-start: calc(var(--countdown-gap) / -2);      translate: -50% -.09em;      color: var(--color-text-muted);      font: var(--font-weight-control) var(--countdown-size)/1.15 var(--font-family-sans);      font-feature-settings: inherit;    }    /* Right to left the gap is on the right, so the colon is pulled back toward it the other way. */    &:dir(rtl) + .countdown-unit::before {      translate: 50% -.09em;    }  }  /* The figure. */  .countdown-value {    margin: 0;    color: var(--color-text-strong);    font: var(--font-weight-control) var(--countdown-size)/1.15 var(--font-family-sans);    font-feature-settings: inherit;    font-variant-numeric: tabular-nums;    letter-spacing: -.02em;    white-space: nowrap;  }  /* What it counts: days, hours, minutes, seconds. */  .countdown-label {    margin: 0;    color: var(--color-text-muted);    font: 400 .75rem/1 var(--font-family-sans);    font-feature-settings: inherit;    letter-spacing: 0;    white-space: nowrap;  }}