Sub topic 12
Respecting user preferences: color scheme and reduced motion
This chapter assumes you can write basic @media rules, understand the cascade, and know CSS custom properties (variables). Width queries describe the screen. Preference queries describe the person using it. The operating system or browser exposes settings such as "dark mode" or "reduce animation," and CSS can read them.
What preference queries are
A preference query is a media feature whose value comes from a user setting, not from the device's size. Two are used most often:
prefers-color-schemereports whether the user wants a light or dark interface.prefers-reduced-motionreports whether the user has asked the system to minimize non-essential animation.
Both are part of Media Queries Level 5 and work in all current major browsers. You write them like any other media feature, and you can combine them with width queries using and, a comma, or not.
Color scheme with prefers-color-scheme
prefers-color-scheme accepts two values in practice: light and dark. A user who has not chosen anything usually matches light, because the browser treats light as the default.
The direct approach is to write the dark rules inside a query:
@media (prefers-color-scheme: dark) {
body {
background: #121212;
color: #eeeeee;
}
}This works, but it scales badly. Every component that uses a color needs its own dark override. A better pattern is to define colors once as custom properties and change only the properties.
Use custom properties for theme colors
:root {
--bg: #ffffff;
--text: #1a1a1a;
--link: #0a58ca;
--surface: #f3f4f6;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #121212;
--text: #eeeeee;
--link: #8ab4ff;
--surface: #1e1e1e;
}
}
body {
background: var(--bg);
color: var(--text);
}
a {
color: var(--link);
}
.card {
background: var(--surface);
}The query appears once. Components never mention it. Adding a new component means using the existing variables, and it gets dark mode for free.
Tell the browser which schemes you support
Custom properties only restyle the elements you style. Form controls, scrollbars, and the default page background still use light colors unless you opt in. The color-scheme property tells the browser that your page supports both schemes:
:root {
color-scheme: light dark;
}With this set, built-in widgets such as checkboxes, text inputs, and scrollbars switch automatically with the user's setting. You can also add this tag to the document head so the first paint is not a white flash:
<meta name="color-scheme" content="light dark">Do not simply invert colors
Dark mode is not a color inversion. Pure black (#000) with pure white text produces harsh contrast and can cause halation, where bright text appears to bleed on dark backgrounds. Many designs use a very dark gray background and slightly dimmed text. Saturated colors that look fine on white often vibrate on dark, so choose lighter, less saturated variants, as the --link value above does. Check contrast in both schemes. WCAG asks for at least 4.5:1 for normal text.
The light-dark() function
Modern browsers also support light-dark(), which picks a value based on the active scheme. It requires color-scheme: light dark on the element or an ancestor:
:root {
color-scheme: light dark;
}
body {
background: light-dark(#ffffff, #121212);
color: light-dark(#1a1a1a, #eeeeee);
}It is shorter, but it is newer than the media query approach. Check browser support for your audience before you rely on it, or keep the media query as the fallback.
Reduced motion with prefers-reduced-motion
Some people get dizzy, nauseous, or distracted by large animations. This includes people with vestibular disorders, migraines, or attention conditions. Operating systems let them set a "reduce motion" option. The query exposes it:
no-preferencemeans the user has not asked for less motion.reducemeans the user has asked for less motion.
The blunt approach
A common snippet disables all animation:
@media (prefers-reduced-motion: reduce) {
* {
animation: none !important;
transition: none !important;
}
}It is quick and often better than nothing. It also has costs. Removing a transition can break behavior that depends on the transitionend or animationend events. Removing all animation also removes helpful feedback, such as a progress spinner that tells the user something is loading.
The better approach: motion is opt-in
Reduced motion does not mean no motion. It means no unnecessary, large, or sudden movement. A more careful pattern writes animations only for users who have not asked for less:
.panel {
opacity: 1;
}
@media (prefers-reduced-motion: no-preference) {
.panel {
transition: transform 300ms ease, opacity 300ms ease;
}
.panel.is-entering {
transform: translateY(2rem);
opacity: 0;
}
}Users with the reduce setting see the panel appear without sliding. Users with no preference get the animation. Because the default state has no motion, you cannot forget to remove it later.
Replace instead of remove
When motion carries meaning, replace it with something gentler. A parallax or zoom effect can become a simple fade. A spinner can stay, but a bouncing or spinning decorative icon can go.
.toast {
animation: slide-in 400ms ease-out;
}
@keyframes slide-in {
from { transform: translateX(100%); }
to { transform: translateX(0); }
}
@keyframes fade-in {
from { opacity: 0; }
to { opacity: 1; }
}
@media (prefers-reduced-motion: reduce) {
.toast {
animation-name: fade-in;
animation-duration: 200ms;
}
}Smooth scrolling is another case. Set it only when motion is acceptable:
@media (prefers-reduced-motion: no-preference) {
html {
scroll-behavior: smooth;
}
}Combining preference queries with other queries
Preference queries combine with the operators from earlier chapters. This rule applies only to dark mode on narrow screens:
@media (prefers-color-scheme: dark) and (max-width: 40em) {
.hero {
background-image: none;
}
}Keep this kind of combination rare. Each combined query multiplies the states you must test.
Where the preference comes from
The browser re-evaluates these queries continuously. If the user switches to dark mode while your page is open, the styles update without a reload. Scheduled "dark at night" settings trigger the same change.
Common errors
| Mistake | Result | Fix |
|---|---|---|
| Overriding colors on every component in the dark query | Large, hard-to-maintain stylesheet | Change custom properties on `:root` |
| Forgetting `color-scheme` | Light form controls and scrollbars on a dark page | Set `color-scheme: light dark` |
| Using `*` with `animation: none !important` and relying on `animationend` | Scripts that wait for events never continue | Replace motion with short fades, or scope the rule |
| Testing only your own settings | Bugs for users with the opposite setting | Emulate both values in developer tools |
| Using `prefers-color-scheme: light` for the base styles | Users with no signal may get unexpected results | Write light as the default and override for dark |
| Treating the query as a manual theme switch | Users cannot override the system choice | Offer a toggle that sets a class or `data-theme` attribute, and let it take priority |
Testing preferences
In Chrome or Edge DevTools, open the Rendering panel and find "Emulate CSS media feature prefers-color-scheme" and "Emulate CSS media feature prefers-reduced-motion." Firefox offers color scheme buttons in the Inspector toolbar. Toggle each value and check every major page state, including hover, focus, error, and empty states. Also test with the real operating system setting once, because it confirms that the browser reads it as expected.
Summary of decisions
- Define theme colors as custom properties and override them once in a
prefers-color-scheme: darkquery. - Declare
color-scheme: light darkso built-in controls match. - Write motion inside
prefers-reduced-motion: no-preference, or replace large movement with fades underreduce. - Test both values of each feature, and provide a manual override if your product needs one.
Exercise
A page defines --bg and --text on :root and overrides them in @media (prefers-color-scheme: dark). In dark mode, the page background is dark, but text inputs and the scrollbar are still light. What is missing, and how would you fix it?
Show answer
The page never told the browser it supports both schemes, so built-in controls keep their default light appearance. Add color-scheme: light dark; to :root (and optionally <meta name="color-scheme" content="light dark"> in the head). Browsers then render form controls and scrollbars to match the active scheme.
Exercise
You have a card that slides up 3rem when it enters the viewport using a CSS transition. Write a rule set so the slide happens only for users without a reduced-motion preference, and explain why this is safer than adding a reduce override afterwards.
Show answer
Put the transition and the offset inside @media (prefers-reduced-motion: no-preference) { .card { transition: transform 300ms ease, opacity 300ms ease; } .card.is-entering { transform: translateY(3rem); opacity: 0; } }. Users who request reduced motion get the default state with no movement. It is safer because motion is added only when permitted, so a new animation cannot reach reduced-motion users unless someone deliberately places it outside the query.