media query
A CSS rule, written @media, that applies styles only when a condition about the device or user settings is true.
In more detail
Conditions include screen width, prefers-color-scheme, prefers-contrast, forced-colors and prefers-reduced-motion. JavaScript can check the same conditions with window.matchMedia and listen for changes.
Example
In the browser: @media (min-width: 800px) targets wide viewports, and preference queries such as prefers-color-scheme and prefers-reduced-motion follow user settings. Media queries only know the viewport, not where a component sits.Related terms
- responsive breakpoint — A screen width at which a layout or font size changes, set with a CSS media query.
- prefers-color-scheme — The CSS media feature that reports whether the user has asked their operating system for a light or dark appearance.
- prefers-reduced-motion — The CSS media feature that reports whether the user has asked their operating system to minimise animation.
- forced colors mode — A browser mode, turned on by settings such as Windows Contrast Themes, that replaces a page's colours with a small palette the user chose.
- container query — A CSS rule, written with @container, that styles a component based on the size of its container instead of the whole viewport.
- viewport — The visible area of a web page in the browser window or device screen.
Taught in
- Typography & Spatial Grids: Modular Scales & Fluid Typography — Design Systems & Component Engineering
- Dark Mode Strategies: System Preference, Class & High-Contrast — Design Systems & Component Engineering
- Visual Contrast Auditing: Color Blindness & Reduced Motion — Design Systems & Component Engineering
- Advanced CSS Grid: Named Areas, minmax And auto-fit — Frontend Architecture & Web Platform
- Container Queries: Component-Driven Responsive Design — Frontend Architecture & Web Platform
- CSS Custom Properties: Scoping, Fallbacks And Theming — Frontend Architecture & Web Platform
- Modern CSS Animations, Transitions And View Transitions API — Frontend Architecture & Web Platform