CSS Media Queries, Tested Live on Your Device
A media query applies CSS only when a condition about the device or the user's settings is true. @media (width >= 48rem) { ... } targets wider screens; prefers-color-scheme, prefers-reduced-motion, hover and pointer describe settings and input. Write the small-screen styles first, then add min-width queries for larger layouts.
| Goal | Query |
|---|---|
| Tablet and up | @media (width >= 48rem) |
| Phones only | @media (width < 40rem) |
| Dark mode | @media (prefers-color-scheme: dark) |
| Less animation | @media (prefers-reduced-motion: reduce) |
| Mouse users | @media (hover: hover) and (pointer: fine) |
| Printing | @media print |
Updated
The panel below runs sixteen queries through window.matchMedia on your own browser and updates as you resize the window, rotate a phone or switch dark mode. Under it: the range syntax, user preference queries, input queries, framework breakpoints, and when a container query is the better tool.
Queries tested by the live panel
| Group | Query | Matches when |
|---|---|---|
| Width | (width < 40rem) | Narrower than Tailwind sm (640px at default font size) |
| Width | (40rem <= width < 64rem) | Between Tailwind sm and lg |
| Width | (width >= 64rem) | Tailwind lg and up |
| Width | (min-width: 768px) | Classic tablet breakpoint, old syntax |
| Orientation | (orientation: portrait) | Viewport taller than wide |
| Orientation | (orientation: landscape) | Viewport wider than tall |
| Input | (hover: hover) | Main input can hover (mouse, trackpad) |
| Input | (pointer: coarse) | Main input is imprecise (finger) |
| Input | (pointer: fine) | Main input is precise (mouse, stylus) |
| Input | (any-hover: hover) | At least one input can hover |
| Preference | (prefers-color-scheme: dark) | System set to dark mode |
| Preference | (prefers-reduced-motion: reduce) | User asked for less motion |
| Preference | (prefers-contrast: more) | User asked for more contrast |
| Display | (min-resolution: 2dppx) | High density screen, 2x or more |
| Display | (display-mode: standalone) | Running as an installed web app |
| Display | Printing or print preview |
Common breakpoint sets
Values from the official Tailwind CSS v4 and Bootstrap 5 documentation. Tailwind's are in rem, shown here at the default 16px.
| Name | Tailwind v4 | Bootstrap 5 |
|---|---|---|
| sm | 40rem (640px) | 576px |
| md | 48rem (768px) | 768px |
| lg | 64rem (1024px) | 992px |
| xl | 80rem (1280px) | 1200px |
| 2xl / xxl | 96rem (1536px) | 1400px |
Range syntax and the classic form
Same breakpoints, two syntaxes
Range syntax reads like maths and makes non-overlapping bands easy: < on one side, >= on the other.
/* classic */
@media (min-width: 768px) and (max-width: 1023.98px) { .nav { gap: 1rem; } }
/* range syntax */
@media (768px <= width < 1024px) { .nav { gap: 1rem; } }
/* mobile first: base, then larger */
.grid { display: grid; gap: 1rem; }
@media (width >= 48rem) { .grid { grid-template-columns: 1fr 1fr; } }
@media (width >= 80rem) { .grid { grid-template-columns: repeat(4, 1fr); } }Preferences and input
Small additions that make a site feel native on every device.
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; scroll-behavior: auto !important; }
}
@media (hover: hover) and (pointer: fine) {
.card:hover { transform: translateY(-2px); }
}
@media (pointer: coarse) {
.icon-button { min-width: 44px; min-height: 44px; }
}
@media print {
nav, .ads { display: none; }
}The same queries in JavaScript
matchMedia returns an object with matches and a change event. This is what the live panel on this page uses.
const dark = window.matchMedia("(prefers-color-scheme: dark)");
console.log(dark.matches);
dark.addEventListener("change", (e) => {
document.documentElement.dataset.theme = e.matches ? "dark" : "light";
});Media query or container query?
| Media query | Container query | |
|---|---|---|
| Looks at | Viewport, device, user settings | Size or style of a parent container |
| Setup | None | container-type: inline-size on the parent |
| Syntax | @media (width >= 48rem) | @container (width >= 30rem) |
| Best for | Page layout, preferences, print | Reusable components placed in columns of any width |
| Tailwind v4 | md:flex | @container on the parent, @md:flex on the child |
A card that reacts to its own width
Both cards have the same markup. Only the wide one has room to switch to a row layout. Edit the widths to see it change.
<style>
.wrap { container-type: inline-size; border: 1px dashed #94a3b8; margin-bottom: 8px }
.card { display: grid; gap: 8px; padding: 10px; font: 14px system-ui }
.card div { background: #bfdbfe; height: 40px; border-radius: 6px }
@container (width >= 320px) { .card { grid-template-columns: 100px 1fr } }
</style>
<div class="wrap" style="width: 220px"><div class="card"><div></div><p style="margin:0">Narrow container</p></div></div>
<div class="wrap" style="width: 400px; max-width: 100%"><div class="card"><div></div><p style="margin:0">Wide container</p></div></div>Tailwind mapping
| Variant | CSS |
|---|---|
| md: | @media (width >= 48rem) |
| max-md: | @media (width < 48rem) |
| md:max-xl: | 48rem and up, below 80rem |
| min-[900px]: | @media (width >= 900px) |
| dark: | @media (prefers-color-scheme: dark) by default |
| motion-reduce: | @media (prefers-reduced-motion: reduce) |
| portrait: / landscape: | @media (orientation: portrait) / (orientation: landscape) |
| print: | @media print |
Anatomy of a media query
A query is an optional media type (screen, print or all) followed by feature tests in parentheses. @media screen and (orientation: landscape) needs both to be true. A comma between queries means either one is enough.
Queries do not add specificity. The rule inside wins or loses by normal cascade order, which is why responsive overrides usually go after the base styles in the file.
Input and preference queries
hover and pointer describe the primary input; any-hover and any-pointer describe all of them. A laptop with a touch screen typically reports hover: hover and pointer: fine for its trackpad, while any-pointer: coarse is also true because of the screen.
Preference queries reflect choices the person made in their system settings. Honouring prefers-reduced-motion is an accessibility requirement in practice, and prefers-color-scheme lets the site match the rest of their screen.
How it works
- Look at the live panel: green rows match on this device right now.
- Resize the window, rotate your device or switch your system theme. The rows update on their own.
- Copy the queries you need from the reference sections.
- For exact screen and viewport sizes, open the screen resolution tool.
Tips
- Use rem or em in breakpoints so layouts switch at the same reading width when someone enlarges their default text size.
- Pick breakpoints where your content breaks, not where popular phones happen to be.
- Gate hover effects behind (hover: hover) so touch users do not get stuck hover states.
- Combine queries with and, separate alternatives with a comma, and negate a whole query with not.
Frequently asked questions
What is the media query range syntax?
A shorter way to write width conditions with comparison operators: (width >= 600px) instead of (min-width: 600px), and (400px <= width < 900px) for a band. It is supported in all current major browsers; Safari added it in 16.4 in 2023, so check caniuse if you support older devices.
Is min-width or max-width better?
Mobile-first stylesheets use min-width: base styles serve small screens and each query adds layout for wider ones. That keeps small-screen CSS simple. max-width is fine for small exceptions, but mixing both at the same breakpoint can leave a one-pixel gap or overlap; range syntax with < and >= avoids that.
How do I detect dark mode in CSS?
Use @media (prefers-color-scheme: dark) and redefine your colour variables inside it. In JavaScript, window.matchMedia('(prefers-color-scheme: dark)').matches gives the same answer, and its change event fires when the user switches.
What is the difference between media queries and container queries?
Media queries look at the viewport and the device. Container queries look at the size of a parent element marked with container-type. A card that should change layout when its column is narrow, wherever it is placed, wants a container query.
Why is my media query not working?
Common causes: the viewport meta tag is missing, so phones pretend to be about 980px wide; the query sits above the rule it should override and loses on source order; or there is a missing space or parenthesis, which silently invalidates the whole condition.
Related: Screen resolution and viewport tool, CSS variables, Modern CSS features, Flexbox and grid cheat sheet, Scroll animations.
Build your site free or see all free tools. On We.Inc you can build your site free, with no card and no trial needed to start. AI building is on paid plans from $19 a month.
More in Guides
- BEM and CSS Naming Conventions
- Parallax Scrolling in Pure CSS
- CSS position: Five Values, One Live Demo
- CSS Quotes: The quotes Property, q and Styled Blockquotes
- Modern CSS Reset (Annotated)
- CSS Scroll Snap: Carousels and Full-Page Sections
- CSS Selectors Cheat Sheet With a Live Tester
- CSS Sizing Functions: calc(), min(), max(), clamp() and Viewport Units
Product
Who It's For
- For Agencies
- For Freelancers
- For Hosting Providers
- For SaaS Platforms
- For Startups
- For Financial Services
- For Healthcare
Features
Resources
- Blog
- Help Center
- Free Business Tools
- Free UI Components
- Website Templates
- Template Collections
- Website Examples
- Guides
- Comparisons
- Alternatives
- Best Lists
- Reviews
- Pricing Guides
- Use Cases
- Glossary & Guides
- Integrations
- Developers
- Benchmark
- Windows App