Source Code

<div class="pd-wrap" id="pdWrap">
  <div class="pd-header">
    <h2>Design tokens</h2>
    <label class="pd-switch">
      <input type="checkbox" id="pdToggle" />
      <span class="pd-switch-track"><span class="pd-switch-thumb"></span></span>
      <span class="pd-switch-label">Dark mode</span>
    </label>
  </div>

  <div class="pd-swatches">
    <div class="pd-swatch"><span class="pd-swatch-color" style="background:var(--pd-surface)"></span>Surface</div>
    <div class="pd-swatch"><span class="pd-swatch-color" style="background:var(--pd-text)"></span>Text</div>
    <div class="pd-swatch"><span class="pd-swatch-color" style="background:var(--pd-border)"></span>Border</div>
    <div class="pd-swatch"><span class="pd-swatch-color" style="background:var(--pd-accent)"></span>Accent</div>
  </div>

  <div class="pd-components">
    <button class="pd-btn">Primary button</button>
    <div class="pd-card">
      <h3>Sample card</h3>
      <p>Every color here reads from a shared set of custom properties.</p>
    </div>
    <input class="pd-input" type="text" placeholder="Sample input" />
  </div>
</div>

Dark Mode CSS Variable Palette Demo — Free HTML CSS JS Snippet

Dark Mode CSS Variable Palette Demo · Misc · Plain HTML, CSS & JS · Live preview

What's included

Features

Five semantic CSS custom properties (surface, text, border, accent, muted) drive every color in the demo
Live swatch row visualizing the current value of each token
Sample button, card, and input all restyled purely by variable inheritance, no per-component overrides
@media (prefers-color-scheme: dark) sets the correct theme by default before any manual toggle
Manual data-theme attribute toggle always takes precedence over the OS-level default
No flash of incorrect theme — the default state is resolved via CSS media query, not a JS reflow after load
Toggle switch initial position synced to the resolved starting theme via a single matchMedia read
Fully self-contained, framework-free token system easy to extend with more colors

About this UI Snippet

Dark Mode CSS Variable Palette Demo — Token-Driven Theming with prefers-color-scheme

Screenshot of the Dark Mode CSS Variable Palette Demo snippet rendered live

This snippet demonstrates the core technique behind scalable dark mode support: defining a small set of semantic CSS custom properties — surface, text, border, accent, muted — and having every component in the system read exclusively from those tokens rather than hardcoded colors.

Tokens defined once, consumed everywhere

All five custom properties are declared on the outer .pd-wrap container. The four color swatches, a button, a card, and an input all reference them via var(--pd-surface), var(--pd-text), and so on — nothing in any component's own CSS rule hardcodes a hex value, so redefining the five tokens is sufficient to reskin every element inside the container at once.

Honoring the OS preference by default

Before any manual interaction, a @media (prefers-color-scheme: dark) block guarded by :not([data-theme="light"]) redefines the same five tokens to their dark values — so a visitor whose OS is already set to dark mode sees the dark palette immediately, with no flash of the light theme and no JavaScript required for that initial state. The JS only reads matchMedia once, on load, purely to set the toggle switch's initial checked state to match.

The manual override

Toggling the switch sets an explicit data-theme="dark" or data-theme="light" attribute on the wrapper. A same-specificity-order CSS rule keyed to [data-theme="dark"] redefines the tokens identically to the media-query block, and takes over once present — giving the user a manual override that always wins over the OS-level default while sharing the exact same token values.

Step by step

How to Use

  1. 1
    Load the snippetClick "Dark Mode CSS Variable Palette Demo" in the sidebar to load its HTML, CSS, and JS into the editor panels. The preview updates instantly.
  2. 2
    Edit the codeModify any panel — HTML, CSS, or JS. The preview refreshes as you type. Use Reset in each panel header to restore the original.
  3. 3
    Preview on devicesClick the Mobile (375px), Tablet (768px), or Desktop buttons in the preview header to check responsiveness.
  4. 4
    Export in your formatClick "HTML" to download a standalone file, "JSX" for a React component, "Tailwind" for a React + Tailwind CSS component, "Tailwind HTML" for a standalone HTML file with Tailwind CDN, "Vue" for a Vue 3 SFC with <template>/<script setup>/<style scoped>, or "Angular" for a standalone Angular .component.ts file. "Copy all" copies the full code to clipboard.
  5. 5
    Save your versionClick "Save as", type a name, and press Enter. Your snippet saves to IndexedDB and appears in the Saved tab.

Real-world uses

Common Use Cases

Design system documentation
Demonstrate a token palette and how components consume it to design and engineering stakeholders.
Dark mode implementation reference
A minimal, copy-pasteable pattern for adding CSS-variable-driven dark mode to any project.
Teaching CSS custom properties
Shows both the media-query default and manual-override layers of a token system in one file.
Theming for component libraries
The same five-token approach scales to full component libraries needing consistent light/dark support.

Got questions?

Frequently Asked Questions

Every component references shared custom properties like var(--pd-surface) instead of hardcoded colors. Redefining those five properties at the container level is enough to reskin the button, card, and input simultaneously.

A @media (prefers-color-scheme: dark) block, guarded so it does not fire once a manual light theme is chosen, redefines the same custom properties to dark values — so an OS set to dark mode shows the dark palette immediately on load with no JavaScript needed for that first paint.

It sets an explicit data-theme="dark" or "light" attribute on the wrapper. A CSS rule scoped to [data-theme="dark"] then redefines the tokens, and takes precedence over the OS-level media query default from that point on.

Yes — add the new custom property to both the base :root-style declaration and the dark override blocks (the media query and the [data-theme="dark"] rule), then reference it with var() in any component.