Paj Digital Labs
All articles
Web Design & Engineering

Dark Mode Done Properly: Tokens, Contrast and Images That Don't Glow

Dark mode is more than inverting colours. A token-based approach that keeps contrast correct, respects user preference without a flash, and handles images, code blocks and shadows.

Paj Digital Labs 2 min read
Web design workspace with desktop and laptop displays
Editorial illustration · Web Design & Engineering

Most dark modes are built in an afternoon and look like it: pure black backgrounds, pure white text that vibrates, logos that disappear, and screenshots that light up the room. Doing it well is mostly about structure, not colour picking.

Use semantic tokens

Components should never reference raw colours. They reference roles:

css
:root {
  --background: oklch(0.97 0.006 250);
  --foreground: oklch(0.24 0.06 265);
  --muted-foreground: oklch(0.48 0.03 260);
  --border: oklch(0.88 0.01 250);
}
.dark {
  --background: oklch(0.18 0.03 265);
  --foreground: oklch(0.93 0.01 250);
  --muted-foreground: oklch(0.70 0.02 255);
  --border: oklch(0.30 0.03 265);
}

A component that uses var(--foreground) works in both themes automatically. A component that hardcodes a hex value breaks in one of them.

Avoid pure black and pure white

Pure white text on pure black creates harsh contrast that many readers find tiring for long reading. Slightly tinted dark backgrounds and slightly softened text read better and look more considered. Keep body text contrast comfortably above 4.5:1 — just not at the absolute maximum.

Elevation changes direction

In light mode, shadows communicate depth. In dark mode, shadows barely show. Instead, raised surfaces get slightly lighter backgrounds or more visible borders. If your design uses hairline rules rather than shadows, you already have an easier time.

Respect the system setting, without a flash

Default to the user's system preference, let them override it, and remember the choice. The common bug is a flash of the wrong theme on load, because the stored preference is applied after the page renders.

Apply the class before first paint with a small inline script in the document head:

html
<script>
  (function () {
    var t = localStorage.getItem(class="text-code-string">"theme");
    var dark = t ? t === class="text-code-string">"dark" : matchMedia(class="text-code-string">"(prefers-color-scheme: dark)").matches;
    document.documentElement.classList.toggle(class="text-code-string">"dark", dark);
  })();
</script>

Also set color-scheme: light dark so form controls and scrollbars match the theme.

Images and media

  • Logos: provide a light-on-dark version, or use SVG that inherits currentColor.
  • Screenshots: consider a subtle border so bright images don't float on a dark page; reduce brightness slightly if needed.
  • Diagrams: transparent PNGs with black lines become invisible — use SVG with theme colours, or give them a light background panel.
css
.dark img:not([data-no-dim]) { filter: brightness(0.92); }

Code blocks

Use a syntax theme designed for each mode rather than one theme for both. Check that comments are still readable — they are often the lowest-contrast colour in a syntax theme.

Test both themes on every change

Dark mode regresses quietly. Every new component should be reviewed in both themes. Automated contrast checks run against each theme catch the most common breakages before release.

Done structurally, dark mode costs almost nothing to maintain. Done as an afterthought, it becomes a second design to fix forever.

Paj Digital Labs

The engineering journal of Paj Digital Solutions — hosting, server infrastructure and web engineering, written by the people who run the servers.

Related reading