Sub topic 12
Respecting user preferences: color scheme and reduced motion
This chapter assumes you can write a basic @media rule, you know CSS custom properties (--name and var()), and you understand how the cascade orders rules.
Preference queries
Width queries describe the screen. Preference queries describe the person using it. Operating systems let users choose a dark theme or turn off animation, and browsers expose those choices to CSS as media features. Your stylesheet reads them, and the user doesn't have to configure anything on your site.
| Media feature | Values | Meaning |
|---|---|---|
| `prefers-color-scheme` | `light`, `dark` | The user's preferred theme |
| `prefers-reduced-motion` | `no-preference`, `reduce` | The user wants less animation |
Color scheme
The prefers-color-scheme: dark query matches when the system asks for a dark theme.
@media (prefers-color-scheme: dark) {
body {
background: #121212;
color: #eeeeee;
}
}This works for one element. On a real site, colors appear in many rules, and repeating each one inside a query is hard to maintain. Define the colors once as custom properties and override only the properties in the query.
:root {
color-scheme: light dark;
--bg: #ffffff;
--text: #1a1a1a;
--link: #0a58ca;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #121212;
--text: #eeeeee;
--link: #6ea8fe;
}
}
body {
background: var(--bg);
color: var(--text);
}
a {
color: var(--link);
}The color-scheme: light dark declaration tells the browser that the page supports both themes. Built-in elements such as form controls and scrollbars then switch with it. Without it, a dark page can still show light scrollbars and inputs.
Reduced motion
Some people get dizziness or nausea from parallax, large sliding panels, or zooming effects. They enable "reduce motion" in their system settings. The prefers-reduced-motion: reduce query lets you respect that.
@media (prefers-reduced-motion: reduce) {
* {
animation: none !important;
transition: none !important;
}
}The !important flags here are deliberate. This rule must beat any animation declared anywhere else in the stylesheet, whatever its specificity.
A gentler approach is to treat motion as an enhancement. Apply it only when the user has no stated preference.
.panel {
opacity: 1;
}
@media (prefers-reduced-motion: no-preference) {
.panel {
transition: transform 300ms ease-out;
}
}With this approach, the safe behavior is the default. Browsers that don't support the query also get no animation.
Common mistakes
- Setting only
backgroundin a dark query. The text color stays dark and becomes unreadable. Always change foreground and background together. - Using pure
#000on pure#fff, or the reverse. Slightly softened colors are easier on the eyes. - Removing every transition when
reduceis set. Users usually object to large movement, not to a color fade. Consider keeping subtle, non-moving feedback such as color or opacity changes. - Forgetting that
*does not select pseudo-elements such as::before. Add*::before, *::afterto your selector if they animate.
Testing
Chrome and Firefox developer tools can emulate both features. In Chrome, open the Rendering panel and set "Emulate CSS media feature prefers-color-scheme" and "prefers-reduced-motion". You can check both behaviors without changing your operating system settings.
Exercise
A page sets body { background: #121212; } inside a prefers-color-scheme: dark query, but the text stays hard to read in dark mode. What is the likely cause, and how would you fix it?
Show answer
The text color was not changed, so it is still a dark color on a dark background. Set color to a light value inside the same query, or better, define background and text colors as custom properties and override both of them in the query.
Exercise
Why does the "no-preference" pattern for animation (adding transitions only inside prefers-reduced-motion: no-preference) tend to be safer than adding transitions everywhere and turning them off inside reduce?
Show answer
Motion is off by default and is added only when the user has not asked to reduce it. A browser that doesn't understand the query, or a rule you forgot to override, therefore cannot show unwanted animation.