# Contrast Master reference: User Preference Media Queries (`prefers-*`) Part of the `contrast-master` skill. Read this only when the task reaches these sections. ## User Preference Media Queries (`prefers-*`) Modern CSS provides media queries that detect user preferences at the OS level. WCAG does not require honouring these queries as such, but they are a practical way to meet criteria such as 2.3.3 Animation from Interactions (AAA), and they make interfaces genuinely adaptive. ### `prefers-reduced-motion` (WCAG 2.3.3) The basic pattern is in SKILL.md. Additional guidance: - Do NOT remove animations entirely if they convey meaning (e.g., a loading spinner). Instead, simplify them (crossfade instead of slide, instant instead of eased). - Scroll-triggered animations, parallax effects, and auto-advancing carousels must all be disabled. - JavaScript: check `window.matchMedia('(prefers-reduced-motion: reduce)').matches` before starting JS-driven animations. - Frameworks: Motion for React (formerly Framer Motion) supports ``. CSS-based animation libraries should be wrapped in the media query. ```js const prefersReducedMotion = window.matchMedia( '(prefers-reduced-motion: reduce)' ).matches; if (!prefersReducedMotion) { element.animate([/* keyframes */], { duration: 300 }); } ``` ### `prefers-contrast` (related: 1.4.3, 1.4.11) Users who need higher contrast set this in their OS (macOS "Increase contrast", Windows "Contrast themes"). Respect it. Values: `more` | `less` | `custom` | `no-preference` ```css /* Increase border and text contrast for users who request it */ @media (prefers-contrast: more) { :root { --border-color: #000000; --text-secondary: #1a1a1a; /* Upgrade from gray to near-black */ --bg-subtle: #f5f5f5; /* Lighten subtle backgrounds */ } /* Make borders more prominent */ button, input, select, textarea { border: 2px solid #000000; } /* Remove semi-transparent overlays */ .overlay { background-color: #000000; opacity: 1; } } /* Some users prefer lower contrast (e.g., light sensitivity) */ @media (prefers-contrast: less) { :root { --text-primary: #333333; --bg-primary: #f0f0f0; } } ``` Key rules: - `prefers-contrast: more` - eliminate subtle grays, increase border weight, remove transparency - `prefers-contrast: less` - soften harsh black-on-white, but never drop below 4.5:1 for text - Semi-transparent backgrounds (`rgba()`, `opacity < 1`) should become opaque under `more` - Gradient text and low-contrast placeholder text should be fixed under `more` ### `prefers-color-scheme` (WCAG 1.4.3, 1.4.11) Dark mode is a preference, not just a design trend. Users with light sensitivity, migraines, or low vision may depend on it. ```css /* Light mode defaults */ :root { --bg: #ffffff; --text: #1a1a1a; --link: #0066cc; } /* Dark mode overrides */ @media (prefers-color-scheme: dark) { :root { --bg: #121212; --text: #e0e0e0; --link: #6db3f2; } } ``` Key rules: - Re-check EVERY contrast ratio in dark mode. Inverting colors does not preserve ratios. - Dark mode backgrounds should not be pure black (`#000000`). Use `#121212` to `#1e1e1e` to reduce halation for users with astigmatism. - Avoid pure white text on dark backgrounds for body text - use `#e0e0e0` to `#f0f0f0`. - Shadows are invisible on dark backgrounds - use borders or lighter backgrounds for elevation. - Status colors (red, green, amber) often need different shades in dark mode to maintain contrast. - Test with both OS-level dark mode AND any in-app theme toggle. ### `forced-colors` (Windows High Contrast / Contrast Themes) Windows High Contrast Mode (now called "Contrast Themes") overrides all colors with a system-defined palette. This is NOT the same as `prefers-contrast: more`. The browser applies `forced-colors: active` and replaces your colors entirely. ```css @media (forced-colors: active) { /* The browser replaces your colors, but you may need to fix layout */ /* Borders that were invisible (matching background) become visible */ /* Custom focus indicators may be overridden - verify they still work */ /* Use system colors for intentional styling */ .custom-button { border: 2px solid ButtonText; background: ButtonFace; color: ButtonText; } /* SVG icons may become invisible - use currentColor */ svg { fill: currentColor; } /* Decorative backgrounds/gradients are removed - use borders instead */ .card { border: 1px solid CanvasText; } /* Ensure custom checkboxes/radios remain visible */ input[type="checkbox"]::before { forced-color-adjust: none; /* Only if you handle all states manually */ } } ``` System color keywords to use when `forced-colors: active`: - `Canvas` - page background - `CanvasText` - page text - `LinkText` - link color - `VisitedText` - visited link - `ActiveText` - active link - `ButtonFace` - button background - `ButtonText` - button text - `Field` - input background - `FieldText` - input text - `Highlight` - selected item background - `HighlightText` - selected item text - `GrayText` - disabled text - `Mark` / `MarkText` - highlighted (find-on-page) text Key rules: - Never use `forced-color-adjust: none` globally. Only apply it to specific elements where you manually manage every color state. - Custom UI controls built from `
` and `` often become invisible. Use semantic HTML (`