Sub topic 5
Mobile-first CSS with min-width
This chapter assumes you know the box model, selectors, and units such as px, rem, and %. It also assumes you have seen a media query before. Mobile-first is a way of organizing CSS so that the base styles describe the smallest layout and media queries add changes as the screen gets wider.
The working model
In a mobile-first stylesheet, the rules outside any media query are the mobile layout. Each min-width query then says "from this width upward, also apply these rules." Styles accumulate as the viewport grows, and each larger layout builds on the one before it.
The opposite approach is desktop-first. It writes the wide layout as the base and uses max-width queries to undo it on small screens. That works, but the small screen has to override many rules, and a phone has to download and parse styles it never uses.
Make sure the page head contains the viewport tag. Without it, mobile browsers lay the page out on a wide virtual viewport and your queries match the wrong width.
<meta name="viewport" content="width=device-width, initial-scale=1">A worked example
The base styles give a single column. Each query adds columns once there is room.
.layout {
display: grid;
gap: 1rem;
padding: 1rem;
}
/* Tablet and up */
@media (min-width: 48em) {
.layout {
grid-template-columns: 1fr 1fr;
}
}
/* Desktop and up */
@media (min-width: 80em) {
.layout {
grid-template-columns: 2fr 1fr 1fr;
max-width: 80rem;
margin-inline: auto;
}
}Here is what the browser applies at different widths:
| Viewport width | Queries that match | Result |
|---|---|---|
| 400px | none | One column |
| 800px (50em) | `min-width: 48em` | Two equal columns |
| 1400px (87.5em) | both | Three columns, centered, capped at 80rem |
At 1400px both queries match. The desktop rule wins because it comes later in the file and has the same specificity. That is why you order min-width queries from smallest to largest.
Why this pattern holds up
- Less overriding. The base layout is usually the simplest one, so queries add rules instead of cancelling them.
- Safer defaults. A browser that ignores your queries, or a width you did not plan for, still gets a usable single column.
- Predictable order. Reading the file top to bottom matches the order in which the layout grows.
The em unit in a query refers to the browser's default font size, not your page's. Using em in queries means the layout also adapts when a user raises their default text size.
Common errors
- Mixing
max-widthandmin-width. Combining them in one stylesheet makes the cascade hard to follow. Pickmin-widthfor layout and keep to it. - Writing queries in the wrong order. A
min-width: 48emblock placed after amin-width: 80emblock can override the larger layout, because both match and the later one wins. - Putting desktop values in the base styles. If the base sets
grid-template-columns: 2fr 1fr 1fr, the phone gets three squeezed columns until you undo it. - Forgetting the viewport tag. The CSS is correct, but the phone reports a width of around 980px and shows the desktop layout.
- Treating a breakpoint as a device. The numbers 48em and 80em are examples. The next chapter covers choosing them from where your content breaks.
Practice
Exercise
A stylesheet sets .sidebar { width: 30%; float: right; } in the base styles and then uses @media (max-width: 47.99em) { .sidebar { width: 100%; float: none; } } to fix small screens. Rewrite it mobile-first.
Show answer
Put the small-screen values in the base and add the wide values in a min-width query:
.sidebar { width: 100%; }
@media (min-width: 48em) {
.sidebar { width: 30%; float: right; }
}
The base no longer needs float: none, because the default is already no float. The query adds only what the wider layout needs.
Exercise
Your page has min-width queries at 80em and then 48em, in that order. At a 1400px viewport, which layout do you get, and how do you fix the order?
Show answer
You get the 48em (tablet) layout, because both queries match and the later one wins at equal specificity. Reorder the queries from smallest to largest, so 48em comes first and 80em comes last.