Sub topic 5
Layering wider layouts with min-width queries
This chapter assumes you know HTML structure, CSS selectors, the box model and the cascade, basic Flexbox or Grid, and how to use browser developer tools. It also assumes you have already set the viewport meta tag and written single-column base styles.
The working model
In mobile-first CSS, your base rules describe the smallest layout. A min-width media query then says "from this width upward, also apply these rules." Each query adds to what came before. It does not replace the whole stylesheet.
The browser applies every matching rule, and the cascade resolves conflicts. A rule inside a matching query wins over an earlier rule with the same specificity because it comes later in the source. That is why query order matters.
A worked example
The base rule gives a single column. The query adds the second column only when there is room.
/* Base: small screens */
.layout {
display: grid;
gap: 1rem;
padding: 1rem;
}
/* Wider screens: add, don't undo */
@media (min-width: 48em) {
.layout {
grid-template-columns: 2fr 1fr;
padding: 2rem;
}
}Below 48em, only the first rule applies. At 48em and above, both apply. The query changes padding and adds grid-template-columns. It does not repeat display: grid or gap, because those are inherited from the base rule through the cascade.
Why em in queries
Notice the query uses 48em, not 768px. In media queries, em and rem both resolve against the browser's default font size, not your CSS. A user who raises their default text size reaches the breakpoint at a wider screen, so the layout gets more room for larger text. Pixel breakpoints ignore that setting.
Stacking several queries
You can add more queries for larger widths. Keep them in ascending order, smallest first, so later and wider rules override earlier ones.
.layout {
display: grid;
gap: 1rem;
}
@media (min-width: 40em) {
.layout {
gap: 1.5rem;
}
}
@media (min-width: 64em) {
.layout {
grid-template-columns: 2fr 1fr;
gap: 2rem;
}
}At 70em, all three blocks match. The gap is 2rem because the last matching rule wins.
Common errors
- Queries in the wrong order. If a
min-width: 64emblock comes before amin-width: 40emblock and both set the same property, the 40em block wins on wide screens. Order queries from smallest to largest. - Undoing instead of adding. If a query mostly resets base values, the base is probably designed for the wrong screen. Move the common rules into the base and keep only the additions in the query.
- Mixing
max-widthinto a mobile-first sheet. Combining both directions makes the cascade hard to follow. Usemin-widththroughout, and use a range such as(min-width: 40em) and (max-width: 63.99em)only when a style must apply in a limited band. - Specificity mismatches. A query adds no specificity. If a base rule uses
.layout .mainand the query uses.main, the base rule wins at every width. Match selectors between the base and the query. - Missing viewport tag. Without it, mobile browsers use a wide virtual viewport, so your queries match the wrong width.
Exercise
A stylesheet has .nav { flex-direction: column; } in the base. A query @media (min-width: 40em) { .nav { flex-direction: row; } } follows it. A second query @media (min-width: 30em) { .nav { flex-direction: column-reverse; } } comes after that. What is flex-direction at a viewport of 45em, and why?
Show answer
It is column-reverse. At 45em both queries match, and they have equal specificity, so the one later in the source wins. The 30em block should be placed before the 40em block so that the wider rule wins and the value is row.