Widely available Recommended when a UI intentionally supports multiple themes and wants to align with system preferences.

Browser support

Feature Desktop Mobile
Chrome
Edge
Firefox
Safari
Chrome Android
Safari iOS
76
79
67
12.1
76
13
CSS at-rule
prefers-color-scheme.respects-inherited-scheme
Non-standard

Respects `color-scheme` inherited from parent

129
129
105
129
Other

The HTTP Sec-CH-Prefers-Color-Scheme request header is a media feature client hint which provides the user's preference for light or dark color themes. A user indicates their preference through an operating system setting (for example, light or dark mode) or a user agent setting.

93
93
93
1+Supported (version) Not supported Has note Sub-feature descriptions sourced from MDN Web Docs (CC BY-SA 2.5)
Notes 3 item(s)
Limitation
  • This browser only partially implements this feature
Removed
  • This feature was removed in a later browser version (129)
Implementation note
  • Only supports SVG images, not iframes.
Notes 3 item(s)
Limitation
  • This browser only partially implements this feature
Removed
  • This feature was removed in a later browser version (129)
Implementation note
  • Only supports SVG images, not iframes.
Notes 3 item(s)
Limitation
  • This browser only partially implements this feature
Removed
  • This feature was removed in a later browser version (129)
Implementation note
  • Only supports SVG images, not iframes.

Syntax

CSS
:root {
  --bg: #ffffff;
  --text: #1a1a1a;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a1a1a;
    --text: #f0f0f0;
  }
}

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

Live demo

lightmode

prefers-color-scheme lightmode demo.

PreviewFullscreen

darkmodesupport

prefers-color-scheme darkmodesupport demo.

PreviewFullscreen

cardexample

prefers-color-scheme cardexample demo.

PreviewFullscreen

Use cases

  • System-aligned theming

    Adapt page surfaces, text, and controls to the user’s preferred scheme without requiring an initial manual toggle.

  • Theme-aware components

    Adjust reusable modules so they stay legible and coherent in both light and dark contexts.

Cautions

  • Dark mode support should be complete enough that contrast, imagery, and focus states remain usable across the interface.
  • System preference is a good default, but many products still benefit from a manual theme override as well.

Accessibility

  • Theme preferences are part of accessibility for many users, so support should stay consistent rather than decorative only.