More CSS answers

by Danny
Published 2 min read

Build dark mode with plain CSS: color-scheme tells the browser which themes you support, light-dark() picks colours, and prefers-color-scheme handles the rest.

The short answer

#

Declare which themes your page supports with color-scheme, then give each colour a light and a dark value with light-dark(). The browser picks the right value from the user's system setting and also switches its own form controls and scroll bars to match.


_10
:root {
_10
color-scheme: light dark;
_10
--bg: light-dark(#ffffff, #121212);
_10
--text: light-dark(#1a1a1a, #e8e8e8);
_10
}
_10
_10
body {
_10
background: var(--bg);
_10
color: var(--text);
_10
}

Why it happens

#

There are three separate pieces of dark mode in CSS, and they are often confused.

  • prefers-color-scheme is a media feature. It reports whether the user's operating system or browser is set to light or dark. It does not change anything on its own.
  • color-scheme is a property. It tells the browser which colour schemes an element can be shown in. With light dark, the browser uses dark defaults for things it draws itself: the page background, form inputs, buttons, scroll bars and the text cursor.
  • light-dark() is a colour function. It returns its first value when the element's used colour scheme is light and its second when it is dark. It only works when color-scheme allows both.

If you only write a prefers-color-scheme media query, your own colours change but native controls stay light, so you end up with white date pickers and scroll bars on a dark page. If you only set color-scheme, the browser switches its defaults but your custom colours stay the same.

light-dark() has been supported in Chrome, Edge, Firefox and Safari since 2024.

How to fix it

#

Use color-scheme with light-dark()

This is the shortest setup for current browsers. Keep colours in custom properties so the theme lives in one place.


_10
:root {
_10
color-scheme: light dark;
_10
--surface: light-dark(#f7f7f8, #1c1c1f);
_10
--border: light-dark(#d4d4d8, #3f3f46);
_10
--link: light-dark(#0b57d0, #8ab4f8);
_10
}

Also add the color-scheme meta tag to the document head. The browser reads it before your CSS loads, which avoids a white flash on dark systems.


_10
<meta name="color-scheme" content="light dark" />

Use prefers-color-scheme for anything that is not a colour

light-dark() only accepts colours. For images, shadows built from several values or other changes, use the media query.


_10
.hero {
_10
background-image: url('/hero-light.jpg');
_10
}
_10
_10
@media (prefers-color-scheme: dark) {
_10
.hero {
_10
background-image: url('/hero-dark.jpg');
_10
}
_10
}

For images in HTML, the picture element accepts the same media query:


_10
<picture>
_10
<source srcset="/logo-dark.svg" media="(prefers-color-scheme: dark)" />
_10
<img src="/logo-light.svg" alt="Company logo" />
_10
</picture>

Add a manual theme toggle

Because light-dark() follows the element's color-scheme, a toggle only needs to change that one property. Set an attribute on the root element from JavaScript and save the choice.


_11
:root {
_11
color-scheme: light dark;
_11
}
_11
_11
:root[data-theme='light'] {
_11
color-scheme: light;
_11
}
_11
_11
:root[data-theme='dark'] {
_11
color-scheme: dark;
_11
}


_10
function setTheme(theme) {
_10
if (theme === 'system') {
_10
delete document.documentElement.dataset.theme;
_10
} else {
_10
document.documentElement.dataset.theme = theme;
_10
}
_10
localStorage.setItem('theme', theme);
_10
}

Run the code that reads the saved theme in a small inline script in the head, before the page paints, to avoid a flash of the wrong theme.

Support older browsers with the media query

If you need to support browsers without light-dark(), define the dark values in a media query instead. It is more verbose but works everywhere.


_12
:root {
_12
color-scheme: light dark;
_12
--bg: #ffffff;
_12
--text: #1a1a1a;
_12
}
_12
_12
@media (prefers-color-scheme: dark) {
_12
:root {
_12
--bg: #121212;
_12
--text: #e8e8e8;
_12
}
_12
}

Common pitfalls

#
  • Using light-dark() without setting color-scheme. The function always returns the light value.
  • Forgetting the meta tag. Without it, dark-mode users can see a white flash before the stylesheet loads.
  • Using pure black and pure white. Very high contrast is tiring to read; slightly softer values usually work better. Check contrast ratios in both themes.
  • Forgetting that color-scheme inherits. Setting color-scheme: dark on one component switches every light-dark() value and native control inside it, which is handy for an always-dark header but surprising if done by accident.
  • Inverting the whole page with filter: invert(1). It also inverts photos and brand colours.
#

Was this resource helpful?