prefers-color-scheme
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.
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.
Embedded elements
For SVG and iframes, prefers-color-scheme
lets you set a CSS style for the SVG or iframe based on the color-scheme
of the parent element in the web page.
SVGs must be used embedded (i.e., <img src="circle.svg" alt="circle" />
) as opposed to inlined in HTML.
An example of using prefers-color-scheme
in SVGs can be found in the "Inherited color scheme in embedded elements" section.
Using prefers-color-scheme
is allowed in cross-origin <svg>
and <iframe>
elements. Cross-origin elements are elements retrieved from a different host than the page that is referencing them.
To learn more about SVGs, see the SVG documentation and for more information about iframes, see the iframe documentation.
Syntax
Examples
Detecting a dark or light theme
A common usage is to use a light color scheme by default, and then use prefers-color-scheme: dark
to override the colors to a darker variant. It is also possible to do it the other way around.
This example shows both options: Theme A uses light colors, but can be overridden to dark colors. Theme B uses dark colors, but can be overridden to light colors. In the end, if the browser supports prefers-color-scheme
, both themes will be light or dark.
HTML
<div class="box theme-a">Theme A (initial)</div>
<div class="box theme-a adaptive">Theme A (changed if dark preferred)</div>
<br />
<div class="box theme-b">Theme B (initial)</div>
<div class="box theme-b adaptive">Theme B (changed if light preferred)</div>
CSS
Theme A (brown) uses a light color scheme by default, but will switch to a dark scheme based on the media query:
.theme-a {
background: #dca;
color: #731;
}
@media (prefers-color-scheme: dark) {
.theme-a.adaptive {
background: #753;
color: #dcb;
outline: 5px dashed #000;
}
}
Theme B (blue) uses a dark color scheme by default, but will switch to a light scheme based on the media query:
.theme-b {
background: #447;
color: #bbd;
}
@media (prefers-color-scheme: light) {
.theme-b.adaptive {
background: #bcd;
color: #334;
outline: 5px dotted #000;
}
}
Result
The left boxes shows Theme A and Theme B as they would appear without the prefers-color-scheme
media query. The right boxes show the same themes, but one of them will be changed to a darker or lighter variant based on the user's active color scheme. The outline of one box will be dashed or dotted if it was changed based on your browser or operating systems settings.
Inherited color scheme in embedded elements
The following example shows how to use the prefers-color-scheme
media feature in an embedded element to inherit a color scheme from a parent element.
A script is used to set the source of the <img>
elements and their alt
attributes. This would normally be done in HTML as <img src="circle.svg" alt="circle" />
.
You should see three circles, with one drawn in a different color.
The first circle inherits the color-scheme
from the OS and can be toggled using the system OS's theme switcher.
The second and third circles inherit the color-scheme
from the embedding element; the @media
query allows setting styles of the SVG content based on the parent element's color-scheme
.
In this case, the parent element with a color-scheme
CSS property is a <div>
.
<div>
<img />
</div>
<div style="color-scheme: light">
<img />
</div>
<div style="color-scheme: dark">
<img />
</div>
<!-- Embed an SVG for all <img> elements -->
<script>
for (let img of document.querySelectorAll("img")) {
img.alt = "circle";
img.src =
"data:image/svg+xml;base64," +
window.btoa(`
<svg width="32" height="32" viewBox="0 0 32 32" xmlns="http://www.w3.org/2000/svg">
<style>
:root { color: blue }
@media (prefers-color-scheme: dark) {
:root { color: purple }
}
</style>
<circle fill="currentColor" cx="16" cy="16" r="16"/>
</svg>
`);
}
</script>
Specifications
Specification |
---|
Media Queries Level 5 # prefers-color-scheme |
Browser compatibility
BCD tables only load in the browser
See also
color-scheme
CSS propertySec-CH-Prefers-Color-Scheme
HTTP Header User Agent Client Hint- 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.