Skip to main content
Login

Sign in to access your account

Countdown - National Design System

A live count of the time left until a deadline or the end of a duration, as text or as a row of cards

Overview

A countdown shows the time left until a deadline, such as the day applications close, or until a duration ends, such as a session or a one-time code. You write the units and their boxes in the markup. The script writes the numbers, and the unit word into an empty label. A <div> root lays its units out in one row: each number over its word in one banner card, or a Card for each unit. A <span> root is a run of text.

Pick another component when:

  • a button must wait before it can be pressed again: Cooldown Button
  • the page shows the current date and time: Top Bar

Markup

Options
Structure
Target
Size
Direction
Color
Layout
More
Preview
Applications close soon

Submit your application before 11:59 PM on the closing day.

--
--
--
--
<div class="nds-card nds-stroke nds-horizontal"> <div class="nds-card-header"> <div class="nds-card-featured-icon"> <span class="nds-featured-icon nds-circle"> <i class="hgi hgi-stroke hgi-flash"></i> </span> </div> </div> <div class="nds-card-content"> <div class="nds-card-text"> <span class="nds-card-title">Applications close soon</span> <p class="nds-card-description">Submit your application before 11:59 PM on the closing day.</p> </div> </div> <div class="nds-countdown" data-countdown="2027-12-31T23:59"> <div data-unit="d"> <span class="nds-countdown-value">--</span> <span class="nds-countdown-label"></span> </div> <div data-unit="h"> <span class="nds-countdown-value">--</span> <span class="nds-countdown-label"></span> </div> <div data-unit="m"> <span class="nds-countdown-value">--</span> <span class="nds-countdown-label"></span> </div> <div data-unit="s"> <span class="nds-countdown-value">--</span> <span class="nds-countdown-label"></span> </div> </div> <div class="nds-card-actions"> <a href="#" class="nds-btn nds-primary"> <span class="nds-label">Apply now</span> </a> </div> </div>

Behavior

Deadline

data-countdown takes a date and time, such as 2027-12-31T23:59. A value with no offset is read in the site timezone, the one <html data-timezone> sets. A value with an offset, such as +03:00 or Z, is read as written. Either way, every visitor counts to the same moment, wherever they are.

Duration

data-countdown-seconds counts down from the moment the countdown starts. A reload starts it again. To resume a timer after a reload, render the seconds left on the server, or call NDS.Countdown.setTarget() with them.

Units

Each [data-unit] element shows one unit: d days, h hours, m minutes or s seconds. Only the units you write show. Hours, minutes and seconds always show two digits, such as 05. Days show as many digits as they need. The largest unit takes the time above it, so hours and minutes alone show 2 days as 48 hours. A unit you skip in the middle carries into the next smaller unit you wrote.

Warning

With data-countdown-warn, the root gets data-state="warning" when that many seconds are left, and the numbers turn red. Text written directly in the root turns red too. The nds:countdown:warn event fires once at that moment.

Ended Message

At zero, the root gets data-state="ended". A .nds-countdown-ended element anywhere inside the root then becomes its only content: the units, your words and the separators leave the page. set() puts them back.

Linked Elements

data-countdown-hide on the root lists element ids, separated by spaces or commas. At zero, the script hides those elements, such as the actions row of an offer that ends with the deadline. data-countdown-show lists elements to show at zero instead, such as a closed message outside the countdown. The elements can be anywhere on the page. set() puts both back.

Built-in Features

  • Auto-initialization

    Every .nds-countdown on the page starts at load, and any added later. No init call is needed.

  • No Drift

    One timer serves every countdown on the page, aligned to the second. Each tick computes the time left from the target, so a slow or hidden tab shows the right time when the visitor returns.

  • Unit Words

    An empty .nds-countdown-label gets the unit word in the language of the nearest lang attribute, with the right plural for the number. The day word of the default inline countdown comes from the same place. A label with text stays as you wrote it.

  • Server Time

    Put the server's current time in data-countdown-now, on the root or on <html> for the whole page. A wrong clock on the visitor's device then cannot move a deadline.

  • Stable Width

    Every digit takes the same width. A unit word keeps the width of its longest form, such as "seconds" when it shows "second". The largest number keeps the width it starts with, such as 10 days at 9. The text and the cards do not move as the time changes.

  • Time Direction

    A countdown of digits alone, such as 05:12, reads left to right on every page, Arabic included. Arabic unit words make the countdown read right to left.

  • Phone Row

    On a phone, the cards in a row get a 12px padding and 12px unit words, so four cards fit a 320px screen. A --card-padding on a card beats the padding.

Best Practices

  • Write -- in each .nds-countdown-value. The placeholder shows until the script loads, so the layout does not move.
  • Show seconds only when they matter, such as in the last hour or for a one-time code. A deadline weeks away reads better in days and hours.
  • Put the deadline itself in the text near the countdown, such as “Applications close on 31 December”. The countdown says how long is left, not when.
  • Add an ended message to every countdown on a live page. A row of zeros does not tell the visitor what happened.
  • Hide an action that ends with the deadline, such as Apply now: list its id in data-countdown-hide. After zero, the button leads nowhere.
  • Set data-countdown-now from the server for a deadline that has legal weight, such as a submission window.
  • Add data-countdown-hide-zero to the days unit of a countdown that is usually under one day, such as a sale that ends tonight. The row then shows no days at 0.
  • Keep the countdown out of aria-live regions. A screen reader that reads a new number every second is noise. Fire your own message from nds:countdown:warn or nds:countdown:end instead.

API

Other Classes

Class Element Effect
.nds-countdown-value a [data-unit], or an element inside one The script writes the number here. A [data-unit] with no .nds-countdown-value gets the number itself
.nds-countdown-label an element inside a [data-unit] When empty at load, the script writes the unit word here every second

Data Attributes

Attribute Element Effect
data-countdown .nds-countdown The deadline: YYYY-MM-DD, with an optional THH:mm or THH:mm:ss and an optional offset. No offset means the site timezone. A value that is not a date logs a console warning. Empty, the countdown shows its placeholders and waits for NDS.Countdown.setTarget(): no timer and no events until then
data-countdown-seconds .nds-countdown A duration in seconds, counted from the moment the countdown starts. It wins over data-countdown
data-countdown-warn .nds-countdown Seconds left at which the script adds warning to data-state
data-countdown-now .nds-countdown, or any element around it, such as <html> The server’s current time, in the format of data-countdown. The script reads it once, when the countdown starts
data-countdown-hide .nds-countdown Element ids, separated by spaces or commas. The script adds hidden to each at zero, and removes it when the countdown starts and on set(). An id with no element logs a console warning
data-countdown-show .nds-countdown Element ids, separated by spaces or commas. The script removes hidden from each at zero, and adds it when the countdown starts and on set(). Write hidden on them yourself too, so they stay hidden before the script runs. An id with no element logs a console warning
dir .nds-countdown Fixes the direction, such as dir="ltr" to keep the number left of the word on an Arabic page. Without it, the script sets dir="auto": digits and separators alone read left to right, and Arabic unit words read right to left. A CSS direction on the root works too
data-state="warning" .nds-countdown The script adds it when the time left reaches data-countdown-warn. It removes it at zero and on set()
data-state="ended" .nds-countdown The script adds it at zero. It removes it on set()
data-unit an element inside .nds-countdown One unit: d, h, m or s. A root with no [data-unit] gets the default units from the script
data-countdown-hide-zero a [data-unit] element The script adds hidden to the unit while its value is 0, and removes it when the value is above 0
data-countdown-sizes an empty .nds-countdown-label The script writes every form of the unit word into it, one per line, such as seconds and second. The CSS reserves the widest, so the label never resizes its card. Do not set it yourself
data-countdown-sizes the value of the largest unit The script writes the starting value into it, such as 10 days. The CSS reserves that width, so the value keeps it when it loses a digit. Do not set it yourself

CSS Custom Properties

Set them on the root of Cards or Banner. Each one beats the size class.

Property Default Controls
--countdown-gap var(--spacing-md), var(--spacing-3xl) in Banner The space between the units
--countdown-value-size var(--typo-display-sm-FS) The font size of the numbers
--countdown-value-line-height var(--typo-display-sm-LH) The line height of the numbers

Tokens

It reads these shared tokens. A change to one also changes other components: see Tokens.

Source: the var() reads in _sass/components/_countdown.scss.

TokenPreviewValue
--text-error
Read by color

var(--colors-red-700)
Dark mode var(--colors-red-400)

JavaScript

The NDS.Countdown script loads after the page shows. Until it loads, each call loads it and returns a promise of the result, so await a call whose result you use.

Method Effect
NDS.Countdown.init() Starts every .nds-countdown on the page that has not started yet. Runs at load
NDS.Countdown.setTarget(el, target) Starts the countdown again with a new target: a date string, a Date, or a number of seconds from now

All events bubble.

Event Fired on Detail
nds:countdown:tick .nds-countdown, every second { remaining }: the seconds left
nds:countdown:warn .nds-countdown, once, when the time left reaches data-countdown-warn { remaining }: the seconds left
nds:countdown:end .nds-countdown, once, at zero None
const timer = document.querySelector('#otp-timer'); // Let the user ask for a new code when the old one expires. timer.addEventListener('nds:countdown:end', () => { document.querySelector('#resend-btn').disabled = false; }); // A new code was sent: count down its 5 minutes again. NDS.Countdown.setTarget(timer, 300);

The full API is in the banner of _js/nds-countdown.js.

Last Modified Date: 20/11/2026 - 05:15 PM
Was this page useful?
60% of users said Yes from 2843 Feedbacks