π What Is color-scheme?
The color-scheme property tells the browser which **color themes** (light, dark, or both) your component or site is designed to support. This enables the browser to automatically adjust built-in UI elements such as:
- β Form controls
- β Scrollbars
- β Input fields
- β Dialogs & widgets
Note
β Works with system preferences β prefers-color-scheme
β Applies to UI controls, not just text
π¦ Syntax
color-scheme syntax
color-scheme: normal | light | dark | light dark;| Value | Description |
|---|---|
| normal | No special theme support (default). |
| light | Component supports only light mode. |
| dark | Component supports only dark mode. |
| light dark | Supports both modes β browser picks based on user preference. |
π 1. Enable Dark & Light Mode Automatically
Both modes
:root {
color-scheme: light dark;
}Allows browser UI to match the user's system theme automatically.
π 2. A Component That Supports Only Dark Mode
Dark UI only
.dark-widget {
color-scheme: dark;
}Browser renders scrollbars, inputs, and form controls using dark styles.
π 3. Force Light Appearance for a Section
Light only
.light-section {
color-scheme: light;
}Ensures UI controls use light styling even in dark mode.
π 4. Combine With prefers-color-scheme
System theme support
@media (prefers-color-scheme: dark) {
body {
background: #111;
color: #eee;
}
}
@media (prefers-color-scheme: light) {
body {
background: #fff;
color: #222;
}
}Let CSS detect the user's system preference and theme the page accordingly.
π 5. Improve Form Input Styling Automatically
Form controls
form {
color-scheme: light dark;
}Browser-native controls (checkboxes, radios, selects) automatically adapt.
π 6. Use Inside Shadow DOM Components
Shadow DOM
:host {
color-scheme: dark;
}Ensures web components render UI consistent with their internal theme.
π§ͺ Real-World Examples
1οΈβ£ Whole Site Supports Light & Dark Modes
Site-wide theme support
html {
color-scheme: light dark;
}2οΈβ£ Dashboard Widgets With Forced Dark Theme
Dashboard
.widget {
background: #1e1e1e;
color: #fafafa;
color-scheme: dark;
}3οΈβ£ Modal or Card That Must Stay Light Even in Dark Mode
Force light mode
.modal {
background: white;
color: black;
color-scheme: light;
}β οΈ Common Mistakes
- β Assuming color-scheme changes your pageβs background β it doesnβt
- β Forgetting that it affects only system-drawn UI parts
- β Using dark-only UI while your text remains light β contrast issues
Note
π₯ Summary
The color-scheme property helps developers create seamless light/dark mode experiences by letting the browser automatically style built-in UI parts like scrollbars, inputs, and form controls. It improves accessibility, consistency, and user experience.
Learn more βMDN Docs π