prefers-color-scheme
The prefers-color-scheme
CSS media feature is used to detect if the user has requested a light or dark color theme.
The user might indicate this preference through an operating system setting (e.g. light or dark mode) or a user agent setting.
Syntax
light
-
Indicates that user has notified that they prefer an interface that has a light theme, or has not expressed an active preference.
dark
-
Indicates that user has notified that they prefer an interface that has a dark theme.
Examples
The elements below have an initial color theme. They can be further themed according to the user's color scheme preference.
HTML
<div class="day">Day (initial)</div> <div class="day light-scheme">Day (changes in light scheme)</div> <div class="day dark-scheme">Day (changes in dark scheme)</div> <br> <div class="night">Night (initial)</div> <div class="night light-scheme">Night (changes in light scheme)</div> <div class="night dark-scheme">Night (changes in dark scheme)</div>
CSS
.day { background: #eee; color: black; } .night { background: #333; color: white; } @media (prefers-color-scheme: dark) { .day.dark-scheme { background: #333; color: white; } .night.dark-scheme { background: black; color: #ddd; } } @media (prefers-color-scheme: light) { .day.light-scheme { background: white; color: #555; } .night.light-scheme { background: #eee; color: black; } } .day, .night { display: inline-block; padding: 1em; width: 7em; height: 2em; vertical-align: middle; }
Result
Specifications
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
prefers-color-scheme |
76 |
79 |
67 |
No |
62 |
12.1 |
76 |
76 |
67 |
54 |
13 |
14.2 |
no-preference |
76-80 |
79-80 |
67-79 |
No |
62-71 |
12.1 |
76-80 |
76-80 |
67-79 |
54 |
13 |
14.2 |
See also
- Simulate prefers-color-scheme in Firefox (Firefox Page Inspector > Examine and edit CSS)
- Video tutorial: Coding a Dark Mode for your Website
- Redesigning your product and website for dark mode
- Changing color schemes in Windows, macOS, Android, or other platforms.
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme