Media queries

Topic 2 of 5 in Responsive design8 min readBeginnerFree course

Overview

A media query applies CSS only when a condition about the device or the user is true. They’re the classic tool of responsive design — but modern media queries do much more than test widths. They let your layout respond to the user’s preferences and input method too.

CSS
@media (width >= 48rem) {
  .layout { grid-template-columns: 16rem 1fr; }
}

The rules inside the block apply when the viewport is at least 48rem wide. Outside it, they’re ignored — as if they weren’t in the stylesheet.

Range syntax

There are two ways to write width conditions. The modern range syntax reads like maths:

CSS
@media (width >= 48rem) { … }               /* at least 48rem */
@media (width < 48rem) { … }                /* narrower than 48rem */
@media (40rem <= width < 64rem) { … }       /* a range */

The older syntax uses min- and max- prefixes:

CSS
@media (min-width: 48rem) { … }
@media (max-width: 47.99rem) { … }

Range syntax is supported in all major browsers (since 2023) and avoids the off-by-one problem: with min-width: 48rem and max-width: 48rem, both queries match at exactly 48rem. width < 48rem and width >= 48rem never overlap.

Breakpoints

Breakpoints from content. A ruler from 0 to over 1440px: base styles apply everywhere; @media (width >= 40rem) applies from 640px; @media (width >= 64rem) from 1024px; @media (width >= 90rem) from 1440px. Write queries in rem so they scale with the user’s font size. Place breakpoints where the content needs them, not at device widths.

Why rem in queries?

In a media query, rem and em are based on the browser’s default font size, not the html font size you set in CSS. If someone raises their default from 16px to 24px, a 48rem breakpoint moves from 768px to 1152px. Their text is bigger, so they get the more spacious single-column layout sooner — exactly what they need. Pixel breakpoints ignore this.

Mobile-first order

Write queries from smallest to largest, using >=, so each one builds on the previous:

CSS
.cards { display: grid; gap: 1rem; }
@media (width >= 40rem) { .cards { grid-template-columns: repeat(2, 1fr); } }
@media (width >= 64rem) { .cards { grid-template-columns: repeat(3, 1fr); } }

Since later rules win when specificity is equal, the largest matching query takes effect.

Other viewport features

CSS
@media (orientation: landscape) { … }    /* wider than tall */
@media (height < 30rem) { … }            /* short screens: phones in landscape */
@media (aspect-ratio >= 16/9) { … }
@media (resolution >= 2x) { … }          /* high-density screens */

Height queries are underused: a sticky header that’s fine on a tall screen can cover half the content on a phone in landscape. @media (height < 30rem) { header { position: static; } } fixes it.

User preference queries

These describe what the user has asked for in their operating system settings. Respecting them is part of building accessible layouts.

prefers-reduced-motion

Some people get dizzy or nauseous from motion, or find it distracting:

CSS
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

This “big hammer” version effectively turns off movement across the page while still letting animation end events fire. You can also reverse it — add motion only for people who haven’t asked to reduce it:

CSS
@media (prefers-reduced-motion: no-preference) {
  .hero { animation: float-in 0.6s ease-out; }
}

prefers-color-scheme

CSS
:root { --bg: white; --text: #222; }

@media (prefers-color-scheme: dark) {
  :root { --bg: #16181d; --text: #e8e8e8; }
}

body { background: var(--bg); color: var(--text); }

Adding color-scheme: light dark to :root also makes built-in form controls and scrollbars switch themes.

prefers-contrast

CSS
@media (prefers-contrast: more) {
  .card { border: 2px solid currentColor; }
}

forced-colors

In Windows high-contrast mode, the browser replaces your colours with the user’s palette. Backgrounds and box shadows that you used as the only boundary between areas disappear, so keep a border (even a transparent one, which becomes visible):

CSS
.card { border: 1px solid transparent; }

Input queries: pointer and hover

Screen size doesn’t tell you whether someone is using a mouse or a finger. A large tablet has a coarse pointer; a small laptop window has a fine one.

CSS
@media (pointer: coarse) {
  /* fingers: bigger targets */
  .menu a { min-block-size: 44px; }
}

@media (hover: hover) {
  /* only devices that can actually hover */
  .card:hover { translate: 0 -2px; }
}

any-pointer and any-hover check any available input (for a laptop with a touchscreen, say).

Combining conditions

CSS
@media (width >= 48rem) and (orientation: landscape) { … }
@media (width < 30rem), (height < 30rem) { … }   /* comma = or */
@media not (hover: hover) { … }

Where media queries fall short

Media queries always test the viewport. A card component that should change its layout when it is narrow — in a sidebar on a big screen, say — can’t be written with a media query, because the viewport is wide. That’s what container queries solve, later in this chapter. Use media queries for page-level layout and user preferences; use container queries for components.

Two rules of thumb: write breakpoints in rem, mobile-first with >=; and add a prefers-reduced-motion block to every project with animation.

Exercise

A layout that respects the user

Write mobile-first media queries for .shell: one column by default; at width >= 40rem a 14rem sidebar plus content; at width >= 70rem add a third 18rem column for .extra. Use modern range syntax and rem units. Then: respect prefers-reduced-motion by turning off the .toast animation, provide a dark theme with prefers-color-scheme, and make .menu a links at least 44px tall only on devices with a coarse pointer.

HTML
<div class="shell">
  <nav class="menu"><a href="#">Home</a><a href="#">Courses</a></nav>
  <main>Main content</main>
  <aside class="extra">Extra</aside>
</div>
<p class="toast">Saved!</p>

<style>
  * { box-sizing: border-box; }
  body { background: white; color: #222; }
  .shell { display: grid; gap: 1rem; }
  .menu a { display: block; padding: 0.25rem; }
  .toast { animation: slide-in 0.4s ease-out; }
  @keyframes slide-in { from { translate: 0 2rem; opacity: 0; } }
  /* your code here */
</style>

Summary

  • @media (width >= 48rem) { … } applies its rules only when the condition is true; range syntax is supported everywhere.
  • Use rem or em in queries so breakpoints scale with the user’s font size.
  • Media queries test the viewport, not the element — use container queries for component-level changes.
  • Preference queries (prefers-reduced-motion, prefers-color-scheme, prefers-contrast) adapt to the user, not the screen.
  • pointer and hover queries describe the input device; use them for touch-friendly sizing and hover-only effects.