Sub topic 12
Respecting user preferences
This chapter assumes you can write media queries and know how the cascade resolves conflicting rules. You should also be comfortable using browser developer tools.
Screen width is only one thing that varies between users. People also set preferences in their operating system or browser: reduced motion, a dark color scheme, higher contrast, larger text. CSS exposes these as media features, so your stylesheet can respond to them with no JavaScript.
How preference queries work
A preference query looks like a width query, but it asks about the user's settings instead of the screen. The browser reads the setting from the operating system and matches the query when the setting is on.
The mobile-first habit carries over. Write the default experience first, then add or remove behavior when a preference asks for it.
Reduced motion
Some people get dizzy or nauseous from large movement on screen, such as sliding panels and parallax effects. They can ask their operating system to reduce motion, and prefers-reduced-motion reports that choice.
.banner {
animation: slide-in 400ms ease-out;
}
@media (prefers-reduced-motion: reduce) {
.banner {
animation: none;
}
}The banner animates by default. Users who asked for less motion get the banner with no animation. Do not remove the content itself. Only the movement should go.
Color scheme
prefers-color-scheme reports whether the user prefers a light or dark interface. A clean way to support both is to store colors in custom properties and change only their values.
:root {
color-scheme: light dark;
--bg: #ffffff;
--text: #1a1a1a;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #121212;
--text: #eeeeee;
}
}
body {
background: var(--bg);
color: var(--text);
}The color-scheme property tells the browser that the page supports both schemes. Built-in elements such as form controls and scrollbars then match the active scheme too. The rest of your CSS reads var(--bg) and var(--text) and never needs its own dark-mode rules.
Other preference features
Several more features follow the same pattern.
| Media feature | What it reports | Typical response |
|---|---|---|
| `prefers-contrast: more` | The user wants stronger contrast | Darker text, solid borders |
| `prefers-reduced-data: reduce` | The user wants to save data | Skip large decorative images |
| `forced-colors: active` | The browser enforces a limited palette | Rely on borders, not background color alone |
Support varies by browser. prefers-reduced-data in particular is not widely implemented, so treat it as an optional improvement and never as a requirement for the page to work.
Common mistakes
- Treating preferences as a replacement for width queries. They answer a different question. A phone and a desktop can both ask for dark mode.
- Disabling every transition. Reduced motion does not mean no feedback. A short fade or color change is usually fine. Remove large movement, scaling and parallax.
- Hard-coding colors across many rules. If you do, dark mode means duplicating every rule. Custom properties keep the change in one place.
- Forgetting to test. The preference only matches when the setting is on, so the code path is never exercised by default.
Testing preferences
You do not need to change your operating system to test. In Chrome DevTools, open the Rendering panel and use the emulation options for prefers-color-scheme and prefers-reduced-motion. Firefox offers color scheme and reduced motion toggles in the Inspector. Check both states, and then check them on a real device at least once.
Exercise
Your stylesheet animates a hero image with a 600 ms zoom. Write the media query that removes the zoom for users who asked for reduced motion, and explain why you would keep the hero image visible.
Show answer
Use @media (prefers-reduced-motion: reduce) { .hero { animation: none; } }. The preference asks to remove movement, not content. The image carries information and design, so it must still be shown. Only the zoom animation is dropped.
Exercise
A dark-mode stylesheet repeats background and color declarations for 40 selectors inside a prefers-color-scheme: dark block. What change reduces this to one small block, and what does color-scheme: light dark add?
Show answer
Store the colors in custom properties on :root, use var() in the 40 selectors, and override only the property values inside the dark media query. color-scheme: light dark tells the browser the page supports both schemes, so built-in controls and scrollbars match the active scheme automatically.