Dark Mode in 2026: prefers-color-scheme and color-scheme

Dark Mode: prefers-color-scheme and color-scheme

Modern dark mode without duplicated component CSS — prefers-color-scheme, color-scheme, semantic tokens, custom properties, and a three-option system override.

Modern dark mode does not need duplicated component CSS. Define theme tokens as CSS custom properties on :root, override the same tokens inside @media (prefers-color-scheme: dark), and let components consume only tokens — the theme switch happens in one place. Set color-scheme: light dark on :root so form controls and scrollbars follow the theme, eliminating the visible mismatch where the page goes dark but the inputs stay light. Add a three-option override toggle (system / light / dark) backed by a data-theme attribute on <html> so users can opt out per site. Test both schemes against WCAG 1.4.3 contrast minimum, and consider easing pure white on pure black — near-black and near-white anchors still keep contrast well above AAA.

Why dark mode is a responsive concern

Responsive design used to mean adapting to viewport size. In 2026 it also means adapting to environment and user preference — the same site rendering on the same device should look different at 2pm in an office and at 11pm in a dark bedroom. The native prefers-color-scheme media feature (Baseline 2020) exposes the user's light or dark theme preference — set in the operating system or in the browser — to CSS, which means a single stylesheet can respond to both viewport size and color scheme through the same media query mechanism.

The other half of the story is browser UI. Form controls (checkboxes, radio buttons, text inputs), scrollbars, the canvas surface behind the document, and other browser-provided UI such as spellcheck underlines have a default appearance that the browser draws based on the page's declared color scheme. Until you tell the browser "this page supports dark," form controls keep the browser's default appearance even when the rest of the page is dark (a browser may override this when the user has asked it to force a color scheme) — producing a visible mismatch that signals "this site forgot to finish their dark mode." The color-scheme CSS property (Baseline 2022), or the matching <meta name="color-scheme"> tag in HTML, is the signal that fixes this.

Together, prefers-color-scheme (the media query) and color-scheme (the CSS property) are the two primitives that make dark mode declarative instead of imperative. The rest is token discipline and one cascade rule for user overrides.

Custom property strategy

The most important architectural decision in a dark mode refactor is whether the theme switch happens at the token layer or at the component layer. The wrong answer is to duplicate component CSS:

css
/* Anti-pattern — duplicated component CSS */
.card { background: white; color: #111; border: 1px solid #ddd; }

@media (prefers-color-scheme: dark) {
  .card { background: #1a1a1a; color: #eee; border: 1px solid #333; }
}

This duplicates every property for every component that uses a themable value. Adding a new component means writing both branches. Adding a new color value means hunting for every place that references it. The CSS grows linearly with the cross product of components and themes.

The right pattern is to define theme tokens once on :root, override the same tokens in the dark query, and let components reference only tokens:

css
:root {
  --surface: #ffffff;
  --surface-elevated: #fafafa;
  --text: #111111;
  --text-muted: #555555;
  --border: #dddddd;
  --accent: #2563eb;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --surface: #121212;
    --surface-elevated: #1e1e1e;
    --text: #ededed;
    --text-muted: #a0a0a0;
    --border: #2a2a2a;
    --accent: #60a5fa;
  }
}

.card {
  background: var(--surface-elevated);
  color: var(--text);
  border: 1px solid var(--border);
}

Components no longer know about the theme; they consume tokens, and the tokens carry the theme. Adding a new component means writing it once against tokens. Adding a new theme — high contrast, sepia, brand-specific — means adding one more override block. CSS Custom Properties reached Baseline 2017 and have been Widely available since 2019, so the pattern works in all the core browsers that Baseline tracks.

The naming discipline that makes this work is using semantic token names — --surface, --text, --border — not literal names like --gray-500 or --white. Literal names tie components to specific values; semantic names let the same component switch themes by switching what the token resolves to.

The color-scheme CSS property

Set color-scheme: light dark on :root and the browser knows the page supports both schemes. The native UI then follows:

css
:root {
  color-scheme: light dark;
}

The visible effects (a browser may override them when the user has asked it to force a color scheme):

ElementWithout color-scheme (property or meta tag)With color-scheme: light dark
Text inputsLight background even in dark themeAdapt to active scheme
Checkboxes / radiosLight borders in dark themeTheme-appropriate
ScrollbarsLight on dark theme — visible mismatchMatch active scheme
Document default backgroundBrowser's default schemeMatches the active scheme once the stylesheet is parsed; the meta tag can apply it before CSS loads

The last row is the subtle one. The CSS property takes effect only after the stylesheet that declares it has been downloaded (when it is referenced with <link rel="stylesheet">) and parsed, so on its own it cannot set the background the browser paints before then. To help the browser render the page background in the chosen scheme immediately, also put <meta name="color-scheme" content="light dark"> in <head>; the meta tag and the property on :root end in the same behavior, and the meta tag lets the browser adopt the preferred scheme sooner. The web.dev color-scheme article walks through the rendering path in detail.

Omit both the property and the color-scheme meta tag and form controls keep the browser's default appearance even when the rest of the page is dark (a browser may override this when the user has asked it to force a color scheme). If form controls and scrollbars stay light in your dark theme, check whether the property or the meta tag is actually in effect.

Image handling across themes

Logos and illustrations need to consider both themes. The patterns that scale:

  • SVG with currentColor — for monochrome marks, set fill="currentColor" on the SVG and the icon inherits the surrounding text color. One asset, both themes:
html
<svg width="24" height="24" fill="currentColor" aria-hidden="true">
  <path d="..." />
</svg>
  • Inline SVG with CSS variables — for multi-color marks, define fills as var(--icon-primary) and override the token in the dark theme. The icon is one asset; the theming is centralized.
  • A <picture> element with a prefers-color-scheme media source — for raster images that genuinely need different versions:
html
<picture>
  <source srcset="logo-dark.png" media="(prefers-color-scheme: dark)">
  <img src="logo-light.png" alt="...">
</picture>
  • Photographs — usually no change. Some sites apply a slight desaturation in dark mode (filter: brightness(0.9) contrast(0.95)) to reduce retina shock against the dark surface, but this is a taste choice, not a requirement.

The pattern to avoid is encoding theme assumptions into image filenames or paths in JavaScript. Runtime theme switching that swaps src attributes moves the image choice out of the markup and into script, and each variant still needs a request the first time it is shown; the <picture> and CSS patterns above keep that choice declarative.

User override toggle

Some users want to override the OS preference per site — they run their OS in dark mode but prefer one particular news site in light, or vice versa. The pattern is a three-option control: system (default, follows the OS or browser setting), light, dark.

The implementation:

js
// Read preference at page load, before paint, to avoid flash
const stored = localStorage.getItem('theme'); // 'light' | 'dark' | null
if (stored) document.documentElement.setAttribute('data-theme', stored);
css
/* Default: follow the user's system or browser preference */
:root { /* light tokens */ }
@media (prefers-color-scheme: dark) {
  :root { /* dark tokens */ }
}

/* Explicit override wins over the media query */
:root[data-theme="light"] { color-scheme: light; /* light tokens — same as :root default */ }
:root[data-theme="dark"] { color-scheme: dark; /* dark tokens — same as the dark @media block */ }

The selector choice matters. A bare [data-theme] attribute selector and :root carry the same specificity (0,1,0), and a @media block adds none, so a bare attribute rule would only win by coming later in the source — fragile once stylesheets are split or reordered. Compounding the two as :root[data-theme] raises the override to (0,2,0), so it beats the media-query block regardless of order. Each override also pins color-scheme to the chosen value; otherwise light dark lets form controls keep following the user's system or browser preference while the page follows the toggle. When the user picks "system," remove the attribute and the media query takes over again.

Inline the localStorage read in a <script> in <head> before the stylesheet so the attribute is applied before first paint. This is a case where blocking JavaScript is the right trade-off — the alternative is a flash of the wrong theme during page load, which is more disruptive than the brief pause a small inline script adds.

Provide three options — system, light, dark — not just a binary toggle. The binary forces users who want their OS preference to choose one specific value, losing the ability to follow the OS when it is set to switch between dark and light automatically.

Color contrast survives both themes

WCAG Success Criterion 1.4.3 Contrast Minimum (Level AA) requires the visual presentation of text and images of text to have a contrast ratio of at least 4.5:1, except that large-scale text and images of large-scale text need at least 3:1, and incidental text or images of text (part of an inactive user interface component, pure decoration, not visible to anyone, or part of a picture that contains significant other visual content) and text that is part of a logo or brand name have no contrast requirement. The criterion does not distinguish between themes, so the same thresholds apply in both light and dark. Designing one theme well and the other poorly is a common failure mode. A heading at color: #333 on background: white is 12.6:1 (well above AAA); the same color logic flipped to color: #ccc on background: black is 13.1:1 (also strong). But a muted body text at color: #777 on white is 4.48:1 — just under the 4.5:1 AA line, so it fails; #767676 (4.54:1) is the lightest gray that passes. Flipped to color: #888 on black it is 5.9:1 — passing, but with little margin, and a refactor that moves the muted color a couple of shades closer to the background drops it below AA.

Run the contrast checker on both themes. A contrast checker such as WebAIM's, run against each theme's token values, catches a token that passes in light mode and fails in dark, or the reverse.

Two anchors to watch:

  • Pure white (#fff) on pure black (#000) — 21:1, the maximum ratio the WCAG formula allows. One approach is to ease both anchors: a near-black background around #121212 and near-white text around #ededed produce roughly 16:1 — still well above AAA — without using the extremes.
  • Focus rings invisible in dark mode — many sites use outline: 2px solid blue on focus, which is fine on white but disappears against a dark blue button. Tokenize the focus color (--focus-ring) and override it in the dark theme so it stays visible against the new background colors.

Phase 1 deliverables and acceptance criteria

The dark mode refactor pays for itself in maintenance reduction even if no users ever toggle the theme.

  • Refactor color CSS into custom properties on :root with semantic names (--surface, --text, --border). Components reference only tokens, never raw color values.
  • Add @media (prefers-color-scheme: dark) overriding the same tokens. Do not duplicate component CSS.
  • Set color-scheme: light dark on :root so form controls and scrollbars follow the theme, and add <meta name="color-scheme" content="light dark"> in <head> so the browser can paint the initial background in the chosen scheme before CSS loads.
  • Add a three-option toggle (system / light / dark) backed by a data-theme attribute on <html>. Inline the localStorage read in <head> before the stylesheet.
  • Run the contrast checker on both schemes. Ease pure-white-on-pure-black to roughly #ededed on #121212 if you started there.
  • Verify focus rings remain visible in dark mode — tokenize the focus color if not already.

Modern dark mode is seven coordinated pieces — three CSS features (prefers-color-scheme, color-scheme, custom properties), one naming discipline (semantic tokens), one HTML attribute (data-theme), one meta tag (color-scheme), and one inline script that reads the stored preference — arranged so the theme switches in one place and components stay theme-agnostic. The cost of doing it well is one refactor; the cost of doing it badly is duplicated CSS that grows with every feature you ship.

Project Inquiry