Sub topic 6
Mobile-first styling with min-width
This chapter assumes you know the viewport meta tag, basic @media syntax, and width features such as min-width and max-width. It also assumes you are comfortable with CSS selectors, the cascade, the box model, and units like em and rem.
What mobile-first means
Mobile-first means you write your base styles for the smallest screen, with no media query at all. You then add min-width queries that layer on changes as the viewport gets wider.
The opposite approach is desktop-first. It starts with a wide layout and uses max-width queries to undo styles for smaller screens. Both approaches can produce the same result. Mobile-first usually needs less code, because a single column is the browser's default flow and needs little extra CSS.
Why min-width fits the cascade
A min-width query matches when the viewport is at least that wide. Once a query matches, it keeps matching at every larger width. Later rules with equal specificity override earlier ones, so each breakpoint builds on the one before it.
A worked example
This card grid starts as one column. It becomes two columns at 40em and three at 64em.
.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);
}
}At 30em wide, only the base rule applies. At 50em, the base rule and the 40em rule apply, and the 40em rule wins for grid-template-columns. At 70em, all three apply, and the 64em rule wins because it comes last.
The gap is set once in the base styles and is never repeated. Each query changes only what differs.
Order matters
Media queries add no specificity. A rule inside @media has the same specificity as the same selector outside it. The source order therefore decides the winner.
Write the base rule first, then the queries from smallest to largest. If you put the 64em query before the 40em query, the 40em rule overrides it at wide widths, because both match and the later one wins.
Units in breakpoints
The examples use em for breakpoints. In media queries, em is relative to the browser's initial font size, not to your own CSS. If a user raises their default font size, the breakpoints move with it, and the layout adapts to the larger text. Pixel breakpoints do not do this.
Common errors
- Mixing directions. Using
max-widthfor some breakpoints andmin-widthfor others makes the cascade hard to follow. Pickmin-widthand keep to it. - Undoing styles. If a query only resets what the base set, move that value out of the base. For example, don't set
display: nonein the base styles and thendisplay: blockin the query, unless the element really is hidden on small screens. - Overlapping ranges. Using
max-width: 40emandmin-width: 40emtogether matches both at exactly 40em. When you must pair them, use a value like39.99emfor the upper bound, or use range syntax, which a later chapter covers. - Missing viewport tag. Without
<meta name="viewport" content="width=device-width, initial-scale=1">, mobile browsers lay the page out at a wide virtual width. Your base styles then never apply as intended.
Exercise
A page has a base rule .nav { display: block; }. Below it are @media (min-width: 64em) { .nav { display: flex; } } and @media (min-width: 40em) { .nav { display: grid; } }. What is the display value at a viewport width of 70em, and why? How would you fix the order?
Show answer
The value is grid. Both queries match at 70em, they have equal specificity, and the 40em query comes later in the source, so it wins. Fix this by placing the 40em query before the 64em query, so the order runs from smallest to largest. The result at 70em is then flex.