Sub topic 1
The viewport and CSS pixels
Every breakpoint decision rests on one number: the width of the viewport, measured in CSS pixels. This chapter explains what that number is, why a phone with a 1170-pixel-wide screen reports about 390, and what the viewport meta tag does. Later chapters build media queries and design frames on this model.
Prerequisites
This chapter assumes you can:
- Write basic HTML and CSS, including selectors and the box model.
- Read CSS units such as
px,rem, and%. - Open browser developer tools and run a line of JavaScript in the console.
You don't need a design tool for this chapter. Frames in Figma or a similar tool come in a later chapter, and they depend on the ideas here.
Three kinds of pixels
"Pixel" means three different things in responsive work.
- A device pixel (also called a physical pixel) is one dot of light on the screen hardware.
- A CSS pixel is the unit the browser uses for
pxin CSS, for layout, and for media query widths. It is an abstract unit defined so that one CSS pixel looks roughly the same size on any device, at normal viewing distance. - The device pixel ratio (DPR) is the number of device pixels per CSS pixel along one axis. A DPR of 3 means a 1 by 1 CSS pixel square is drawn with a 3 by 3 block of device pixels.
A typical modern phone shows how these relate:
| Property | Value |
|---|---|
| Screen in device pixels | 1170 by 2532 |
| Device pixel ratio | 3 |
| Screen in CSS pixels | 390 by 844 |
You design and write CSS against the last row. A width: 390px element fills this phone's screen, even though it covers 1170 device pixels. The extra device pixels make text and images sharper. They don't give you more room to lay things out.
Browser zoom changes the ratio too. When you zoom a desktop page to 200%, the browser doubles the device pixels per CSS pixel. A 1280-wide window then has a viewport of 640 CSS pixels, and your 640px media queries match. This is why responsive layouts also serve users who zoom for readability.
Two viewports
The word "viewport" covers two related things on a phone.
- The layout viewport is the rectangle the browser uses to compute layout. Percentage widths,
vwunits, and media queries refer to it. - The visual viewport is the part of the page currently visible on screen. Pinch-zooming shrinks the visual viewport and moves it around inside the layout viewport. It does not change the layout viewport, so your CSS doesn't re-run when someone pinches.
Zooming with the browser's zoom setting is different. That changes the layout viewport, as shown above.
Why the meta tag exists
Early smartphones met a web full of desktop-only pages. To avoid showing them cropped, mobile browsers pretended the layout viewport was wide, about 980 CSS pixels in many browsers. They laid the page out at that width and then shrank the result to fit the screen. The page looked like a tiny desktop site, and your media queries saw a width near 980, not 390.
The viewport meta tag opts out of this behavior:
<meta name="viewport" content="width=device-width, initial-scale=1">width=device-widthsets the layout viewport width to the device's width in CSS pixels, for example 390.initial-scale=1sets the starting zoom so that one CSS pixel maps to its normal size, with no automatic shrinking.
Put the tag in the <head> of every page. Without it, a phone treats your carefully built layout as a legacy desktop page, and none of your mobile breakpoints apply.
Inspecting the viewport yourself
Measure the values instead of trusting a device list. Save this as a page and open it on a phone, or in your browser's device emulation mode.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Viewport inspector</title>
<style>
body { font-family: system-ui, sans-serif; padding: 1rem; }
pre { font-size: 1rem; }
</style>
</head>
<body>
<h1>Viewport inspector</h1>
<pre id="out"></pre>
<script>
function report() {
const vv = window.visualViewport;
document.getElementById("out").textContent = [
"innerWidth (layout viewport, includes scrollbar): " + window.innerWidth,
"clientWidth (layout width without scrollbar): " + document.documentElement.clientWidth,
"visualViewport.width (changes on pinch zoom): " + Math.round(vv.width),
"devicePixelRatio: " + window.devicePixelRatio,
"screen.width (device-reported, CSS px): " + screen.width
].join("\n");
}
report();
window.addEventListener("resize", report);
window.visualViewport.addEventListener("resize", report);
</script>
</body>
</html>Try three experiments:
- Remove the meta tag and reload on a phone.
innerWidthjumps to about 980. - Restore the tag and pinch-zoom.
visualViewport.widthshrinks whileinnerWidthstays put. - On a desktop browser, press
Ctrland+(orCmdand+on macOS) to zoom. BothinnerWidthanddevicePixelRatiochange.
Viewport units and their traps
CSS gives you units tied to the viewport: vw (1% of the viewport width) and vh (1% of the height). Two problems come up often.
100vw includes the scrollbar. On systems with classic scrollbars, a vertical scrollbar takes about 15 CSS pixels from the page, but 100vw still counts the full window width. An element with width: 100vw is then wider than the space available, and the page scrolls sideways. Use width: 100% for full-width blocks, and keep vw for things like type scaling.
100vh on phones. Mobile browsers hide and show their address bar as you scroll. The old vh unit is based on the largest viewport, with the bar hidden, so a 100vh hero can extend under the bar when it is visible. Modern CSS has three variants:
| Unit | Based on | Use it for |
|---|---|---|
| `svh` | Small viewport: browser bars fully shown | Content that must always be fully visible |
| `lvh` | Large viewport: browser bars hidden | Backgrounds that may extend under the bars |
| `dvh` | Dynamic viewport: current state, updates as bars move | Full-height layouts, with the cost of recalculating as the user scrolls |
Each has a width counterpart (svw, lvw, dvw), though you'll rarely need those. A common full-height pattern is:
.hero {
min-height: 100vh; /* fallback for older browsers */
min-height: 100dvh; /* used where supported */
}Use min-height so the content can still grow past the viewport when the text is long or the user has a larger font size.
What to put in the viewport tag, and what to leave out
Extra values in content are common in copied snippets, and some of them cause harm.
<!-- Avoid: blocks pinch zoom -->
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">Another option is viewport-fit=cover. It lets the page extend into the notch area of some phones. Add it only when you handle the safe areas yourself with env(safe-area-inset-top) and related values. Otherwise leave it out.
Common errors
- Missing meta tag. Symptom: the page looks like a shrunken desktop site on a phone, and mobile styles never apply. Fix: add the tag to the
<head>. - Testing only in a resized desktop window. A desktop window with no meta tag involved can look correct while a real phone shows the 980px fallback. Test on a device or with device emulation, which honors the tag.
- Fixed pixel widths. A
width: 600pximage or table makes a 390px viewport scroll sideways. Prefermax-width: 100%and relative sizes. - Treating screen resolution as layout width. A phone advertised as "1170 pixels wide" lays out at 390. Read the CSS pixel width from developer tools or the inspector above.
- Equating "mobile" with a width. Tablets in portrait, foldables, and small desktop windows all land in the same CSS pixel ranges. A breakpoint describes width, not a device type. This idea returns in the chapters on choosing breakpoints.
What comes next
With the layout viewport set to the real device width, a media query sees the number you expect. The next chapter defines what a breakpoint is, then looks at where those numbers come from.
Exercise
A phone has a 1080 by 2400 pixel screen and a device pixel ratio of 2.5. Its page has the meta tag width=device-width, initial-scale=1. What is the layout viewport width in CSS pixels, and which media query min-width values in pixels would match?
Show answer
The layout viewport width is 1080 / 2.5 = 432 CSS pixels. Any min-width of 432px or less matches, for example min-width: 320px or min-width: 400px. A min-width: 480px query does not match.
Exercise
A page has no viewport meta tag. On a phone, window.innerWidth reports 980, and a rule inside @media (max-width: 600px) never applies. Explain why, and name the one-line fix.
Show answer
Without the tag, the mobile browser lays the page out in a virtual viewport about 980 CSS pixels wide and scales it down to fit the screen, so media queries see about 980 px and the 600px rule never matches. Add <meta name="viewport" content="width=device-width, initial-scale=1"> to the head.
Exercise
A full-width banner uses width: 100vw and causes a horizontal scrollbar on desktop. Why does it happen, and what should replace it?
Show answer
On systems with classic scrollbars, 100vw includes the width taken by the vertical scrollbar, so the banner is wider than the available space. Use width: 100% (or just let a block element fill its container).