User Preference Queries and Dark Mode
How prefers-color-scheme, color-scheme and light-dark() build a system-following dark theme, and how reduced-motion and contrast queries adapt a page.
User Preference Queries and Dark Mode
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- User preference queries ask about the person, not the screen: `prefers-color-scheme`, `prefers-reduced-motion`, `prefers-contrast` and `forced-colors` all reflect settings the user made in their operating system.
- `color-scheme: light dark` on `:root` tells the browser the page supports both. Built-in UI (form controls, scrollbars, the default canvas and text colors) then follows the system theme without any further CSS.
- `light-dark(a, b)` picks `a` in a light scheme and `b` in a dark one, so a theme is one declaration per color instead of a media query copy. It needs `color-scheme` to include both values.
- A manual theme toggle works by setting `color-scheme` on an element (for example `[data-theme=light] { color-scheme: light }`). That makes `light-dark()` follow the toggle instead of the system. Without it, a toggle to light on a dark-mode system fails.
- Honor `prefers-reduced-motion: reduce` by removing large movement and parallax. It is a request from people for whom motion causes dizziness or nausea.
Asking about the person
Media queries from the first lesson in this group ask about the screen. Another family asks about the person using it. Operating systems have settings for dark mode, reduced motion and higher contrast, and the browser passes them to CSS as media features. A good stylesheet treats them like another dimension of responsiveness.
| Feature | Asks | Typical response |
|---|---|---|
prefers-color-scheme: dark |
does the user want a dark theme? | swap the palette |
prefers-reduced-motion: reduce |
does motion make them unwell? | remove large movement |
prefers-contrast: more |
do they want stronger contrast? | darker text, clearer borders |
forced-colors: active |
is the OS overriding your colors (high-contrast mode)? | don’t depend on shadows or colors alone |
You can test them without touching your OS settings: Chrome and Firefox devtools can emulate each of them. All of the ones above are Baseline. prefers-reduced-data is not, so don’t count on it.
Dark mode, the old way and the new way
The original approach was to define colors as custom properties and override them in a query:
:root { --bg: white; --text: #222; }
@media (prefers-color-scheme: dark) {
:root { --bg: #111; --text: #eee; }
}
It works, and it is still the right shape for a big theme with many tokens. But there are two newer tools that remove most of the repetition.
color-scheme: tell the browser
:root { color-scheme: light dark; }
This declares that the page supports both themes. The browser then switches everything it draws itself, such as form controls, scrollbars and the page’s default background and text color, to match the user’s setting. A page with no colors set at all becomes a working dark-mode page. You can also set it in HTML with <meta name="color-scheme" content="light dark">, which applies before the CSS loads and avoids a white flash.
light-dark(): two values, one declaration
:root { color-scheme: light dark; }
body {
background: light-dark(#fff, #111);
color: light-dark(#222, #eee);
}
light-dark(a, b) returns a under a light scheme and b under a dark one, so each color is defined once, next to its use. It works with any color, including the oklch values from earlier. The function has been in every major browser since May 2024. Image versions (light-dark() for url() values) are newer and not yet widely supported.
light-dark() reads the element’s used color-scheme. Without the color-scheme: light dark declaration, the scheme is plain light, so light-dark() always returns the first value. In a test with the system set to dark, the rule with the declaration gave the second value, and a page without one kept the first.
The toggle pitfall
Many sites also offer a manual switch. The mistake is to use a class to change colors and leave the system-driven rules as they are, so the two fight. The clean fix is to drive everything from color-scheme, and let the toggle override it:
:root { color-scheme: light dark; } /* follow the system by default */
:root[data-theme="light"] { color-scheme: light; }
:root[data-theme="dark"] { color-scheme: dark; }
Every light-dark() on the page now follows the toggle, and with no data-theme the system decides. In a test with the system set to dark, an element inside [data-theme=light] got the light value while the rest of the page got the dark one. Saving the user’s choice is JavaScript’s job, usually in localStorage.
Reduced motion
Large movement can make some people feel dizzy or sick. The prefers-reduced-motion query lets you opt out:
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after {
animation-duration: 0.01ms !important;
transition-duration: 0.01ms !important;
scroll-behavior: auto !important;
}
}
That blanket reset is blunt but common. A more careful approach is to keep fades and color changes, and remove parallax, zooming and sliding. The animation lessons come back to this. Tests with Chromium’s emulation confirmed that (prefers-reduced-motion: reduce) and (prefers-contrast: more) both match when set.
Contrast and forced colors
prefers-contrast: more is a signal to strengthen borders, text color and focus rings. forced-colors: active is different: the operating system (Windows High Contrast, for example) replaces your colors with a small palette. In that mode box-shadow is removed and outlines survive, as you saw in borders, radius and outlines, so anything that conveys state should not rely on a shadow or a background alone.
What the video covers
The embed is a clip, 0:00 to 24:39, from Syntax’s “Modern CSS Theming | Light Mode / Dark Mode and More!” (April 2025, about twenty-eight minutes). The chapters in that range are: color-scheme, picture source media, light-dark, a CSS-only theme switcher, saving the user’s theme preference to localStorage, setting the picture source with the selected theme, matchMedia and prefers-color-scheme, the next-themes package, setting the data-theme attribute on page load, and avoiding prefers-color-scheme: dark. The middle section is JavaScript and React, so you can skip it if you only need CSS. The chapters after 24:39 are about color-mix() and relative colors, which belong to the colors lesson. The video does not cover reduced motion, contrast or forced colors, so those sections are the prose’s own. I could not read a transcript, so I do not describe the demos beyond the chapter titles.
Where this fits
Dark mode is a color problem first, which is why colors and color spaces comes before it. prefers-color-scheme is a media query like those in media queries and mobile-first, and the accessibility angle continues in accessible CSS. Reduced motion matters again in transitions and keyframe animations.
Check yourself
You write `color: light-dark(black, white)` and it always resolves to black, even with the OS in dark mode. What is missing?
`color-scheme: light dark` (usually on `:root`). `light-dark()` follows the element's used color scheme, and without that declaration the page's scheme is just `light`. In a test, the same rule gave blue (the dark value) once the scheme included `dark`, and stayed on the light value without it.
A site has a theme toggle, but a user whose OS is in dark mode picks "Light" and the page stays dark. Why?
The CSS is following `prefers-color-scheme` (or `light-dark()` under a `light dark` scheme), so the system setting wins. The toggle must set an override, such as `data-theme="light"` with `[data-theme=light] { color-scheme: light }`. In a test with a dark system preference, an element with `color-scheme: light` took the light value.
What does `color-scheme: light dark` do for form controls and scrollbars you haven't styled?
It lets the browser draw them in the matching theme: dark scrollbars, dark inputs and a dark default canvas under a dark system setting. It also sets default text and background colors for the page, so unstyled content isn't black on black.
Why use `@media (prefers-reduced-motion: reduce)` and what should be inside?
Some users get vestibular symptoms from motion on screen, and set this in their OS. Inside the query, remove or replace large movement: parallax, big slides and zooms, auto-playing animation. Fades or color changes are typically fine.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.