Codectionary / Developer documentation / CSS

prefers-color-scheme & prefers-reduced-motion

These media queries detect user system preferences rather than device characteristics. prefers-color-scheme detects whether the user has requested a light or dark theme at the OS level. prefers-reduced-motion detects whether the user has requested minimal animation, important for users with vestibular motion sensitivity.

Syntax

@media (prefers-color-scheme: dark) { }
@media (prefers-reduced-motion: reduce) { }

Examples

Automatic Dark Mode

Respecting the user's OS-level theme preference without needing a toggle.

:root {
  --bg-color: white;
  --text-color: black;
}

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

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

Respecting Reduced Motion

Disabling or simplifying animations for users who have requested reduced motion.

.card {
  animation: fadeInUp 0.5s ease-out;
}

@media (prefers-reduced-motion: reduce) {
  .card {
    animation: none; /* or a much shorter, simpler transition */
  }
}

Parameters and values

  • prefers-color-scheme (light | dark): Detects the user's OS-level theme preference
  • prefers-reduced-motion (no-preference | reduce): Detects whether the user has requested minimal animation

Best practices

  • Support prefers-color-scheme as a baseline even in projects with a manual theme toggle, so the OS preference is respected on first visit
  • Always wrap meaningful animations and auto-playing effects in a prefers-reduced-motion check - this genuinely matters for users with vestibular disorders
  • Test both preferences using actual OS/browser settings, not just guessing, since behavior can be easy to overlook without deliberately checking
  • Combine both queries with custom properties for a clean, maintainable way to swap entire sets of values based on user preference

At a glance

Purpose
Presentation and layout
File extension
.css
Runs in
Web browsers
Usually used with
HTML and JavaScript

Specifications & further reading

Related CSS documentation