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
Submit your application before 11:59 PM on the closing day.
Variants
.nds-countdown is the root in every structure. In Banner, it sits in the card, between the text and the actions. Size and the card options need Cards or Banner. Layout and End action need Banner. A card option goes on every .nds-card. The rest of the card options are on Cards. The target date is sample content: set your own deadline.
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Structure | Banner (default) (hint: Text, countdown and a button in one card) | — | — | A full-width row card: an icon, a title and a line of text, the numbers with their words under them, and a button. All four parts sit on one line while the text keeps room to read. When it would not, the actions and then the countdown move to the next line, centered. Every card option applies |
| Structure | Inline (id: inline) | canon #countdown-inline |
— | The script writes the days with their word, then hh:mm:ss. The days hide under one day. Use it inside a sentence or a label |
| Structure | Own units (hint: Your own words and units) (id: units) | canon #countdown-units |
— | You write each unit and the words around it, in one sentence. At zero, an ended message replaces the whole sentence. The largest unit takes the time above it: hours alone show 2 days as 48 hours |
| Structure | Cards (hint: One card per unit) (id: cards) | canon #countdown-cards |
— | A row of statistic cards, one per unit, in equal columns, with 8px corners (--radius-md). Every card variant applies. A --card-radius on a card beats the corners |
| Target | Date (default) | [data-countdown="2027-12-31T23:59"] |
.nds-countdown |
Counts to a deadline. A date with no offset is read in the site timezone |
| Target | Duration (id: duration) | [data-countdown-seconds="63"] |
.nds-countdown |
Counts down a number of seconds from the moment the countdown starts, such as a one-time code |
| Target | Passed (hint: A deadline that is already over) (id: passed) (demo: + ended) | [data-countdown="2026-01-01T00:00"] |
.nds-countdown |
A deadline in the past: the countdown is at zero and in its ended state from page load, such as a page that stays online after applications close. The builder adds the ended message with it |
| Size | LG (not: inline, units) | .nds-lg |
.nds-countdown |
Numbers --typo-display-clamp-lg-FS: 32px on a phone, up to 48px on a wide screen |
| Size | MD (default) (not: inline, units) | — | .nds-countdown |
Numbers --typo-display-sm-FS, 30px. It needs no class |
| Size | SM (not: inline, units) | .nds-sm |
.nds-countdown |
Numbers --typo-display-xs-FS, 24px |
| Direction | Auto (default) | — | — | Follows the content, even inside a card number: digits and separators alone (05:12) read left to right, and Arabic unit words read right to left |
| Direction | RTL (hint: Right to left, whatever the page) | [dir="rtl"] |
.nds-countdown |
Fixes right to left: the first unit sits on the right |
| Direction | LTR (hint: Left to right, whatever the page) | [dir="ltr"] |
.nds-countdown |
Fixes left to right, such as a number left of its word on an Arabic page |
| Stroke | Stroke (default) | .nds-stroke |
.nds-card |
A 1px border on each card. Leave out both stroke and shadow for plain cards |
| Shadow | Shadow | .nds-shadow |
.nds-card |
An elevation shadow on each card. It combines with the stroke |
| Color | None (default) | — | — | The default colors |
| Color | Neutral | .nds-neutral |
.nds-card |
Tints the numbers. Also .nds-gray |
| Color | Green | .nds-green |
.nds-card |
Tints the numbers |
| Color | Yellow | .nds-yellow |
.nds-card |
Tints the numbers |
| Color | Red | .nds-red |
.nds-card |
Tints the numbers |
| Color | Blue | .nds-blue |
.nds-card |
Tints the numbers |
| Color | On color (hint: For a dark or photo background) | .nds-oncolor |
.nds-card |
For cards on a dark or photo background. It replaces the color classes, which do nothing on an on-color card. Inside an on-color area (data-theme="dark oncolor"), the cards already take this look: skip the class |
| Color | On color (hint: For a dark or photo background) | .nds-oncolor |
.nds-card-actions .nds-btn |
The button inside takes the on-color look from the card. The class on it is optional |
| Tinted | Tinted (hint: A light tint of the card color) | .nds-color |
.nds-card:not(.nds-oncolor) |
Fills each card with a light tint of its color. With no color class, the tint is the brand primary |
| Layout | Row (default) | .nds-horizontal |
.nds-card |
One row, which wraps as the Banner row says |
| Layout | Center (hint: Every part stacked and centered) (not: cards) | .nds-center |
.nds-card |
Stacks the icon, the text, the countdown and the button, each centered. Use it for a banner that stands alone, such as the top of a page |
| Warning | Warning (hint: Turns red near the end) (demo: + duration) | [data-countdown-warn="60"] |
.nds-countdown |
The countdown turns red when 60 seconds are left. The builder picks Duration with it, 63 seconds, so the red shows after 3 seconds |
| Ended | Ended message (hint: Text shown at zero) (id: ended) (demo: + passed) | canon #countdown-ended |
.nds-countdown |
The text replaces the countdown at zero. Without it, the zeros stay. The builder picks Passed with it, so the text shows at once |
| End action | Hide at end (hint: The actions hide at zero) (not: inline, units, cards) (demo: + passed) | [data-countdown-hide="countdown-apply"] |
.nds-countdown |
At zero, the elements whose ids it lists get hidden, wherever they are on the page. Here it is the actions row, whose button has no use after the deadline. The builder picks Passed with it, so the row is gone at once |
| End action | Hide at end (hint: The actions hide at zero) (not: inline, units, cards) (demo: + passed) | [id="countdown-apply"] |
.nds-card-actions |
The actions row carries the id that data-countdown-hide lists |
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-countdownon 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-labelgets the unit word in the language of the nearestlangattribute, 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-paddingon 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-nowfrom the server for a deadline that has legal weight, such as a submission window. - Add
data-countdown-hide-zeroto 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-liveregions. A screen reader that reads a new number every second is noise. Fire your own message fromnds:countdown:warnornds:countdown:endinstead.
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.
| Token | Preview | Value |
|---|---|---|
--text-errorRead 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 |
The full API is in the banner of _js/nds-countdown.js.