Sub topic 6
Mobile-first styling with min-width
This chapter assumes you know basic HTML structure, CSS selectors and the cascade, the box model, and the units px, em, and rem. It also assumes you have seen the viewport meta tag and the basic @media syntax from earlier chapters.
What mobile-first means
Mobile-first is a way of ordering your CSS. You write the base styles for the smallest screen, with no media query around them. Then you add @media (min-width: ...) blocks that layer changes on top as the viewport gets wider.
The opposite approach is desktop-first. There you write the wide layout as the base and use max-width queries to undo or simplify it for narrow screens.
Both approaches can produce the same result. Mobile-first usually gives you less CSS to override and a simpler mental model, for these reasons:
- The base layout of a small screen is usually simple: one column, stacked blocks. Simple rules need no undoing.
- A device that fails to match any query still gets a usable page.
- Each query adds to the layout instead of reversing earlier rules.
How min-width queries stack
A min-width query matches when the viewport is at least that wide, and it keeps matching at every larger width. So the rules inside it apply to the breakpoint and everything above it. This is why the queries stack:
- Below the first breakpoint, only the base rules apply.
- Past the first breakpoint, the base rules and the first query apply.
- Past the second breakpoint, all three layers apply, and later rules win when selectors tie.
Because the layers stack, source order matters. Queries do not add specificity. A rule inside @media has the same specificity as the same selector outside it, so the later rule wins. Always place the media query after the base rule it modifies.
A worked example: the card grid
The card grid below starts as a single column. At 40em it becomes two columns, and at 64em it becomes three.
.cards {
display: grid;
gap: 1rem;
grid-template-columns: 1fr;
}
@media (min-width: 40em) {
.cards {
grid-template-columns: repeat(2, 1fr);
}
}
@media (min-width: 64em) {
.cards {
grid-template-columns: repeat(3, 1fr);
}
}Notice what the stylesheet does not contain. There is no rule that resets the layout to one column, because one column is the default. Each query changes only the property that needs to change. The display and gap declarations are written once and inherited by every layer.
Now compare the desktop-first version of the same layout:
.cards {
display: grid;
gap: 1rem;
grid-template-columns: repeat(3, 1fr);
}
@media (max-width: 63.99em) {
.cards {
grid-template-columns: repeat(2, 1fr);
}
}
@media (max-width: 39.99em) {
.cards {
grid-template-columns: 1fr;
}
}It works, but the base rule describes the widest case and each query takes something away. With more components, this style tends to accumulate overrides that must be reasoned about in reverse. The .99em values are also a workaround for max-width being inclusive, and min-width avoids that problem.
Why em for breakpoints
The examples use em rather than px in the query conditions. Inside a media query, em and rem are relative to the browser's initial font size, usually 16px, and not to any element's font size. They ignore your CSS font-size settings.
This matters when a user raises their default text size in the browser. With 40em, the breakpoint moves from 640px to a larger pixel width, so the layout switches to multiple columns later. That keeps the columns from becoming too narrow for the larger text. A 640px breakpoint would not adapt in this way.
Building a full page layer by layer
Here is a larger example. It styles a page with a header, navigation, main content, and a sidebar. The base styles target a phone, and two queries adapt the layout for larger screens.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Mobile-first page</title>
<style>
* { box-sizing: border-box; }
body {
margin: 0;
font-family: system-ui, sans-serif;
line-height: 1.5;
}
.page {
display: grid;
grid-template-areas:
"header"
"nav"
"main"
"aside";
gap: 1rem;
padding: 1rem;
}
header { grid-area: header; }
nav { grid-area: nav; }
main { grid-area: main; }
aside { grid-area: aside; }
nav ul {
display: flex;
flex-direction: column;
gap: 0.5rem;
margin: 0;
padding: 0;
list-style: none;
}
@media (min-width: 40em) {
nav ul {
flex-direction: row;
}
}
@media (min-width: 64em) {
.page {
grid-template-columns: 1fr 3fr;
grid-template-areas:
"header header"
"nav nav"
"aside main";
max-width: 72rem;
margin-inline: auto;
}
}
</style>
</head>
<body>
<div class="page">
<header><h1>Site title</h1></header>
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Articles</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
<main><p>Main content goes here.</p></main>
<aside><p>Related links.</p></aside>
</div>
</body>
</html>Here is what each width gets:
| Viewport width | Navigation | Page grid |
|---|---|---|
| Below 40em | Vertical list | Single column, areas stacked |
| 40em to below 64em | Horizontal row | Single column, areas stacked |
| 64em and above | Horizontal row | Two columns, sidebar left of main, centered at 72rem max |
Notice that the two queries are independent. The navigation changes at 40em and the page grid at 64em. Each component gets its own breakpoints, and they do not have to share the same values.
On a phone, the source order is also the visual order: header, nav, main, aside. Keeping source order and small-screen order identical is good for keyboard and screen reader users. Use grid-template-areas to rearrange blocks on wider screens only when needed, and remember that reordering visually does not change the tab order.
Keeping queries organized
There are two common ways to place media queries in a stylesheet.
- Grouped at the end of the file. All queries sit together. This is easy to scan by breakpoint, but one component's styles are split across the file.
- Next to the component. Each component has its base rules followed by its own queries. A component's whole behavior is in one place, which is easier to maintain, but the same query condition appears many times.
Either works. Component-local queries are common in larger codebases, and the browser handles repeated queries without trouble. Whichever you choose, keep each query after the base rules it overrides.
Common errors
- Mixing directions. Using
max-widthqueries in a mobile-first stylesheet forces you to think in both directions. Stay withmin-widthunless you need a range, as in the next chapters. - Query placed before the base rule. If the base rule comes later with the same specificity, it overrides the query. Reorder so the query comes after.
- Overriding more than necessary. If a query repeats declarations that have not changed, the layer is heavier than it needs to be. Change only the properties that differ.
- Missing viewport meta tag. Without it, phones lay the page out at a wide virtual width, often around 980px, so your small-screen base styles may never apply.
- Breakpoints chosen for devices. A breakpoint at a specific phone width goes out of date. Choose breakpoints where your content starts to look wrong, which a later chapter covers.
- Hiding content on small screens. Setting
display: noneon content for phones means mobile users cannot reach it. Prefer reorganizing content to removing it.
Performance and loading
Mobile-first has a small performance benefit. The base styles for small screens are applied without any query evaluation, and devices that do not match the larger queries skip those rules. The gain is modest, though, because browsers download all stylesheet rules regardless of whether the queries match. Do not choose mobile-first for speed alone. Its main value is a simpler cascade.
If you split CSS into separate files, you can put the condition on the link element, for example media="(min-width: 64em)". Browsers still download the file but do not block rendering on it when it does not match.
Exercises
Exercise
A stylesheet has .sidebar { display: none; } as a base rule and a @media (min-width: 48em) block later that sets .sidebar { display: block; }. At a viewport width of 70em, is the sidebar visible? Why?
Show answer
Yes. At 70em the viewport is at least 48em, so the query matches. The query comes after the base rule and has the same specificity, so its display: block wins. A min-width query keeps matching at every width above its threshold.
Exercise
Rewrite this desktop-first rule set as mobile-first, keeping the same results at every width: .nav { display: flex; } with @media (max-width: 39.99em) { .nav { display: block; } }.
Show answer
Write .nav { display: block; } as the base rule, followed by @media (min-width: 40em) { .nav { display: flex; } }. Below 40em the nav is block, and at 40em and above it is flex, the same as the original.
Exercise
A user sets their browser's default font size to 24px. A page uses @media (min-width: 40em). At roughly what viewport width in pixels does the query start matching, and why does that help?
Show answer
At 960px, because 40em is 40 times the initial font size of 24px. Text is larger, so the switch to a multi-column layout happens at a wider viewport, which leaves enough room for the columns.