Skip to the main content

Dark Mode Toggle

Free Utilities

A switch between light and dark that follows the system setting until the visitor chooses, and remembers the choice where storage is allowed.

Checked on 3 December 2026 in Chrome 143.0.7499.169: 12 automated checks. What the checks are

Preview and code

HTML

<section class="dmt" aria-labelledby="dmt-title">
  <h2 class="dmt__title" id="dmt-title">Appearance</h2>

  <div class="dmt__row">
    <div class="dmt__text">
      <p class="dmt__label" id="dmt-label">Dark mode</p>
      <p class="dmt__hint" id="dmt-hint">Follows your system setting until you choose.</p>
    </div>
    <button class="dmt__switch" id="dmt-switch" type="button" role="switch" aria-checked="false" aria-labelledby="dmt-label" aria-describedby="dmt-hint" hidden>
      <span class="dmt__knob" aria-hidden="true">
        <svg class="dmt__sun" viewBox="0 0 20 20" width="16" height="16" focusable="false"><circle cx="10" cy="10" r="3.6" fill="currentColor"/><path d="M10 2v2.4M10 15.6V18M2 10h2.4M15.6 10H18M4.3 4.3L6 6M14 14l1.7 1.7M15.7 4.3L14 6M6 14l-1.7 1.7" stroke="currentColor" stroke-width="1.8" stroke-linecap="round"/></svg>
        <svg class="dmt__moon" viewBox="0 0 20 20" width="16" height="16" focusable="false"><path d="M16.5 12.2A7 7 0 0 1 7.8 3.5a7 7 0 1 0 8.7 8.7z" fill="currentColor"/></svg>
      </span>
    </button>
  </div>

  <div class="dmt__preview" aria-hidden="true">
    <div class="dmt__window">
      <div class="dmt__dots"><i></i><i></i><i></i></div>
      <div class="dmt__line dmt__line--title"></div>
      <div class="dmt__line"></div>
      <div class="dmt__line dmt__line--short"></div>
      <div class="dmt__chip">Sample button</div>
    </div>
  </div>

  <p class="dmt__status" id="dmt-status" role="status">Following the system setting.</p>
  <p class="dmt__actions"><button class="dmt__reset" id="dmt-reset" type="button" hidden>Use the system setting</button></p>
  <p class="dmt__note" id="dmt-note" hidden>Storage is blocked here, so your choice is not kept.</p>
</section>

CSS

.dmt {
  --dmt-surface: #ffffff;
  --dmt-ink: #1d2230;
  --dmt-muted: #505a70;
  --dmt-line: #dbe0ec;
  --dmt-wash: #f1f4fa;
  --dmt-track: #5d6679;
  --dmt-knob: #ffffff;
  --dmt-knob-ink: #b25b00;
  --dmt-accent: #3b3fc4;
  --dmt-accent-ink: #ffffff;
  --dmt-ring: #1d2230;
  box-sizing: border-box;
  width: min(100%, 28rem);
  padding: 1.5rem;
  color: var(--dmt-ink);
  background: var(--dmt-surface);
  border: 1px solid var(--dmt-line);
  border-radius: 18px;
  box-shadow: 0 1px 2px rgb(29 34 48 / 6%), 0 14px 32px -14px rgb(29 34 48 / 26%);
  font: 1rem/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  transition: background-color 0.25s ease, color 0.25s ease, border-color 0.25s ease;
}
.dmt[data-theme="dark"] {
  --dmt-surface: #171b2b; --dmt-ink: #eceff9; --dmt-muted: #b0b8d2; --dmt-line: #2f3656; --dmt-wash: #212744;
  --dmt-track: #3b3fc4; --dmt-knob: #171b2b; --dmt-knob-ink: #ffd36a; --dmt-accent: #9ea4ff; --dmt-accent-ink: #12142b; --dmt-ring: #eceff9;
  box-shadow: 0 1px 2px rgb(0 0 0 / 35%), 0 16px 34px -14px rgb(0 0 0 / 70%);
}
@media (prefers-color-scheme: dark) {
  .dmt:not([data-theme="light"]) {
    --dmt-surface: #171b2b; --dmt-ink: #eceff9; --dmt-muted: #b0b8d2; --dmt-line: #2f3656; --dmt-wash: #212744;
    --dmt-track: #3b3fc4; --dmt-knob: #171b2b; --dmt-knob-ink: #ffd36a; --dmt-accent: #9ea4ff; --dmt-accent-ink: #12142b; --dmt-ring: #eceff9;
    box-shadow: 0 1px 2px rgb(0 0 0 / 35%), 0 16px 34px -14px rgb(0 0 0 / 70%);
  }
}
.dmt *, .dmt *::before, .dmt *::after { box-sizing: border-box; }
.dmt__title { margin: 0 0 1rem; font-size: 1.35rem; line-height: 1.25; }
.dmt__row { display: flex; align-items: center; justify-content: space-between; gap: 1rem; padding: 0.9rem 1rem; background: var(--dmt-wash); border-radius: 14px; }
.dmt__label { margin: 0; font-weight: 700; }
.dmt__hint { margin: 0.1rem 0 0; font-size: 0.85rem; color: var(--dmt-muted); }
.dmt__switch { position: relative; flex: none; width: 3.75rem; height: 2.25rem; padding: 0; background: var(--dmt-track); border: 0; border-radius: 999px; cursor: pointer; transition: background-color 0.25s ease; }
.dmt__switch[hidden] { display: none; }
.dmt__knob { position: absolute; top: 0.25rem; left: 0.25rem; display: grid; place-items: center; width: 1.75rem; height: 1.75rem; color: var(--dmt-knob-ink); background: var(--dmt-knob); border-radius: 50%; transition: transform 0.25s ease; }
.dmt__switch[aria-checked="true"] .dmt__knob { transform: translateX(1.5rem); }
.dmt__moon, .dmt__switch[aria-checked="true"] .dmt__sun { display: none; }
.dmt__switch[aria-checked="true"] .dmt__moon { display: block; }
.dmt__switch:focus-visible, .dmt__reset:focus-visible { outline: 3px solid var(--dmt-ring); outline-offset: 3px; }
.dmt__preview { margin: 1rem 0; padding: 1rem; background: var(--dmt-wash); border-radius: 14px; }
.dmt__window { display: grid; gap: 0.5rem; padding: 0.9rem; background: var(--dmt-surface); border: 1px solid var(--dmt-line); border-radius: 12px; }
.dmt__dots { display: flex; gap: 0.3rem; margin-bottom: 0.2rem; }
.dmt__dots i { width: 0.55rem; height: 0.55rem; background: var(--dmt-line); border-radius: 50%; }
.dmt__line { height: 0.55rem; background: var(--dmt-line); border-radius: 4px; }
.dmt__line--title { width: 45%; height: 0.8rem; background: var(--dmt-muted); }
.dmt__line--short { width: 65%; }
.dmt__chip { justify-self: start; margin-top: 0.3rem; padding: 0.35rem 0.8rem; font-size: 0.85rem; font-weight: 700; color: var(--dmt-accent-ink); background: var(--dmt-accent); border-radius: 8px; }
.dmt__status { margin: 0; font-size: 0.9rem; font-weight: 600; }
.dmt__actions { margin: 0.4rem 0 0; }
.dmt__reset { min-height: 44px; padding: 0 0.25rem; font: inherit; font-weight: 600; color: var(--dmt-accent); background: transparent; border: 0; text-decoration: underline; text-underline-offset: 3px; cursor: pointer; }
.dmt__reset[hidden] { display: none; }
.dmt__note { margin: 0.5rem 0 0; font-size: 0.8rem; color: var(--dmt-muted); }
@media (max-width: 26rem) {
  .dmt { padding: 1rem; }
}
@media (prefers-reduced-motion: reduce) {
  .dmt, .dmt__switch, .dmt__knob { transition: none; }
}

JavaScript

(function () {
  'use strict';
  var KEY = 'dmt-choice';
  var root = document.querySelector('.dmt');
  var sw = document.getElementById('dmt-switch');
  var reset = document.getElementById('dmt-reset');
  var status = document.getElementById('dmt-status');
  var note = document.getElementById('dmt-note');
  if (!root || !sw || !reset || !status || !note) return;
  var media = window.matchMedia('(prefers-color-scheme: dark)');
  var canStore = true;

  // To theme a whole page, set data-theme on the html element instead of on root, and
  // change the two selectors in the style sheet from .dmt[data-theme] to [data-theme].
  function read() {
    try {
      var value = localStorage.getItem(KEY);
      return value === 'light' || value === 'dark' ? value : null;
    } catch (error) {
      canStore = false;
      return null;
    }
  }
  function write(value) {
    try {
      if (value) localStorage.setItem(KEY, value); else localStorage.removeItem(KEY);
    } catch (error) {
      canStore = false;
    }
  }

  var choice = read();
  function effective() { return choice || (media.matches ? 'dark' : 'light'); }
  function render() {
    if (choice) root.setAttribute('data-theme', choice); else root.removeAttribute('data-theme');
    sw.setAttribute('aria-checked', effective() === 'dark' ? 'true' : 'false');
    reset.hidden = !choice;
    note.hidden = canStore;
    status.textContent = choice ? (choice === 'dark' ? 'Dark mode is on.' : 'Light mode is on.') : 'Following the system setting (' + effective() + ').';
  }

  sw.hidden = false;
  sw.addEventListener('click', function () { choice = effective() === 'dark' ? 'light' : 'dark'; write(choice); render(); });
  reset.addEventListener('click', function () { choice = null; write(null); render(); sw.focus(); });
  media.addEventListener('change', render);
  render();
})();

Accessibility notes

Written by the author of the snippet. Each note has a test step or a markup check that reads the page.

  • The control is a button with role switch and aria-checked, named by its visible label.
  • A status line says which setting is active, and Space or Enter flips the switch.
  • Without a script the page still follows the system setting through prefers-color-scheme.
Utilities
FreeUses XON

To-do List

A small task list with add, tick, delete, filter and clear-done, with the count announced in a live region and nothing sent anywhere.

  • HTML
  • CSS
  • Uses XON
  • Lists
  • State