Sub topic 4
Querying the viewport width
This chapter assumes you can write basic CSS rules and know the @media at-rule form. It also assumes you know px, em, and rem units. It covers the width-based media features: what they measure, how the boundaries work, and where they go wrong.
What "width" means in a media query
The width media feature describes the width of the viewport. In a browser, that is the width of the area where the page is laid out, measured in CSS pixels. Three details matter:
- A CSS pixel is not a hardware pixel. A phone with a 1170-pixel-wide screen usually reports a viewport of about 390 CSS pixels.
- The value includes the vertical scrollbar if the scrollbar takes up space. A desktop window that shows 1000 CSS pixels of content next to a 15px scrollbar matches
(width: 1015px). - On mobile, the value depends on the viewport meta tag from the previous chapter. Without
width=device-width, many mobile browsers report a layout width of around 980px, and your narrow-screen rules never run.
The three forms of the feature
Each width feature accepts a length. The feature name decides how the comparison works.
| Query | Matches when | Boundary |
|---|---|---|
| `(min-width: 40em)` | viewport width is 40em or more | inclusive |
| `(max-width: 40em)` | viewport width is 40em or less | inclusive |
| `(width: 40em)` | viewport width is exactly 40em | exact match |
Exact width queries are rarely useful, because a user can resize a window to any value. In practice you use min-width and max-width. The range syntax in a later chapter writes the same comparisons with <= and >=.
Here is a complete page you can save and open. It shows which width band is active.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Width bands</title>
<style>
body {
margin: 0;
font-family: sans-serif;
}
.status {
padding: 1rem;
background: #cfe8ff;
}
.status::after {
content: "Narrow: under 40em";
}
@media (min-width: 40em) {
.status {
background: #d4f5d4;
}
.status::after {
content: "Medium: 40em up to 64em";
}
}
@media (min-width: 64em) {
.status {
background: #ffe3b3;
}
.status::after {
content: "Wide: 64em and above";
}
}
</style>
</head>
<body>
<p class="status"></p>
</body>
</html>Resize the window and watch the label change. Both min-width rules match at 70em, and the later rule wins because the specificity is equal and it comes last. Media queries add no specificity. A rule inside @media has the same weight as the same selector outside it, so source order still decides ties.
Using max-width
max-width applies styles at or below a size. Use it to turn something off for small screens or to limit a rule to a range together with min-width.
.promo-banner {
display: block;
}
@media (max-width: 30em) {
.promo-banner {
display: none;
}
}A rule written this way works, but a stylesheet made only of max-width queries has to undo the desktop styles on every small screen. The next chapters explain why most teams start from the small screen and add min-width rules.
Pixels versus ems
You can write breakpoints in px, em, or rem, and the choice changes behavior.
- In a media query,
emandremboth resolve against the browser's initial font size, which is normally 16px. They do not use thefont-sizeyou set onhtmlorbody. - If a user raises their default text size in browser settings, an
embreakpoint moves with it. At a default size of 20px,40emmeans 800px. A640pxbreakpoint stays at 640px. - Em-based breakpoints let the layout change earlier for people who use larger text. That keeps lines from becoming too short for the text size.
/* 40em equals 640px at the default 16px text size */
@media (min-width: 40em) {
.layout {
display: grid;
grid-template-columns: 12rem 1fr;
gap: 1.5rem;
}
}Pick one unit and use it everywhere in the stylesheet. Mixing px and em breakpoints gives breakpoints that are no longer in the order you expect once a user changes the text size.
The fractional-width gap
Many tutorials pair max-width: 599px with min-width: 600px. That leaves a gap. Viewport widths are not always whole numbers. Browser zoom and high-density screens can produce values such as 599.5px, which is more than 599px and less than 600px. A viewport of that width matches neither query, so neither rule applies.
There are two usual fixes:
- Avoid pairing the two directions. Use only
min-widthqueries and let later rules override earlier ones. - If you need an upper bound, use a fractional value such as
59.99em, or use range syntax with<instead of<=.
The spine's example uses the first technique's cousin: a bounded range that stops just below the next breakpoint.
@media screen and (min-width: 30em) and (max-width: 59.99em) {
.sidebar {
display: none;
}
}This rule covers 30em up to 59.99em. A width such as 59.995em still slips through, but the gap is far smaller than one pixel.
Other viewport size features
Width is the main feature, but the same family has related ones.
| Feature | What it measures | Notes |
|---|---|---|
| `height`, `min-height`, `max-height` | viewport height | Useful for short landscape phones and sticky headers. |
| `aspect-ratio` | width divided by height of the viewport | Written as `16/9`; accepts `min-` and `max-` forms. |
| `orientation` | `portrait` or `landscape` | Portrait means height is at least the width. |
| `device-width` | width of the whole screen | Deprecated. Do not use it. |
device-width reports the screen size, not the window size, so it stays the same when a user resizes a desktop window. It is deprecated in the specification. Use width instead.
A height query helps when a sticky header would otherwise cover most of a short screen.
.site-header {
position: sticky;
top: 0;
}
@media (max-height: 30em) {
.site-header {
position: static;
}
}How the browser evaluates your queries
The browser re-evaluates every media query whenever the viewport changes size, and it turns rules on and off as the results change. The flow for a single query looks like this.
Common errors
- Forgetting the viewport meta tag. Mobile browsers use a wide layout width, so
max-width: 30emrules never match on phones. - Expecting the html font size to change
embreakpoints. Settinghtml { font-size: 20px; }does not change what40emmeans inside a media query. - Writing
min-widthandmax-widthin the wrong order of the cascade. If amax-widthblock sits before a base rule with the same selector, the base rule overrides it. Place base rules first. - Using
device-width. The query ignores the actual window size. - Testing only at round sizes. Try 599.5px, odd zoom levels, and a window with a visible scrollbar.
Exercise
A stylesheet uses @media (max-width: 599px) for phones and @media (min-width: 600px) for tablets. A user zooms a desktop browser until the viewport is 599.5 CSS pixels wide. Which rules apply, and how would you change the queries so no gap exists?
Show answer
Neither query matches, because 599.5px is greater than 599px and less than 600px. Fix it by dropping the max-width query and styling the small screen as the default, with only @media (min-width: 600px) on top. Alternatively, change the upper bound to max-width: 599.99px, or use the range syntax (width < 600px).
Exercise
A page has html { font-size: 24px; } and a query @media (min-width: 40em). The browser's default text size is 16px. At what viewport width in CSS pixels does the query start to match?
Show answer
At 640px. The em unit in a media query resolves against the browser's initial font size (16px), not the font-size set in the page's CSS, so 40em equals 40 × 16 = 640px.