Sub topic 5
Enhancing larger screens with min-width media queries
Prerequisites and where this fits
This chapter assumes you know basic CSS selectors, the box model and relative units. It also assumes you have already built a single-column base layout and set the viewport meta tag, as covered in the earlier chapters. Here you add layout changes for wider screens without rewriting the base.
The working model
In a mobile-first stylesheet, the rules outside any media query are the small-screen design. Each min-width query then says: "when the viewport is at least this wide, also apply these rules."
Because min-width rules only add to what is already there, wider screens inherit everything from the base. You override only the properties that need to change. The result is less CSS, and small devices never download or parse rules written for large screens that they cannot use.
A desktop-first stylesheet works the other way. It starts with the full layout and uses max-width queries to undo it. That means small screens must cancel many properties, which is where bugs and extra code come from.
A worked example
This card list is one column by default. It uses CSS Grid with a gap for spacing.
.card-list {
display: grid;
gap: 1rem;
padding: 1rem;
}
@media (min-width: 40em) {
.card-list {
grid-template-columns: repeat(2, 1fr);
}
}
@media (min-width: 64em) {
.card-list {
grid-template-columns: repeat(3, 1fr);
}
}Follow what happens as the window widens:
- Below 40em, no query matches. The grid has one implicit column, so cards stack.
- At 40em or wider, the first query matches and the grid gets two equal columns.
- At 64em or wider, both queries match. The second one comes later in the file with the same specificity, so its
grid-template-columnswins and you see three columns.
The repeat(2, 1fr) function creates two columns that share the free space equally. The fr unit is a fraction of the available space.
Why em in the query
The example uses em instead of px for the breakpoints. In media queries, em is relative to the browser's default font size, not to any font size set in your CSS. If a user raises their default text size, the breakpoints shift with it. A user with large text then keeps the simpler layout longer, which stops columns becoming too narrow for the enlarged text. Pixel breakpoints do not respond to that preference.
Common errors
- Order matters. Queries have the same specificity as normal rules, so the later rule wins. Put
min-widthqueries in ascending order, smallest first. If a 64em block comes before a 40em block, the 40em rules override it on wide screens. - Mixing
max-widthandmin-width. A stylesheet that uses both for the same property is hard to reason about. Pickmin-widthas the default and use a range query only for a real, specific exception. - Repeating the whole rule. Inside a query, write only the properties that change. Copying
display: gridandgapinto each block adds noise and makes later edits error-prone. - A missing viewport tag. Without
<meta name="viewport" content="width=device-width, initial-scale=1">, a phone reports a wide layout viewport and your small-screen rules never apply. If your queries seem to do nothing on a real phone, check this first. - Hiding content with
display: noneat small sizes. Content hidden on mobile is still downloaded. Reorder, collapse or reveal it instead, using the techniques in the progressive disclosure chapter.
Checking your work
Open your browser's developer tools and turn on device emulation. Drag the viewport width slowly across each breakpoint. Confirm the layout changes exactly where you expect, and that it is not broken just below or above that width.
Exercise
A stylesheet has .nav { flex-direction: column; } as the base rule, followed by @media (min-width: 48em) { .nav { flex-direction: row; } }. A developer then adds @media (min-width: 30em) { .nav { flex-direction: column; } } at the end of the file. What does the nav look like at 60em wide, and how do you fix it?
Show answer
It stays in a column. At 60em both queries match, and the 30em rule comes later with equal specificity, so it overrides the row direction. The fix is to put the queries in ascending order, so the 30em block comes before the 48em block. Or remove the 30em block if it only repeats the base rule.