color-scheme - CSS | MDN
Try it
<section class="default-example container" id="default-example">
<textarea id="example-element">Text Area</textarea>
</section>
#example-element {
width: 80%;
height: 50%;
}
Syntax
css
color-scheme: normal;
color-scheme: light;
color-scheme: dark;
color-scheme: light dark;
color-scheme: only light;
/* Global values */
color-scheme: inherit;
color-scheme: initial;
color-scheme: revert;
color-scheme: revert-layer;
color-scheme: unset;
The color-scheme property's value must be one of the following keywords.
Values
normal-
Indicates that the element can be rendered using the page's color scheme settings. If the page does not have a color scheme set, the element is rendered using the page's default color settings.
light-
Indicates that the element can be rendered using the operating system light color scheme.
dark-
Indicates that the element can be rendered using the operating system dark color scheme.
only-
Forbids the user agent from overriding the color scheme for the element.
Can be used to turn off color overrides caused by Chrome's Auto Dark Theme, by applying
color-scheme: only light;on a specific element or:root.
Formal definition
| Initial value | normal |
|---|---|
| Applies to | all elements and text |
| Inherited | yes |
| Computed value | as specified |
| Animation type | discrete |
Formal syntax
color-scheme =
normal |
[ light | dark | <custom-ident> ]+ && only?
Examples
Declaring color scheme preferences
To opt the entire page into the user's color scheme preferences, declare color-scheme on the :root element.
css
:root {
color-scheme: light dark;
}
To opt in specific elements to the user's color scheme preferences, declare color-scheme on those elements.
css
header {
color-scheme: only light;
}
main {
color-scheme: light dark;
}
footer {
color-scheme: only dark;
}
Along with the above CSS, also include the <meta name="color-scheme"> HTML <meta> tag in the <head>, before any CSS style information, to inform user agents about the preferred color scheme, helping prevent unwanted screen flashes during the page load.
Styling based on color schemes
To style elements based on color scheme preferences, use the prefers-color-scheme media query. The example below opts in the entire page to using both light and dark operating system color schemes via the color-scheme property, and then uses prefers-color-scheme to specify the desired foreground and background colors for individual elements in those color schemes.
css
:root {
color-scheme: light dark;
}
@media (prefers-color-scheme: light) {
.element {
color: black;
background-color: white;
}
}
@media (prefers-color-scheme: dark) {
.element {
color: white;
background-color: black;
}
}
Alternatively, use the light-dark() <color> function to set the foreground and background colors for the different color schemes using a more compact code structure:
css
:root {
color-scheme: light dark;
}
.element {
color: light-dark(black, white);
background-color: light-dark(white, black);
}
Specifications
| Specification |
|---|
| CSS Color Adjustment Module Level 1 # color-scheme-prop |
Browser compatibility
See also
prefers-color-schememedia query to detect user preferences for color schemes.light-dark()color function to set colors for both light and dark color schemes.- Other color-related properties:
color,accent-color,background-color,border-color,outline-color,text-decoration-color,text-emphasis-color,text-shadow,caret-color, andcolumn-rule-color background-imageprint-color-adjust- Using relative colors