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

Overview

The prefers-color-scheme media feature detects whether the user prefers a light or dark interface. It supports system-aligned theming and more respectful defaults.

Browser support

Feature Desktop Mobile
Chrome
Edge
Firefox
Safari
Chrome Android
Safari iOS
93
93
93
CSS at-rule

The prefers-color-scheme CSS media feature is used to detect if a user has requested light or dark color themes. A user indicates their preference through an operating system setting (e.g., light or dark mode) or a user agent setting.

76
79
67
12.1
76
13
prefers-color-scheme.respects-inherited-scheme
Non-standard

Respects `color-scheme` inherited from parent

129
129
105
129
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.

Powered by web-features