Home CSS Media Queries prefers-color-scheme Definition
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 schemesSet color-scheme: light dark on :root so built-in controls, scrollbars, and form fields can use an appropriate browser theme.
Syntax
CSS Syntax @media (prefers-color-scheme:light | dark ) { … }
Wrap the feature in parentheses and put the conditional CSS inside the media block.
Values
Feature Values Value Description Try it lightThe user prefers or has not indicated a preference beyond a light appearance Try itdarkThe user prefers a dark appearance Try it
Approach
How to Use prefers-color-scheme Use the preference to enhance a solid, accessible default.
Choose a complete light theme as the default. Override theme tokens inside @media (prefers-color-scheme: dark). Declare color-scheme: light dark for browser-provided UI. Check text, links, focus rings, images, and form controls in both modes. Piece Required? Role @mediaYes Wraps the preference query prefers-color-schemeYes Names the media feature Accessible defaults Yes Work when no query matches Real-device testing Recommended Confirms operating-system behavior
Try it
Live Preview Your browser evaluates each prefers-color-scheme value below through matchMedia.
Code
CSS Examples Each example keeps the default usable, then responds to prefers-color-scheme. Open Try It Yourself for a live editor.
1. Dark theme 2. Light preference 3. Theme tokens Example 1 — Apply a Dark Theme Change page colors when the system requests dark mode.
body {
background: #ffffff;
color: #172033;
}
@media (prefers-color-scheme: dark) {
body {
background: #111827;
color: #f8fafc;
}
} The surface and text follow the active system appearance.
How It Works Light colors form the default theme. The query detects a dark preference. Both background and foreground change together. Readable contrast is preserved in either mode. Example 2 — Target Light Appearance Use an explicit light query for a light-only enhancement.
.logo { filter: none; }
@media (prefers-color-scheme: light) {
.logo {
filter: drop-shadow(0 2px 6px #94a3b8);
}
} The logo gets a subtle shadow in light mode.
How It Works The logo works without an effect. The query matches the light preference. The shadow improves separation on a bright surface. Dark mode keeps the neutral default. Example 3 — Switch CSS Custom Properties Centralize theme colors with custom properties.
:root {
color-scheme: light dark;
--surface: #ffffff;
--text: #172033;
}
@media (prefers-color-scheme: dark) {
:root {
--surface: #111827;
--text: #f8fafc;
}
} Components can reuse the same surface and text tokens.
How It Works Custom properties define reusable theme tokens. color-scheme informs browser controls.Dark mode overrides the tokens once. 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.
Compatibility
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.
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.
Remember
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 What does prefers-color-scheme do? It lets CSS detect whether the user prefers a light or dark interface appearance.
What values are available? The practical values are light and dark. Light also covers cases where the user has not expressed a dark preference.
Does it automatically create dark mode? No. It only supplies the condition. You must define suitable background, text, border, link, and control colors.
What is the color-scheme property for? It tells the browser which schemes the page supports so built-in controls and other browser-provided UI can render appropriately.
Can a manual theme override the system? Yes. Store an explicit user choice and apply a class or data attribute with equal or higher priority than the system-based styles.
How can JavaScript read the preference? Use window.matchMedia('(prefers-color-scheme: dark)') and inspect matches or listen for changes.
CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion