Lesson 19 of 23

In progress

Course progress

HTML and CSS

Media queries and user preferences

Add breakpoints where the content needs them, respect dark mode and reduced motion, and test responsiveness without device lists.

A media query is a conditional block. It applies styles only when the environment matches: a minimum width, a colour-scheme preference, a reduced-motion setting. Used well, it is the difference between a layout that adapts and one that merely shrinks.

What you will learn

  • Write mobile-first breakpoints based on content.
  • Target user preferences, not just screen sizes.
  • Avoid the common traps of max-width and device-specific queries.

Mobile-first syntax

HTML
Editable. Runs in your browser.





Press Run to see the output.

The base styles describe the small screen. The query adds the two-column layout when there is room. This is mobile-first: enhance upward, and a device that never matches still gets a usable page.

max-width queries do the opposite and are useful for exceptions, but a stylesheet built entirely from them tends to fight itself as screens get larger.

Breakpoints follow content

Resize the window slowly and watch. When the line length becomes uncomfortable or a row of cards becomes too cramped, note the width. That is where a breakpoint belongs.

Common values land near 40rem and 64rem, but copying someone else’s list produces gaps and jumps that do not match your design. Two or three breakpoints are usually enough; a dozen means the layout is too rigid.

Modern syntax allows ranges, which read better than combining queries:

@media (width >= 45rem) and (width < 64rem) { /* tablet */ }

Older browsers do not support range syntax, so use min-width and max-width if you need broad compatibility.

Preferences, not devices

Media queries are not only about width. They expose settings the user has already chosen.

HTML
Editable. Runs in your browser.





Press Run to see the output.

Input capability is another useful query. @media (hover: hover) and (pointer: fine) applies hover effects only where a mouse exists, which avoids sticky hover states on touch screens. @media (hover: none) is the place for larger tap targets and always-visible controls. Test both by resizing with a touch emulator, not only by narrowing the desktop window.

prefers-color-scheme is the basis of dark mode, covered in lesson 23. prefers-reduced-motion should remove or shorten non-essential movement, not merely slow it. prefers-contrast is a hint to strengthen borders and text, not a requirement to redesign.

A print stylesheet is another media query: @media print can hide navigation, remove backgrounds and set page margins. It is often the simplest accessibility win for recipes, tickets and certificates.

Container queries, briefly

A media query responds to the viewport. A container query responds to the size of a parent element, which is what you usually mean for a reusable card.

.card-wrap { container-type: inline-size; }
@container (min-width: 30rem) {
.card { display: grid; grid-template-columns: 120px 1fr; }
}

Container queries let a card adapt in a sidebar and in a wide main column without knowing the viewport. Support is good in current browsers; keep a sensible default for the rest.

Testing responsiveness

Use the browser’s responsive mode and drag the width rather than picking device presets. Check at 320 pixels, around 768, and a wide desktop, plus 200 percent zoom.

Look for horizontal scrolling, overlapping text, tap targets under 44 pixels, and content that disappears rather than reflows. Those four cover most real failures.

Hiding content with display: none at small widths, such as a navigation menu with no replacement, removes it for phone users entirely. Either build a working toggle or let the navigation reflow into a list. Hiding is not responsive design.

Build a card that is a single column by default and becomes a two-column layout with the image beside the text above 42rem. Add a reduced-motion rule that disables its hover transform.

Show solution
HTML
Editable. Runs in your browser.





Press Run to see the output.

The base layout is the small screen, the query adds the side-by-side version, and the reduced-motion query removes the transform entirely rather than making it faster.

Recap

  • Write base styles for small screens and add min-width queries where the content needs them.
  • Query preferences such as reduced motion and contrast, not device names.
  • Container queries adapt a component to its parent; test with real resizing and zoom.

Next, you will add motion that supports the interface instead of distracting from it.

How many breakpoints should a site have?
Usually two or three. Each one should exist because the content broke at that width, not because a device list said so.
Do media queries affect performance?
They are cheap to evaluate. A large number of overlapping queries makes the CSS harder to maintain, which is the real cost.

Progress is saved in this browser only. No account, no email.

Admin

About the author

Admin

Instructor and technical writer at tutorials.in

Writes beginner lessons for tutorials.in. Each lesson is checked against the language or tool documentation, and examples are run before publishing.

Reviewed by the editorial team before publishing. Last updated November 16, 2026.