Sub topic 5
Layering wider layouts with min-width queries
A mobile-first stylesheet starts with rules that work on the narrowest screen. It then adds rules for wider screens using min-width media queries. This chapter covers how those layers stack, how to write each query, and the mistakes that break them.
What you need before this chapter
This chapter assumes four things:
- You know CSS selectors, the box model, and how the cascade picks a winner between competing rules.
- You can build a basic Grid or Flexbox layout.
- You have already set the viewport meta tag from the earlier chapter, so the browser lays the page out at device width.
- You have already written single-column base styles.
Without the viewport tag, a phone ignores your queries and renders a wide virtual page. Check that first if nothing below seems to work.
<meta name="viewport" content="width=device-width, initial-scale=1">The working model: a base plus additions
A min-width query reads as "apply these rules when the viewport is at least this wide". The query is a condition on the viewport, and the rules inside it are ordinary CSS. The browser adds them to the cascade only while the condition is true.
That gives you a stack of layers:
- The base layer has no query. It applies at every width.
- Each
min-widthlayer applies from its threshold upward. - At any given width, every layer whose threshold has been passed is active at once.
Here is the basic pattern.
/* Base: small screens */
.layout {
display: grid;
gap: 1rem;
padding: 1rem;
}
/* Wider screens: add, don't undo */
@media (min-width: 48em) {
.layout {
grid-template-columns: 2fr 1fr;
padding: 2rem;
}
}On a narrow screen, .layout is a grid with no column template. It therefore has one implicit column, with 1rem of gap and padding. At 48em and wider, the query matches and two declarations change. The grid gets two columns and the padding grows. The display and gap declarations stay in the base and are never repeated.
The comment "add, don't undo" is the rule to follow. A wide-screen layer should add or change a property. If you find yourself writing float: none or grid-template-columns: none inside a min-width query, the base probably contains something that belongs in a layer.
Why min-width and not max-width
Desktop-first CSS writes the wide layout as the base and uses max-width queries to take features away. The same layout in desktop-first form looks like this:
.layout {
display: grid;
grid-template-columns: 2fr 1fr;
gap: 1rem;
padding: 2rem;
}
@media (max-width: 47.99em) {
.layout {
grid-template-columns: none;
padding: 1rem;
}
}Both versions produce the same result, but they fail differently:
- Undoing is error-prone. Every property set in the base has to be reset in the query, and you must know its initial value. Missing one leaves a desktop rule leaking onto phones.
- The smallest devices do the most work. A phone parses the desktop rules and then overrides them. The base layer is not a safe default, because it only suits wide screens.
- Failure is worse. If a mobile-first query doesn't match, such as when a browser is old or the viewport tag is missing, users still get a usable single-column page. If a desktop-first query doesn't match, they get a desktop layout squeezed onto a small screen.
- The
47.99emtrick is fragile. It exists only to avoid both queries matching at exactly 48em. Withmin-widthlayers, you never write a "just below" value.
Choose max-width only for a rule that really applies to small screens alone, and keep those rules rare.
Writing the query itself
Units: use em or rem, not px
Write breakpoints in em. In a media query, em and rem both resolve against the browser's initial font size, usually 16px. The font-size set in your CSS has no effect. So 48em is 768px by default, and the value stays predictable whatever you do to html.
The benefit shows up when a user raises their default text size. A breakpoint in px stays at the same pixel width. A breakpoint in em moves outward, so a user with large text keeps the narrower layout longer. That is usually what you want, since larger text needs more room per column.
Range syntax
Current browsers support a range form that reads more naturally:
@media (width >= 48em) {
.layout {
grid-template-columns: 2fr 1fr;
}
}It is equivalent to (min-width: 48em). The older form has the widest support. Use the range form if your browser support policy allows it, and use one form consistently across a stylesheet.
Combining conditions
You can join conditions with and. This is useful when a rule needs both a width and a capability:
@media (min-width: 64em) and (hover: hover) {
.menu-item:hover > .submenu {
display: block;
}
}Avoid long chains. Each added condition makes it harder to answer "when does this apply?".
Stacking several layers
Real pages need more than one threshold. Here is a three-layer example with a navigation list and a page wrapper:
/* Layer 0: base, one column, links stacked */
.nav {
display: grid;
gap: 0.5rem;
padding: 1rem;
}
.page {
display: grid;
gap: 1rem;
padding: 1rem;
}
/* Layer 1: from 40em, links sit in a row */
@media (min-width: 40em) {
.nav {
grid-auto-flow: column;
justify-content: start;
gap: 1.5rem;
}
}
/* Layer 2: from 64em, two columns and a capped width */
@media (min-width: 64em) {
.page {
grid-template-columns: 2fr 1fr;
max-width: 70rem;
margin-inline: auto;
padding: 2rem;
}
}At 30em wide, only the base applies. At 50em, the base and layer 1 apply. At 80em, all three apply. Layer 2 doesn't need to repeat anything from layer 1, because layer 1 is still active.
The thresholds are in ascending order in the source, and that matters. A media query adds no specificity. A rule inside @media has the same specificity as the same selector outside it. When two matching rules tie, the one later in the source wins. If you put the 64em layer before the 40em layer and both set the same property on the same selector, the 40em rule overrides the 64em rule on wide screens.
Common errors
| Mistake | Symptom | Fix |
|---|---|---|
| Missing viewport meta tag | Phones show a zoomed-out desktop page and no query seems to match | Add the viewport tag to the `head` |
| Query placed before the base rule | The wide layout never takes effect | Move the query below the base rules for that selector |
| Mixing `max-width` and `min-width` for the same property | Layouts break at one width, or two rules apply at once | Use `min-width` only, and write each layer to add to the one below |
| Breakpoints in `px` | Layout ignores the user's text-size setting | Write breakpoints in `em` |
| Restating base properties inside queries | Duplicated rules drift apart over time | Put shared properties in the base and change only what differs |
| Higher specificity in the base than in the query | The query matches but the property doesn't change | Match selectors exactly, or lower the base specificity |
| Many breakpoints that each change one value | Hard to follow, and each width needs separate testing | Use fluid techniques from later chapters so fewer queries are needed |
The specificity row is the least obvious. If the base rule is .page .layout { padding: 1rem } and the query contains .layout { padding: 2rem }, the base wins at every width. The query does match, but its selector is weaker. The fix is to use the same selector in both places.
Edge cases
- Exact threshold.
min-width: 48emincludes 48em itself. A viewport exactly 48em wide gets the layer. - Fractional widths. Browser zoom and high-density screens can produce viewport widths that are not whole pixels. This is the reason to avoid the
47.99empattern and stick withmin-widththroughout. - The query measures the viewport, not the component. A sidebar that is 20em wide on a 70em screen still matches
min-width: 48em. Container queries, covered in a later chapter, solve that. - Queries inside rules. Modern CSS lets you nest
@mediainside a selector block. It is handy for keeping a component's layers together, but it follows the same cascade rules, and older browsers may not support it. - Print and other media types. A bare
@media (min-width: 48em)applies to every media type, print included. Write@media screen and (min-width: 48em)only if you need to exclude print.
Exercises
Exercise
A stylesheet has this rule: .card { display: block; } followed by @media (min-width: 40em) { .card { display: flex; } } followed by @media (min-width: 64em) { .card { display: block; } }. What is the display value at 30em, 50em and 80em? Why might the third layer be a design smell?
Show answer
At 30em the value is block (base only). At 50em it is flex (40em layer). At 80em it is block again, because the 64em layer matches later in the source and overrides the 40em layer. The third layer undoes a previous layer instead of adding to it. That is a sign the layering is wrong, and the card layout probably belongs in a container query or needs a different design.
Exercise
A developer writes .page { grid-template-columns: 2fr 1fr; } inside @media (min-width: 64em) but places the query above the base rule .page { grid-template-columns: 1fr; } in the file. The page stays single-column on a 90em screen. What is the cause and the fix?
Show answer
Both rules have the same specificity, so source order decides, and the later base rule wins even though the query matches. Move the media query below the base rule, or remove the base grid-template-columns declaration, since a single column is the default for a grid with no template.