Free, independent guides since 2010

Design

Designing a dark mode for your website

How to add a dark mode that looks good and stays readable: using prefers-color-scheme, CSS variables, choosing dark colours, images and logos in dark mode, and offering a manual toggle.

<guide section="Design" type="article" read="1 min"/>

Many people set their phone or computer to dark mode, especially in the evening. Websites can follow that preference automatically. Done well, dark mode is comfortable; done badly, it is a grey mess with unreadable text.

The technical basis

CSS
:root {
  --bg: #faf6f0; --text: #231a1a; --muted: #65585a; --accent: #8f1418;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #171313; --text: #f1e9e2; --muted: #b8aaa5; --accent: #f08f84;
  }
}
body { background: var(--bg); color: var(--text); }
a { color: var(--accent); }

With CSS variables, dark mode is mostly a matter of redefining a few colours.

Colour rules for dark themes

  • Use dark grey or very dark brown instead of pure black: less harsh, better depth.
  • Do not use pure white text; a slightly warm off-white is easier on the eyes.
  • Saturated brand colours often need a lighter, softer version on dark backgrounds.
  • Show elevation with lighter surfaces rather than shadows.
  • Check every combination for contrast with our contrast checker.

Images and logos

  • Logos with dark text need a light variant – use <picture> with a media query, or an SVG that uses currentColor.
  • Screenshots with white backgrounds can be dimmed slightly with CSS.
  • Transparent PNGs with dark details may disappear on dark backgrounds.
HTML
<picture>
  <source srcset="logo-light.svg" media="(prefers-color-scheme: dark)">
  <img src="logo.svg" alt="Company name" width="200" height="60">
</picture>

A manual toggle

A toggle button can set a data-theme attribute on the html element and remember the choice in local storage. Make it a real button with a clear label, and make sure the initial theme is applied before the page paints to avoid a flash.

Frequently asked questions

Does every website need a dark mode?

No. It is a nice extra, especially for sites people read for longer or at night. A well-designed light theme with good contrast is more important.

Should I add a toggle?

Following the system setting is the minimum. A toggle is welcome if visitors may want a different choice on your site; remember their choice locally.