Sub topic 12
Respecting user preferences
Users can tell their operating system or browser how they want pages to look and behave. CSS can read those settings through media features. This chapter covers the main ones, how they fit into a mobile-first stylesheet, and the mistakes that cause trouble.
This chapter assumes you know how media queries work, can use CSS custom properties, and can open browser developer tools. It also assumes you have read the earlier chapters on base styles and min-width queries.
Why preferences are a separate axis
Width queries answer "how much room is there?" Preference queries answer "what does this person need or want?" The two are independent. A phone and a desktop can both be set to dark mode, and a narrow window can belong to someone who needs reduced motion.
This matters for stylesheet structure. Mobile-first CSS layers wider layouts on top of a small-screen base. Preference queries add a second layer that you can write anywhere, because it changes how things look, not where they sit. Keep the two kinds of query separate so you can tell which one is responsible for a given rule.
The last arrow in the diagram is easy to forget. Media queries re-evaluate live, so a user who switches to dark mode at sunset sees your page update without a reload.
Reduced motion
prefers-reduced-motion reports whether the user asked the system to minimize non-essential movement. Large slides, parallax, and zooming can cause nausea or dizziness for people with vestibular disorders. The query has two values: no-preference and reduce.
You can write it in two ways. The first starts with motion and removes it on request:
.banner {
animation: slide-in 400ms ease-out;
}
@media (prefers-reduced-motion: reduce) {
.banner {
animation: none;
}
}The second starts with no motion and adds it only for users who have not asked to reduce it:
@media (prefers-reduced-motion: no-preference) {
.banner {
animation: slide-in 400ms ease-out;
}
}The second form is the same idea as mobile-first: begin with the safest baseline and add. A browser that does not understand the query simply gets no animation. The first form is easier to retrofit onto an existing stylesheet, because you only add an override block.
Reduce, don't always remove
"Reduced" does not mean "none". Fades and color changes are usually fine. Remove or replace large-scale movement such as sliding across the screen, spinning, and scroll-linked parallax. A common substitution is to swap a slide for a short opacity fade.
@keyframes fade-in {
from { opacity: 0; }
to { opacity: 1; }
}
.banner {
animation: slide-in 400ms ease-out;
}
@media (prefers-reduced-motion: reduce) {
.banner {
animation: fade-in 200ms linear;
}
}Smooth scrolling is another motion source:
html {
scroll-behavior: smooth;
}
@media (prefers-reduced-motion: reduce) {
html {
scroll-behavior: auto;
}
}Color scheme
prefers-color-scheme reports light or dark. Custom properties make this simple: define the palette once, override the values in one block, and keep every other rule unchanged.
:root {
color-scheme: light dark;
--bg: #ffffff;
--text: #1a1a1a;
--link: #0b5cd5;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #14161a;
--text: #e8e8e8;
--link: #8ab4ff;
}
}
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 schemes. The browser then themes the parts you did not style, such as form controls, scrollbars, and the default page background. Without it, a dark page can show a bright white text input.
Check your dark values for contrast on their own. Pure white text on a pure black background can look harsh, and a link color that passes contrast checks on white often fails on a dark background. That is why the example uses a lighter link color in the dark block.
Contrast and forced colors
Two more features cover users who need stronger visual separation.
prefers-contrasttakesno-preference,more,less, orcustom. Users who choosemorewant clearer boundaries and stronger text contrast.forced-colors: activemeans the browser is replacing your colors with a limited palette, as in Windows High Contrast mode.
@media (prefers-contrast: more) {
:root {
--text: #000000;
--bg: #ffffff;
}
.card {
border: 2px solid currentColor;
}
}
@media (forced-colors: active) {
.button {
border: 2px solid ButtonText;
}
}Two cautions apply. First, order matters. If a dark-mode block and a high-contrast block set the same variable, the later one wins, so decide which should take priority and place it last. For users who want both dark mode and more contrast, you may need a combined query such as @media (prefers-color-scheme: dark) and (prefers-contrast: more).
Second, in forced-colors mode the browser overrides most color properties, including backgrounds. A button that is distinguished only by its background color can lose its visible shape. Borders and outlines survive, so give controls a real border. Do not use forced-color-adjust: none to fight the mode unless you have a specific, tested reason.
Reduced data and other newer features
prefers-reduced-data is meant to signal that the user wants to save bandwidth, for example to skip large background images. Browser support is very limited and it is not available by default in most browsers. Treat it as a progressive enhancement and never as the only mechanism that keeps a page light. For real savings, rely on the techniques from the performance chapter, such as srcset and lazy loading.
prefers-reduced-transparency is similar: useful where it works, but not yet something to depend on. Check current browser support before using either one.
Reading preferences from JavaScript
Sometimes the decision is not styling. A video carousel might choose not to autoplay, or a charting library might disable its entrance animation. matchMedia gives JavaScript the same information as the CSS query.
const query = window.matchMedia("(prefers-reduced-motion: reduce)");
function applyMotionPreference() {
document.documentElement.dataset.motion = query.matches ? "reduced" : "full";
}
query.addEventListener("change", applyMotionPreference);
applyMotionPreference();Listening for change keeps the page correct if the user flips the setting while it is open. Prefer CSS where you can, and use this only when behavior, not appearance, depends on the preference.
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| Putting `prefers-reduced-motion` rules before the animation they override | The later base rule wins and the preference is ignored | Place preference blocks after the rules they modify |
| Omitting `color-scheme` | Form controls and scrollbars stay light on a dark page | Declare `color-scheme: light dark` on `:root` |
| Hard-coding colors in many rules | Dark mode needs edits everywhere | Use custom properties for the palette |
| Using background color alone to show button boundaries | Controls look flat in forced-colors mode | Add a border or outline |
| Hiding content when the preference is on | Users with the setting lose information | Change presentation, not content |
| Relying on `prefers-reduced-data` | No effect in most browsers | Use it only as an extra |
Testing preferences
You do not need to change your operating system to test these. In Chrome and Edge DevTools, open the Rendering panel (from the command menu) and use the emulation fields for prefers-color-scheme, prefers-reduced-motion, prefers-contrast, and forced-colors. Firefox offers color-scheme and reduced-motion toggles in the Inspector. Safari has similar overrides in its Web Inspector.
Emulation is a good first check. Before release, also flip the real setting on at least one device, because system-level features such as forced colors can behave differently from emulation.
Exercise
Your site has a hero image that zooms slowly in the background using a 20-second CSS animation. A user reports that it makes them feel dizzy, and they say they have "reduce motion" turned on in their operating system. Write the CSS change that fixes this, and explain whether you would remove the animation entirely or the other way around (start without it and add it conditionally). Give one reason for your choice.
Show answer
Wrap the animation in an opt-in query so it only runs for users without a reduce preference: @media (prefers-reduced-motion: no-preference) { .hero { animation: slow-zoom 20s ease-in-out infinite alternate; } } Alternatively, keep the animation in the base rule and add @media (prefers-reduced-motion: reduce) { .hero { animation: none; } } after it. The opt-in form is the safer default. Browsers that do not support the query, and users who have not told the system anything, get the calm baseline, and motion is an addition, in the same way mobile-first adds layout. The override form is faster to retrofit. Either way, the preference block must come after the rule it changes, or the base animation rule wins. Since this is decorative motion, nothing else is lost by removing it.