
CSS Properties and Values: Reference and Cheat Sheet
02/08/2023
How to Create CSS Tooltips
09/08/2023
Build a light and dark theme using HTML, CSS, and JavaScript. The CSS media feature prefers-color-scheme lets the site follow the browser’s reported preference.
First we will use CSS to choose a theme automatically. Then the demo adds a button so people can switch themes manually.
1. What is prefers-color-scheme?
prefers-color-scheme is a media feature that reports whether a user prefers a light or dark color scheme. The preference often comes from the operating system or browser.
Define CSS rules for the two schemes. You can adapt colors for the background, text, controls, and other parts of the site.
@media (prefers-color-scheme: light) {
/* Light theme styles */
body {
background-color: #fff;
color: #222;
}
}
@media (prefers-color-scheme: dark) {
/* Dark theme styles */
body {
background-color: #222;
color: #fff;
}
}
2. How to use prefers-color-scheme
Use the media feature inside an query, as shown below. Provide a base theme so the page remains usable if the query is unavailable.
media
/* Default light theme */
body {
background-color: white;
color: black;
}
/* Dark theme styles */
@media (prefers-color-scheme: dark) {
body {
background-color: black;
color: white;
}
}
With these styles, the page follows the dark scheme when the browser reports that preference.
3. Switch themes manually in CodePen
The demo follows the reported preference initially and uses a button and JavaScript to let users choose a different scheme.
4. Things to consider
- Compatibility: Check the browsers your project supports and verify that the base colors work if a browser ignores the query.
- User choice: A theme switch lets visitors override the system setting for the current site. Give the control a clear label and indicate its state.
- Images and interface details: Check images, shadows, borders, form controls, and focus indicators in both themes. Make sure text has enough contrast.
5. Advantages of a light theme
- Legibility: Some readers find dark text on a light background easier to read, especially in bright conditions.
- Familiarity: A light background resembles text on paper and may suit users accustomed to that presentation.
- Visual design: Some color palettes and images work particularly well in a light theme.
6. Advantages of a dark theme
- Comfort: Some readers find a dark interface more comfortable in dim surroundings. Preferences vary.
- Power use: On an OLED display, dark pixels can consume less power. The difference depends on brightness, displayed colors, and hardware.
- Focus: A dark palette can help some readers concentrate when the interface has strong, well-chosen contrast.
- Appearance: Some people prefer the look of a dark interface, especially for applications they use for long periods.
What is an OLED display? OLED stands for organic light-emitting diode. Its pixels emit their own light, so a black pixel can be switched off. Conventional LCDs illuminate pixels with a backlight.
Offer both schemes when that fits the site. Start from the visitor’s preference and test each theme with real content and controls.