Sub topic 2
Following the System Setting with prefers-color-scheme
This chapter assumes you know basic HTML and CSS, including selectors and the cascade, and that you can read hex color values. You don't need JavaScript yet.
What the media query does
Operating systems and browsers let users choose a light or dark appearance. The prefers-color-scheme media query lets your CSS read that choice. A media query is a condition attached to a block of CSS rules. The browser applies the rules only while the condition is true.
The query accepts two values you will use in practice:
| Value | Matches when |
|---|---|
| `light` | The user prefers a light theme, or has not stated a preference |
| `dark` | The user has chosen a dark theme in their system or browser |
Browsers re-evaluate the query live. If the user switches their system to dark mode while your page is open, the dark rules take effect without a reload.
A first working example
The simplest pattern is to write your light styles as the default. You then override them inside a dark media query. This example stores colors in custom properties, which are named values such as --bg that you read with var(). The next chapter covers them in depth. Here they simply keep the override short.
:root {
--bg: #ffffff;
--text: #1a1a1a;
--link: #0b5fff;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #121212;
--text: #e6e6e6;
--link: #8ab4ff;
}
}
body {
background: var(--bg);
color: var(--text);
}
a {
color: var(--link);
}Only the three variable values change inside the media query. The rules for body and a are written once and work for both themes.
Light is the default for a reason. A browser that doesn't support the query, or a user with no preference, gets a readable light page. Dark is an enhancement layered on top.
Why the order matters
The media query block adds no specificity. :root inside @media has the same specificity as the plain :root rule. The cascade therefore resolves the conflict by source order, and the later rule wins. If you put the media query before the default :root block, the light values override the dark ones and the dark theme never appears.
/* Wrong: the later light rule overrides the dark one. */
@media (prefers-color-scheme: dark) {
:root {
--bg: #121212;
}
}
:root {
--bg: #ffffff;
}Always write the default first and the dark override after it.
Overriding properties directly
You can also set properties inside the query without custom properties. This works for a single rule, such as softening photos on a dark background:
@media (prefers-color-scheme: dark) {
img.photo {
filter: brightness(0.85);
}
}For whole-page themes, variables scale better. With direct overrides you must repeat every selector inside the media query, and the light and dark versions drift apart over time.
Common errors
- Dark rules never apply. Check the source order, as described above. Also check that your system or browser is actually set to dark.
- Only some elements change. Colors that you hard-coded on a selector, such as
color: #333on.card, don't use the variables. Replace them withvar()references. - Form controls and scrollbars stay light. The media query changes only the styles you write. Built-in controls need the
color-schemeproperty, which a later chapter covers. - A typo in the query.
prefers-color-scheme: darkmodeis not a valid value and never matches. Usedarkorlight.
Checking the result
Open your page and change the system appearance. On macOS, use System Settings, then Appearance. On Windows, use Settings, then Personalization, then Colors. The page should switch immediately. Browser developer tools can also emulate the setting, so you don't have to change your system. A later chapter shows how.
Exercise
Your page has :root { --bg: #fff; } followed by @media (prefers-color-scheme: dark) { :root { --bg: #000; } }. A teammate moves the :root block below the media query. The user's system is set to dark. What background color does the page get, and why?
Show answer
White (#fff). The :root rule and the :root rule inside the media query have equal specificity, so the cascade uses source order. The plain :root block now comes last and overrides the dark value, even though the media query matches.