← All posts

A clean CSS dark mode with light-dark() and color-scheme

Dark mode isn't colour inversion. With color-scheme and the light-dark() function you declare each colour once, follow the OS setting, let users override it and avoid the white flash on load.

A clean CSS dark mode with light-dark() and color-scheme

Key points

  • color-scheme also restyles form controls, scrollbars and the default canvas.
  • light-dark() keeps both values of a colour side by side.
  • A tiny inline script in the <head> applies the saved choice before first paint.

“Can we have a dark mode?” It’s a common request, especially for apps people keep open all day. The first attempt we usually find in existing code duplicates every rule inside a prefers-color-scheme media query, and a few months later half the components have been forgotten. Modern CSS offers a much tidier route, and it’s what we now use on new builds.

One declaration per colour

light-dark() takes two colours: the first is used in a light scheme, the second in a dark one. It only works when color-scheme is set. Put it in custom properties and have the rest of the stylesheet use only those:

:root {
  color-scheme: light dark;
  --bg:     light-dark(#ffffff, #14161a);
  --surface:light-dark(#f4f5f7, #1e2127);
  --text:   light-dark(#1d2025, #e4e6ea);
  --muted:  light-dark(#5a6170, #9ba3b1);
  --accent: light-dark(#0b57d0, #8ab4f8);
}
:root[data-theme="light"] { color-scheme: light; }
:root[data-theme="dark"]  { color-scheme: dark; }

body {
  background: var(--bg);
  color: var(--text);
}

color-scheme: light dark makes the browser follow the OS. The two attribute rules handle a manual override: forcing light or dark flips every light-dark() at once.

The part people miss: color-scheme also changes inputs, checkboxes and scrollbars. Without it you get the infamous glaring white text field in the middle of a dark page.

Remember the choice without a flash

Apply the saved preference from a script at the bottom of the page and users see the wrong theme for a moment before it switches. The fix is a very short inline script in the <head>, before the stylesheet:

<meta name="color-scheme" content="light dark">
<script>
  try {
    const saved = localStorage.getItem('theme');
    if (saved === 'light' || saved === 'dark') {
      document.documentElement.dataset.theme = saved;
    }
  } catch (e) {}
</script>

The meta tag lets the browser pick the right background before the CSS arrives, and the try block covers browsers that block storage in private mode. The toggle itself can load normally:

const root = document.documentElement;
const button = document.querySelector('#theme-toggle');

button.addEventListener('click', () => {
  const current = root.dataset.theme
    || (matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');
  const next = current === 'dark' ? 'light' : 'dark';
  root.dataset.theme = next;
  button.setAttribute('aria-pressed', String(next === 'dark'));
  try { localStorage.setItem('theme', next); } catch (e) {}
});

What custom properties can’t fix

light-dark() only accepts colours, so images, shadows and logos need something else. For logos we ship an SVG that uses currentColor, so it follows the text colour in both themes.

Be careful with <picture> and media="(prefers-color-scheme: dark)": it follows the OS, not your toggle. If you offer a manual switch, key image swaps off an attribute on the root element instead.

Skip pure black and pure white. A very dark grey is easier on the eyes and leaves room to separate surfaces. And check contrast for every colour in both themes: an accent that reads well on white rarely does on a dark background.

FAQ

Do older browsers support light-dark()?

Chrome, Edge, Firefox and Safari have supported it since 2024. If your audience includes older devices, declare a plain value first and the light-dark() version after it; browsers keep the last one they understand.

Toggle button or just follow the OS?

Following the OS is a fine starting point. A toggle earns its place when people spend long sessions on the site, or when the dark theme doesn’t suit a particular task.

Want to add dark mode to your site or app without rewriting everything? We can restructure your colour system so both themes stay consistent.

Share LinkedIn Facebook X WhatsApp
</>

webtestudio

Web agency: custom WordPress sites, premium themes and SaaS software.

Discuss your project →