CSS prefers-color-scheme Media Feature

Beginner
6 min read
Updated: Nov 2026
3 examples
Live preview

Definition and Usage

The prefers-color-scheme media feature detects whether the user prefers a light or dark color theme.

Use it to adapt backgrounds, text, borders, and controls to the operating system appearance. A complete theme needs readable contrast in both modes, not just an inverted background.

Baseline · Widely available.

This preference query is designed for progressive enhancement. Browsers that do not support it keep the default styles.

Tip Declare supported schemes

Set color-scheme: light dark on :root so built-in controls, scrollbars, and form fields can use an appropriate browser theme.

CSS Syntax

syntax.css
@media (prefers-color-scheme:light | dark) { … }

Wrap the feature in parentheses and put the conditional CSS inside the media block.

Feature Values

ValueDescriptionTry it
lightThe user prefers or has not indicated a preference beyond a light appearanceTry it
darkThe user prefers a dark appearanceTry it

How to Use prefers-color-scheme

Use the preference to enhance a solid, accessible default.

  1. Choose a complete light theme as the default.
  2. Override theme tokens inside @media (prefers-color-scheme: dark).
  3. Declare color-scheme: light dark for browser-provided UI.
  4. Check text, links, focus rings, images, and form controls in both modes.
PieceRequired?Role
@mediaYesWraps the preference query
prefers-color-schemeYesNames the media feature
Accessible defaultsYesWork when no query matches
Real-device testingRecommendedConfirms operating-system behavior

Live Preview

Your browser evaluates each prefers-color-scheme value below through matchMedia.

Detected preference

Change the related operating-system accessibility or appearance setting, then recheck.

Preference detection
lightdark

Checking…

CSS Examples

Each example keeps the default usable, then responds to prefers-color-scheme. Open Try It Yourself for a live editor.

Example 1 — Apply a Dark Theme

Change page colors when the system requests dark mode.

CSS
body {
  background: #ffffff;
  color: #172033;
}

@media (prefers-color-scheme: dark) {
  body {
    background: #111827;
    color: #f8fafc;
  }
}
Try It Yourself

How It Works

  1. Light colors form the default theme.
  2. The query detects a dark preference.
  3. Both background and foreground change together.
  4. Readable contrast is preserved in either mode.

Example 2 — Target Light Appearance

Use an explicit light query for a light-only enhancement.

CSS
.logo { filter: none; }

@media (prefers-color-scheme: light) {
  .logo {
    filter: drop-shadow(0 2px 6px #94a3b8);
  }
}
Try It Yourself

How It Works

  1. The logo works without an effect.
  2. The query matches the light preference.
  3. The shadow improves separation on a bright surface.
  4. Dark mode keeps the neutral default.

Example 3 — Switch CSS Custom Properties

Centralize theme colors with custom properties.

CSS
:root {
  color-scheme: light dark;
  --surface: #ffffff;
  --text: #172033;
}

@media (prefers-color-scheme: dark) {
  :root {
    --surface: #111827;
    --text: #f8fafc;
  }
}
Try It Yourself

How It Works

  1. Custom properties define reusable theme tokens.
  2. color-scheme informs browser controls.
  3. Dark mode overrides the tokens once.
  4. Components consume tokens without repeating media queries.

Edge Cases & Pitfalls

Dark mode needs a designed palette; automatic inversion is rarely enough.

contrast

Check every color pair

Muted text and borders can become unreadable after a theme switch.

images

Images may need variants

Bright illustrations can glare in dark mode; use suitable assets or restrained filters.

controls

Include browser UI

Use color-scheme so form controls match the page.

override

System choice is a default

If your app offers a manual theme selector, let that explicit choice override the media query.

Browser Support

prefers-color-scheme is a browser media feature. Node.js, Deno, and Bun do not apply CSS media queries.

✓ Baseline · Widely available

prefers-color-scheme

Supported across modern browsers since 2020.

98% Modern browsers
Google Chrome 76+
Full support
Mozilla Firefox 67+
Full support
Apple Safari 12.1+
Full support
Microsoft Edge 79+
Full support
Opera 62+
Full support
Samsung Internet 12+
Full support
Internet Explorer No
No support
@media (prefers-color-scheme) Progressive enhancement

Bottom line: Production-ready. Design and test complete palettes for both light and dark modes.

Key Takeaways

  • Role: respond to a user’s prefers-color-scheme setting.
  • Values: light, dark.
  • Default: keep the page accessible when the query is unsupported.
  • Test: use actual operating-system preference settings.

One line: use prefers-color-scheme to respect user preferences without weakening the default experience.

Frequently Asked Questions

It lets CSS detect whether the user prefers a light or dark interface appearance.
The practical values are light and dark. Light also covers cases where the user has not expressed a dark preference.
No. It only supplies the condition. You must define suitable background, text, border, link, and control colors.
It tells the browser which schemes the page supports so built-in controls and other browser-provided UI can render appropriately.
Yes. Store an explicit user choice and apply a class or data attribute with equal or higher priority than the system-based styles.
Use window.matchMedia('(prefers-color-scheme: dark)') and inspect matches or listen for changes.

Next: prefers-contrast

Continue with the prefers-contrast media feature.

prefers-contrast tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful