Component Library

← Back to gallery

Countdown timer closing state in HTML, CSS and vanilla JS

A copy-paste Countdown timer closing state in plain HTML and CSS, with vanilla JavaScript: no framework, no build step, MIT-licensed.

Category: Data display · Tags: countdown, data-display, responsive

Live preview

Source (copy-ready)

<div class="dcl-countdown-timer-closing-state" data-dcl-scheme="light">
<div data-demo-seconds="473"><header><p class="dcl-countdown-timer-closing-state-eyebrow">ACCESS WINDOW</p><h3>Request intake</h3></header><div class="dcl-countdown-timer-closing-state-row"><strong class="dcl-countdown-timer-closing-state-badge dcl-countdown-timer-closing-state-accent"><span data-part="phase">Open</span></strong><p>About <strong class="dcl-countdown-timer-closing-state-value"><span data-part="minuteTotal">—</span></strong> min left</p></div><p class="dcl-countdown-timer-closing-state-muted" role="status"></p></div>
<script>
(() => {
  const root = document.currentScript.closest(".dcl-countdown-timer-closing-state");
  const preference = matchMedia("(prefers-color-scheme: dark)");
  function setScheme() { root.dataset.dclScheme = preference.matches ? "dark" : "light"; }
  preference.addEventListener("change", setScheme);
  setScheme();

  const panel = root.querySelector("[data-demo-seconds], [data-deadline]");
  const status = panel.querySelector('[role="status"]');
  const demoSeconds = Number(panel.dataset.demoSeconds);
  const end = panel.dataset.deadline ? Date.parse(panel.dataset.deadline) : Date.now() + demoSeconds * 1000;
  const pad = (n) => String(n).padStart(2, "0");
  const parts = [...panel.querySelectorAll("[data-part]")];
  const date = panel.querySelector("time");
  if (!Number.isFinite(end)) {
    status.textContent = "Deadline unavailable.";
    return;
  }
  if (date) {
    date.dateTime = new Date(end).toISOString();
    date.textContent = new Intl.DateTimeFormat(undefined, {dateStyle: "medium", timeStyle: "short"}).format(end);
  }
  function tick() {
    const left = Math.max(0, Math.ceil((end - Date.now()) / 1000));
    const days = Math.floor(left / 86400);
    const totalHours = Math.floor(left / 3600);
    const totalMinutes = Math.floor(left / 60);
    const minutes = totalMinutes - totalHours * 60;
    const seconds = left - totalMinutes * 60;
    const values = {
      days, hours: totalHours - days * 24, totalHours, minutes: pad(minutes),
      seconds: pad(seconds), compact: pad(totalMinutes) + ":" + pad(seconds),
      phase: left === 0 ? "Closed" : left <= 600 ? "Closes soon" : "Open",
      minuteTotal: Math.ceil(left / 60),
    };
    for (const part of parts) {
      const text = String(values[part.dataset.part]);
      if (part.textContent !== text) part.textContent = text;
    }
    if (left === 0) status.textContent = "Request intake has closed.";
    else setTimeout(tick, 250);
  }
  tick();
})();
</script>
</div>
<style>
.dcl-countdown-timer-closing-state, .dcl-countdown-timer-closing-state * { box-sizing: border-box; }
.dcl-countdown-timer-closing-state { max-inline-size: 44rem; margin-inline: auto; padding: clamp(.875rem, 4vw, 1.5rem); border: 1px solid; border-radius: .75rem; font: 1rem/1.5 system-ui, sans-serif; overflow-wrap: anywhere; }
.dcl-countdown-timer-closing-state h3, .dcl-countdown-timer-closing-state h4, .dcl-countdown-timer-closing-state p, .dcl-countdown-timer-closing-state figure, .dcl-countdown-timer-closing-state dl, .dcl-countdown-timer-closing-state ul, .dcl-countdown-timer-closing-state ol { margin: 0; }
.dcl-countdown-timer-closing-state h3 { font-size: 1.18rem; line-height: 1.25; }
.dcl-countdown-timer-closing-state header { margin-block-end: .875rem; }
.dcl-countdown-timer-closing-state .dcl-countdown-timer-closing-state-eyebrow { font-size: .8125rem; font-weight: 700; letter-spacing: .06em; }
.dcl-countdown-timer-closing-state .dcl-countdown-timer-closing-state-well { padding: .7rem .8rem; border: 1px solid; border-radius: .55rem; }
.dcl-countdown-timer-closing-state .dcl-countdown-timer-closing-state-muted { font-size: .875rem; }
.dcl-countdown-timer-closing-state .dcl-countdown-timer-closing-state-row { display: flex; gap: .55rem .8rem; align-items: center; flex-wrap: wrap; }
.dcl-countdown-timer-closing-state .dcl-countdown-timer-closing-state-grid { display: grid; gap: .65rem; grid-template-columns: repeat(auto-fit, minmax(min(100%, 11rem), 1fr)); }
.dcl-countdown-timer-closing-state dt { font-size: .875rem; font-weight: 650; }
.dcl-countdown-timer-closing-state dd { margin: 0; }
.dcl-countdown-timer-closing-state .dcl-countdown-timer-closing-state-value { font-size: clamp(1.35rem, 5vw, 1.9rem); font-weight: 750; font-variant-numeric: tabular-nums; line-height: 1.2; }
.dcl-countdown-timer-closing-state button, .dcl-countdown-timer-closing-state summary, .dcl-countdown-timer-closing-state input { font: inherit; }
.dcl-countdown-timer-closing-state button, .dcl-countdown-timer-closing-state summary { min-height: 2.75rem; cursor: pointer; }
.dcl-countdown-timer-closing-state button { padding: .35rem .7rem; border: 1px solid; border-radius: .4rem; }
.dcl-countdown-timer-closing-state button:active { transform: translateY(1px); }
.dcl-countdown-timer-closing-state button:disabled { cursor: default; border-style: dashed; }
.dcl-countdown-timer-closing-state [hidden] { display: none; }
.dcl-countdown-timer-closing-state-badge { padding:.3rem .65rem; border-radius:999px; }
.dcl-countdown-timer-closing-state[data-dcl-scheme="light"] { color: #11292f; background-color: #ffffff; border-color: #697f84; color-scheme: light; }
.dcl-countdown-timer-closing-state[data-dcl-scheme="light"] .dcl-countdown-timer-closing-state-muted, .dcl-countdown-timer-closing-state[data-dcl-scheme="light"] .dcl-countdown-timer-closing-state-eyebrow { color: #405a60; }
.dcl-countdown-timer-closing-state[data-dcl-scheme="light"] .dcl-countdown-timer-closing-state-well, .dcl-countdown-timer-closing-state[data-dcl-scheme="light"] button, .dcl-countdown-timer-closing-state[data-dcl-scheme="light"] kbd { color: #11292f; background-color: #e4edef; border-color: #697f84; }
.dcl-countdown-timer-closing-state[data-dcl-scheme="light"] .dcl-countdown-timer-closing-state-accent, .dcl-countdown-timer-closing-state[data-dcl-scheme="light"] button:hover { color: #ffffff; background-color: #00535a; border-color: #00535a; }
.dcl-countdown-timer-closing-state[data-dcl-scheme="light"] :focus-visible { outline: 3px solid #005c62; outline-offset: 2px; }
.dcl-countdown-timer-closing-state[data-dcl-scheme="light"] meter, .dcl-countdown-timer-closing-state[data-dcl-scheme="light"] progress, .dcl-countdown-timer-closing-state[data-dcl-scheme="light"] input { accent-color: #00535a; }

.dcl-countdown-timer-closing-state[data-dcl-scheme="dark"] { color: #eff8f9; background-color: #142f36; border-color: #839fa5; color-scheme: dark; }
.dcl-countdown-timer-closing-state[data-dcl-scheme="dark"] .dcl-countdown-timer-closing-state-muted, .dcl-countdown-timer-closing-state[data-dcl-scheme="dark"] .dcl-countdown-timer-closing-state-eyebrow { color: #b0c9ce; }
.dcl-countdown-timer-closing-state[data-dcl-scheme="dark"] .dcl-countdown-timer-closing-state-well, .dcl-countdown-timer-closing-state[data-dcl-scheme="dark"] button, .dcl-countdown-timer-closing-state[data-dcl-scheme="dark"] kbd { color: #eff8f9; background-color: #1c3c44; border-color: #839fa5; }
.dcl-countdown-timer-closing-state[data-dcl-scheme="dark"] .dcl-countdown-timer-closing-state-accent, .dcl-countdown-timer-closing-state[data-dcl-scheme="dark"] button:hover { color: #11292f; background-color: #8fe1da; border-color: #8fe1da; }
.dcl-countdown-timer-closing-state[data-dcl-scheme="dark"] :focus-visible { outline: 3px solid #8fe1da; outline-offset: 2px; }
.dcl-countdown-timer-closing-state[data-dcl-scheme="dark"] meter, .dcl-countdown-timer-closing-state[data-dcl-scheme="dark"] progress, .dcl-countdown-timer-closing-state[data-dcl-scheme="dark"] input { accent-color: #8fe1da; }
</style>

When to use

Use where readers need the current open, closes-soon, or closed state before the approximate time. Replace the demo seconds with a data-deadline ISO timestamp including its offset.

Avoid

Do not use the demo clock as your system of record for whether requests can be submitted.

Accessibility

The remaining time is visible but is not a live region. The end is announced once by a separate polite status. Main text contrast is 15.2:1 in light and 13.1:1 in dark.

Was this useful? Send feedback.