Sub topic 4
Querying the viewport width
This chapter assumes you know CSS selectors, the cascade, and the box model. It also assumes you can read px, em, and rem values and that your page already has the viewport meta tag.
The width feature
A media query tests a condition, and its rules apply only while the condition is true. The most common condition is the width of the viewport, the visible area of the page in the browser window.
The width feature has three forms:
| Form | True when | Typical use |
|---|---|---|
| `min-width: 40em` | viewport is 40em or wider | add styles for larger screens |
| `max-width: 40em` | viewport is 40em or narrower | add styles for smaller screens |
| `width: 40em` | viewport is exactly 40em | rarely useful |
The exact-width form almost never matches in practice, because users resize windows freely. Use min-width and max-width.
.nav {
display: block;
}
@media (min-width: 48em) {
.nav {
display: flex;
gap: 1rem;
}
}Below 48em, the nav stays a block. At 48em and wider, the min-width rule applies and the nav becomes a flex row. The rule inside the query has the same specificity as one outside it, so source order still decides ties. Put the query after the base rule.
Units inside queries
Media queries accept px, em, and rem, but em and rem behave differently here than in normal rules. Inside a query, both are relative to the browser's initial font size, usually 16px. They ignore any font-size you set on html or body. So 40em means 640px by default.
Em-based breakpoints have one advantage. If a user raises the browser's default font size, the breakpoint moves with it, and the layout switches to its wider-screen styles later. Pixel breakpoints don't do this.
@media (max-width: 40em) {
.sidebar {
display: none;
}
}Avoiding gaps between ranges
Suppose you write one query for max-width: 600px and another for min-width: 601px. A viewport can be 600.5px wide, for example under browser zoom or on a high-density display. That width matches neither query, so neither set of rules applies.
Two fixes work. You can use min-width queries only, which the mobile-first chapter covers. Or you can leave a fractional gap on the upper bound:
@media (max-width: 39.99em) {
.sidebar {
display: none;
}
}
@media (min-width: 40em) {
.sidebar {
display: block;
}
}Common mistakes
- Width includes the scrollbar. A 1000px window with a 15px scrollbar still reports 1000px, even though your content area is narrower.
device-widthandmin-device-widthmeasure the physical screen, not the window. They are deprecated, so usewidth.- A missing viewport meta tag makes phones report a layout width near 980px, so your narrow-screen rules never fire.
- Putting a
max-widthquery before the base rule lets the base rule override it.
Exercise
A page sets html { font-size: 20px; } and uses @media (min-width: 40em). At what viewport width in px does the query start to match, assuming the browser default font size is 16px? Why?
Show answer
At 640px. Inside a media query, em is relative to the browser's initial font size (16px), not to the font-size set on html. So 40em is 40 × 16 = 640px.