Sub topic 5
Mobile-first CSS with min-width
This chapter assumes you can write CSS selectors and know the box model. It also assumes you have met media queries and the viewport meta tag in earlier chapters. Here you will learn how to organize a stylesheet so the smallest layout is the default and wider layouts are added on top with min-width queries.
The mobile-first idea
A mobile-first stylesheet has two parts:
- Base styles with no media query. They describe the layout for the narrowest screens you support. For most pages this is a single column with comfortable spacing.
- Enhancement blocks, each wrapped in
@media (min-width: ...). Each block adds or changes only what must differ once more width is available.
The browser reads the file top to bottom. At 360px wide, only the base styles apply. At 800px wide, the base styles apply and then the tablet block overrides them. At 1400px wide, the base, tablet, and desktop blocks all apply, in that order.
This is different from "design for phones only". It is a way of ordering rules so that each wider layout builds on the narrower one.
<meta name="viewport" content="width=device-width, initial-scale=1">Without this tag, mobile browsers lay the page out in a wide virtual viewport (often around 980 CSS pixels) and shrink it to fit. Your min-width queries would then compare against 980px instead of the real screen width, and the mobile base styles would never be used. Put this tag in every page's <head> before you test any breakpoint.
Why min-width
A min-width query matches when the viewport is at least a given width. Queries written this way stack: every wider screen also matches every narrower query.
The diagram below shows which blocks apply at different widths.
Because the blocks accumulate, each block stays small. The desktop block does not need to repeat anything the tablet block already set unless it wants a different value.
A worked example
This is the layout from the spine. The base rule defines a single-column grid. Two queries then add columns.
.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;
}
}Follow it through at three widths:
| Viewport width | Queries that match | Result |
|---|---|---|
| 375px | none | One column (a grid with no template creates one implicit column), 1rem gap and padding |
| 800px | `min-width: 48em` | Two equal columns |
| 1400px | `min-width: 48em` and `min-width: 80em` | Three columns at 2:1:1, centered, capped at 80rem |
Notice that the base rule never mentions columns. A grid with no grid-template-columns already stacks its children, so the mobile layout needs no extra code. Mobile-first often means writing less for the small screen, because the browser's default flow is already a single column.
Why the cascade makes this work
Media queries add no specificity. A rule inside @media has the same specificity as the same selector outside it. Order in the file therefore decides which rule wins when specificity is equal. This leads to the most important rule of the technique:
Write the base styles first, then the queries in ascending order of width.
If you put the min-width: 80em block above the min-width: 48em block, then at 1400px both match, and the later one (tablet) wins. Your desktop columns would be overwritten by the tablet columns.
/* Wrong order: the tablet rule overrides the desktop rule at wide widths */
@media (min-width: 80em) {
.layout { grid-template-columns: 2fr 1fr 1fr; }
}
@media (min-width: 48em) {
.layout { grid-template-columns: 1fr 1fr; }
}The same problem appears if you write a base rule after a query. A plain .layout { padding: 1rem; } placed at the bottom of the file overrides any padding set in the queries above it.
Mobile-first compared with desktop-first
Desktop-first does the opposite. The base styles describe the wide layout, and max-width queries undo it for smaller screens.
| Aspect | Mobile-first (`min-width`) | Desktop-first (`max-width`) |
|---|---|---|
| Base styles describe | Narrowest layout | Widest layout |
| Queries do | Add features as space grows | Remove or reset features as space shrinks |
| Rules that need resetting | Few | Many (columns, floats, fixed widths) |
| Phone without working queries | Gets a usable single column | Gets the desktop layout squeezed in |
| Gap risk at boundaries | None, every width matches the base | Possible if ranges are written with mismatched numbers |
The reset cost is the practical difference. A desktop-first sheet often contains lines like grid-template-columns: 1fr; only to cancel a wider rule. In a mobile-first sheet those lines are not needed.
There is also a failure-mode difference. If a mobile-first query fails to match, for example because the viewport tag is missing, the user still gets a readable single-column page. If a desktop-first query fails, a phone user gets a layout built for 1280px.
Units for breakpoints
Write breakpoints in em (as the examples do) or rem, not px. In a media query, em and rem both resolve against the browser's initial font size, not against any font-size you set on html or body. At the default of 16px, 48em is 768px and 80em is 1280px.
The benefit appears when a user raises their default text size. A user with a 24px default text size sees the 48em query trigger at 1152px instead of 768px. The layout gets its extra columns later, which leaves each column wide enough for the larger text. A px breakpoint ignores that preference.
Keeping breakpoints and design frames in sync
The design tool and the stylesheet should use the same numbers. A common approach is:
- Record each breakpoint as a named design token, such as
bp-tablet: 48emandbp-desktop: 80em. - Make the frame widths in your design tool the first width where each layout is valid. For example, a tablet frame at 768px and a desktop frame at 1280px, matching
48emand80emat the default font size. - Add a base frame for the mobile layout, for example 360px or 375px.
CSS custom properties cannot be used inside @media conditions, so the token has to live in your design tokens file or build tool, which writes the literal value into the CSS. Plain CSS will not accept this:
:root {
--bp-tablet: 48em;
}
/* Does not work: var() is not allowed in a media query condition */
@media (min-width: var(--bp-tablet)) {
.layout { grid-template-columns: 1fr 1fr; }
}If you use a preprocessor such as Sass, a variable there is fine, because it is replaced before the browser sees the CSS.
Common errors
- Mixing
min-widthandmax-widthin one sheet. Amax-width: 767pxblock next to amin-width: 768pxblock works at whole-pixel widths. Browsers on high-density or zoomed screens can have fractional viewport widths such as 767.5px, which match neither query. Stay withmin-widthand the gap cannot occur. - Putting layout-changing declarations in the base that only suit wide screens. Fixed widths like
width: 960pxin the base cause horizontal scrolling on phones. Usemax-widthon the element, or leave the width unset. - Overriding with higher specificity than the base. If the base uses
.layout .itemand the query uses.item, the base wins at every width and the query seems to do nothing. Keep the selectors identical in the base and in the query block. - Repeating whole rule sets in each query. Only list the properties that change. A tablet block that copies every base declaration is hard to maintain and hides what actually differs.
- Testing only at the exact breakpoint widths. A layout can fail at 700px or 1100px even if 768px and 1280px look right. Later chapters cover testing between the breakpoints.
A short checklist for a mobile-first stylesheet
- The viewport meta tag is present.
- Base styles come first and work alone at about 320px wide.
- Queries use
min-widthinemorrem, in ascending order. - Each block contains only the declarations that change.
- Selectors in queries match the specificity of the selectors in the base.
- The breakpoint values match the tokens and the design frame widths.
Exercise
A stylesheet has .nav { display: block; } in the base, then @media (min-width: 80em) { .nav { display: flex; } }, then @media (min-width: 48em) { .nav { display: grid; } }. What display value does .nav have at a 1400px viewport, and how do you fix the problem?
Show answer
At 1400px both queries match, and the later one (min-width: 48em) wins, so .nav is grid. The queries are in descending order. Reorder them so the 48em block comes before the 80em block, and .nav becomes flex at 1400px.
Exercise
A user sets their browser's default font size to 20px. At what viewport width in pixels does a @media (min-width: 48em) block start to apply, and why is that useful?
Show answer
At 960px (48 x 20). Media query em units use the browser's initial font size. The extra columns appear only when the wider text still fits comfortably in them, so the layout does not become cramped for users who enlarged their text.
Exercise
Your phone shows the desktop layout shrunk to fit, and none of your min-width rules seem to match the real screen width. What is the most likely cause?
Show answer
The page is missing <meta name="viewport" content="width=device-width, initial-scale=1">. Without it the browser uses a wide virtual viewport of around 980 CSS pixels, so the media queries compare against that width instead of the device width.