Skip to main content

CSS Accessibility

CSS Theming

Written by Updated CSS Theming exercises

Build light and dark themes that work with browser controls, user preferences, and forced colors. You will coordinate system preferences, color-scheme, light-dark(), custom-property tokens, explicit overrides, and forced-color behavior.

A dark mode is more than a dark background. Text, borders, focus rings, form controls, shadows and code blocks all have to work together in both themes. The trick is to name colors by their job, like surface and text, and switch the values per theme.

Those named roles are usually CSS Variables. The actual color values can use any format, including the Modern Colors like oklch() that make light and dark versions easier to match.

Follow the reader's system setting

Example

css

:root {
  color-scheme: light dark;
  --surface: light-dark(#ffffff, #111827);
  --text: light-dark(#172033, #e5e7eb);
}

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

color-scheme: light dark tells the browser the page supports both, so its own parts, such as scrollbars, checkboxes and input fields, switch too. light-dark() picks the first color in light mode and the second in dark mode. Two lines per color, and the theme follows the operating system automatically.

Let readers choose

Example

css

html[data-theme="dark"] {
  color-scheme: dark;
  --surface: #111827;
  --text: #e5e7eb;
}

Many sites add a theme button. Store the choice as an attribute on <html>, like data-theme="dark", and override the variables there. Save the choice in localStorage so it survives a reload, and fall back to the system setting when nothing is saved.

High contrast modes

Example

css

@media (forced-colors: active) {
  .button {
    border: 2px solid ButtonText;
    forced-color-adjust: auto;
  }
}

On Windows, some people use a forced-colors mode with their own high-contrast palette. The browser then replaces your colors. Do not fight it: just make sure important shapes still have a border, because backgrounds and shadows disappear in this mode. ButtonText and similar keywords are the system's own colors.

Theming Reference

SyntaxPurposeExample
color-schemeDeclares supported schemes for browser-provided UI.color-scheme: light dark
prefers-color-schemeReads the user's preferred scheme.@media (prefers-color-scheme: dark)
light-dark()Selects a color according to the used scheme.color: light-dark(#111, #eee)
forced-color-adjustControls forced-color adjustments in exceptional cases.forced-color-adjust: auto

Complete Example

Complete runnable example

html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>CSS Theming example</title>
  <style>
    * { box-sizing: border-box; }
    body {
      margin: 0;
      padding: 2rem;
      font-family: system-ui, sans-serif;
      line-height: 1.5;
    }
    main { max-width: 70rem; margin-inline: auto; }
    article, section, aside, form, .card, .panel {
      padding: 1rem;
      border: 1px solid #cbd5e1;
      border-radius: 0.6rem;
    }

:root {
  color-scheme: light dark;
  --surface: light-dark(#ffffff, #111827);
  --text: light-dark(#172033, #e5e7eb);
}

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

html[data-theme="dark"] {
  color-scheme: dark;
  --surface: #111827;
  --text: #e5e7eb;
}

@media (forced-colors: active) {
  .button {
    border: 2px solid ButtonText;
    forced-color-adjust: auto;
  }
}
  </style>
</head>
<body>
  <main><article class="card"><h1>System-aware theme</h1><p>This surface follows the active color scheme.</p><button onclick="document.documentElement.dataset.theme=document.documentElement.dataset.theme==='dark'?'':'dark'">Toggle explicit dark theme</button></article></main>
</body>
</html>

Browser Support

Feature
Chrome
Edge
Firefox
Safari
color-scheme81819613
light-dark()12312312017.5

Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8. A partial-support note is included where it changes how the example behaves.

Testing both themes

  • Check contrast in each theme separately. A grey that is fine on white can be unreadable on near-black.
  • Pure white text on pure black is harsh. Slightly off-white on dark grey is easier on the eyes.
  • Images with transparent backgrounds, like logos, may vanish on dark backgrounds. Give them a light outline or a dark-mode version.

CSS Theming exercises

Dark controls

Tell the browser that the page uses a dark scheme: color-scheme dark on :root.

The HTML your code works on
<div class="card">
  <h2 class="title">Hi</h2>
</div>

Result

Check your answer to see it here.