To use media queries for responsive web design, add the viewport meta tag, write mobile-first base styles, then add @media (width >= 40em) rules where your content starts to look broken. A media query is a CSS condition that applies a block of styles only when the device matches, such as a minimum viewport width. Follow the eight steps below, each with code you can copy and a way to check it worked.
How media queries work
Responsive web design means building one page that adapts its layout to the screen size, instead of serving a separate site per device. It rests on three ingredients: flexible layouts, flexible images, and media queries. The purpose of a media query is to let the browser test a condition and apply CSS only when it is true. The condition can be viewport width, orientation or a user preference. The viewport is the visible area of the page inside the browser window.
The browser reads your stylesheet and evaluates every @media condition against the current device. It applies the matching rules in normal cascade order, which means later rules win when specificity is equal. When the user resizes the window or rotates the phone, the conditions are re-evaluated instantly.
A query has up to three parts: an optional media type, one or more media features in parentheses, and the CSS rules inside braces. The media type says what kind of output the styles are for. all is the default when you leave the type out, screen covers phones, tablets, TVs and computer monitors, print covers printed pages, and speech covers screen readers, the software that reads a page aloud. A media feature, such as width, height, orientation or resolution, is the test that must pass.
Media queries are not the only way to adapt a page. JavaScript can also detect the screen width and change elements, but CSS media queries are usually more efficient because they need no script.

Step 1: Set the viewport meta tag
Without this tag, mobile browsers pretend the screen is much wider than it really is and shrink the page. Your media queries then never match as intended. Put this in the <head>:
<meta name="viewport" content="width=device-width, initial-scale=1">
The tag tells the browser to match the viewport width to the device width and start at normal scale. Check: open the page on a phone or in device mode in your browser developer tools. Text should look readable without pinch zooming.
Step 2: Write mobile-first base styles
Mobile-first means your default CSS, outside any query, targets the smallest screens. Larger screens then add rules on top. This keeps the base lightweight and avoids undoing styles again and again.
*, *::before, *::after { box-sizing: border-box; }
body {
margin: 0;
font-family: system-ui, sans-serif;
font-size: 1rem;
line-height: 1.5;
}
.container {
padding: 1rem;
max-width: 70rem;
margin-inline: auto;
}
.cards {
display: grid;
gap: 1rem;
grid-template-columns: 1fr;
}
Check: at around 320 pixels wide, everything stacks in one column with no horizontal scrollbar.

Step 3: Add min-width queries where the content breaks
Choose breakpoints from content, not devices
A breakpoint is the width at which a media condition becomes true and the layout changes. Widen your browser window slowly. The moment a line becomes too long or a gap looks awkward, that width is your breakpoint. Device lists change with every release cycle, so they make poor anchors.
The common breakpoints below are only a starting point. Treat them as defaults to adjust, not as a standard.
| Target | Typical starting width |
|---|---|
| Phones | 600px and down |
| Portrait tablets and large phones | 600px and up |
| Landscape tablets | 768px and up |
| Laptops and desktops | 992px and up |
| Large laptops and desktops | 1200px and up |
Use range syntax
Range syntax reads like a math comparison and replaces min-width and max-width. Newer media query features can have limited support in older browsers. Check current support on MDN before relying on it.
/* Classic */
@media (min-width: 48em) { ... }
/* Range syntax */
@media (width >= 48em) { ... }
/* Between two values */
@media (40em <= width < 64em) { ... }
Using em units makes the breakpoint respect the user's default font size, which helps accessibility. Pixels can make sense when your content is mostly images.
Check: drag the window across the breakpoint and confirm the style changes exactly once.
Step 4: Adapt grid, navigation and images
Grid
@media (width >= 40em) {
.cards { grid-template-columns: repeat(2, 1fr); }
}
@media (width >= 64em) {
.cards { grid-template-columns: repeat(3, 1fr); }
}
Navigation
On small screens, hide the full menu behind a button or stack the links. On wider screens, show them in a row. Hiding an element with a media query is a common way to simplify a small screen.
.nav ul { display: none; list-style: none; padding: 0; }
.nav.open ul { display: block; }
.menu-button { display: inline-block; }
@media (width >= 48em) {
.nav ul { display: flex; gap: 1.5rem; }
.menu-button { display: none; }
}
Images and type
img { max-width: 100%; height: auto; }
h1 { font-size: clamp(1.75rem, 4vw + 1rem, 3rem); }
The clamp() function sets a font size that grows with the viewport between a minimum and a maximum. This gives fluid text without extra breakpoints, and you can still change the font size inside a query when needed.
For different image files per size, use srcset and sizes in HTML rather than CSS. The browser then downloads only the file it needs. Hiding an image with display: none inside a query does not stop the download. Loading the right file also helps load times on mobile devices.
Check: images never overflow their container, and the menu works at both widths.
Step 5: Handle orientation, height, hover, pointer and resolution
Orientation tells you whether the viewport is taller than wide (portrait mode) or wider than tall (landscape mode). It is useful for full-height hero sections on phones turned sideways. Height queries help when a window is wide but short.
@media (orientation: landscape) and (height < 30em) {
.hero { min-height: auto; padding-block: 1rem; }
}
/* Hover only on devices that really hover */
@media (hover: hover) and (pointer: fine) {
.card:hover { transform: translateY(-4px); }
}
/* Bigger tap targets on touch screens */
@media (pointer: coarse) {
.nav a { padding: 0.75rem 1rem; }
}
/* Sharper images on high-resolution (retina) displays */
@media (min-resolution: 2dppx) {
.logo { background-image: url(logo-2x.png); }
}
A fine pointer is a mouse or trackpad, and a coarse pointer is a finger on a touch screen. The resolution feature detects how dense the pixels are, so you can serve a larger image only to high-resolution screens.
Check: rotate a phone or use the rotate button in developer tools, and confirm hover effects do not get stuck on touch.

Step 6: Respect user preference features
User preference media features let the visitor's system settings drive your design. They matter for accessibility and comfort.
@media (prefers-reduced-motion: reduce) {
* { animation: none !important; transition: none !important; }
}
@media (prefers-color-scheme: dark) {
:root { --bg: #111; --text: #eee; }
}
@media (prefers-contrast: more) {
:root { --text: #000; --bg: #fff; }
}
@media print {
.nav, .menu-button { display: none; }
}
The print media type shows that queries are not only about screens. Check: toggle these settings in your operating system or in the rendering panel of browser developer tools.
Step 7: Combine conditions correctly
- and: all conditions must be true, for example
(width >= 40em) and (orientation: landscape). - or: use a comma, for example
(width < 30em), (orientation: portrait). - not: negates the whole query, for example
not print.
You can also combine min-width and max-width to target one range of screen sizes. These combinations can be logically tricky, so test that they interact as you expect.

Step 8: Test that the site is responsive
To test whether a website is responsive, combine several checks:
- Open developer tools in Chrome, Firefox or Safari and switch to responsive or device mode. Drag the width from 320 pixels up to a wide desktop.
- Look for horizontal scrolling, overlapping text, tiny tap targets and images that exceed their box.
- Test on at least one real phone and one tablet. Emulation does not reproduce touch behaviour or browser toolbars exactly.
- Zoom the text to 200 percent and confirm the layout still works.
- Run an automated audit, such as Lighthouse in Chrome DevTools, for extra hints.
Common mistakes and how to fix them
Overlapping queries
Symptom: at exactly 768 pixels, two layouts apply at once. Cause: max-width: 768px and min-width: 768px both match. Fix: stay mobile-first with only min-width, or use range syntax with an exclusive bound: (width < 48em) and (width >= 48em).
Wrong cascade order
Symptom: a media query seems ignored. Cause: a later rule with the same specificity overrides it. Media queries add no specificity. Fix: place each query after the base rule it modifies.
Missing viewport tag
Symptom: the desktop layout shows on phones. Fix: return to step 1.
Device-specific breakpoints
Symptom: layout breaks on a new phone. Cause: breakpoints copied from a device list. Fix: base them on where your own content breaks.
Using pixels for everything
Symptom: users who enlarge text get a cramped layout. Fix: prefer em or rem in queries.
When container queries fit better
Media queries look at the viewport. A reusable component such as a card, however, may sit in a wide main area one day and a narrow sidebar the next. Container queries respond to the size of the parent instead, so the component adapts wherever it is placed.
.card-wrap { container-type: inline-size; }
.card { display: block; }
@container (width >= 30em) {
.card { display: grid; grid-template-columns: 8rem 1fr; gap: 1rem; }
}
A practical split: use media queries for page-level decisions such as the overall grid, navigation and user preferences. Use container queries for components. Also consider whether you need a query at all. repeat(auto-fit, minmax(16rem, 1fr)) in CSS Grid, Flexbox wrapping and clamp() often adapt layouts without any breakpoint.
Which approach fits you
If you build a whole site or landing page, start mobile-first with a few content-based min-width queries and the preference features above. If you build a component library, lean on container queries and intrinsic grids, keeping media queries for the page shell. Open your current stylesheet, pick one page, and run through the eight steps with developer tools in device mode to see which breakpoints you really need.
