Lesson 19 of 23
In progress
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-widthand device-specific queries.
Mobile-first syntax
Editable. Runs in your browser.
@media (min-width: 45rem) {
.columns { grid-template-columns: 1fr 1fr; }
}
</style>
<div class="columns">
<div>One</div>
<div>Two</div>
</div>
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.
Editable. Runs in your browser.
@media (prefers-contrast: more) {
.card { border-width: 2px; }
}
</style>
<div class="card" style="padding:20px;border:1px solid #999">
Animations are reduced for people who ask for it.
</div>
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
Editable. Runs in your browser.
@media (min-width: 42rem) {
.card { grid-template-columns: 180px 1fr; align-items: center; }
}
@media (prefers-reduced-motion: reduce) {
.card { transition: none; }
.card:hover { transform: none; }
}
</style>
<article class="card">
<img src="https://picsum.photos/id/1062/360/240" alt="A dog on a path" width="360" height="240">
<div>
<h2>Adaptive card</h2>
<p>One column on small screens, two when there is room.</p>
</div>
</article>
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-widthqueries 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?
Do media queries affect performance?
Progress is saved in this browser only. No account, no email.