Dark mode for websites: how to do it with CSS
Dark mode for your website with CSS: how to use prefers-color-scheme, CSS variables and light-dark(), and how to avoid the typical mistakes.
You build a dark mode for your website with the CSS media feature prefers-color-scheme: define your colours as CSS variables and override them in a media query for devices set to dark. The page then follows the setting on iPhone, iPad or Mac automatically, with no JavaScript at all.
This post covers the foundation, the shorter light-dark() syntax, the matching meta tags and the typical mistakes.
The foundation: colours as variables and one media query
The most important step happens before dark mode: every colour appears only once in the stylesheet, as a variable. After that, a single block is enough to swap all values for the dark appearance.
:root {
color-scheme: light dark;
--background: #ffffff;
--text: #1d1d1f;
--accent: #0066cc;
}
@media (prefers-color-scheme: dark) {
:root {
--background: #121214;
--text: #f5f5f7;
--accent: #4da3ff;
}
}
body {
background: var(--background);
color: var(--text);
}
a { color: var(--accent); }The line color-scheme: light dark tells the browser that the page supports both appearances. It then adapts form controls, scrollbars and default colours as well.
Shorter with light-dark()
Current browsers support the light-dark() function. It takes two colours and picks the right one for the active appearance. Each variable then holds both values on one line, and the media query is no longer needed.
:root {
color-scheme: light dark;
--background: light-dark(#ffffff, #121214);
--text: light-dark(#1d1d1f, #f5f5f7);
}It requires color-scheme to be set. If your site also needs to look right on older devices, stay with the media query from the first example.
Meta tags, browser bar and images
Three small things make the difference between “works” and “feels finished”: the color-scheme meta tag in the page head prevents a brief white flash while loading. With theme-color you tint the browser bar in Safari to match. And the picture element lets you serve a separate logo to devices in dark mode.
<meta name="color-scheme" content="light dark">
<meta name="theme-color" content="#ffffff"
media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#121214"
media="(prefers-color-scheme: dark)">
<picture>
<source srcset="logo-dark.svg"
media="(prefers-color-scheme: dark)">
<img src="logo-light.svg" alt="Logo">
</picture>Photos usually do not need a second version. Logos and graphics with dark lettering almost always do. The same goes for icons, as described in Favicon and Apple touch icon.
The most common dark mode mistakes
- Pure black and pure white: the harsh contrast is tiring. A very dark grey background and a slightly off-white text colour read more comfortably.
- Simply inverting colours: strong accent colours look too loud on a dark background. Choose slightly lighter, less saturated tones for dark mode.
- Forgetting shadows: shadows barely show on dark surfaces. Lift areas with a slightly lighter background instead.
- Not checking contrast: grey text on dark grey becomes unreadable quickly. Check both appearances separately.
- Hard-coded colours in the HTML: any colour that does not come from a variable stays as it is in dark mode.
Testing dark mode in Safari
You do not have to switch your whole system back and forth. Safari's Web Inspector has a toggle in the Elements tab that sets the appearance to light or dark for the open page only. On iPhone, Control Centre is the quickest way to switch.
Finally, check how your page looks when shared, see Open Graph image for link previews. And if you are planning a page for your own app, A website for your app gives you the outline.