Sub topic 1
The viewport and CSS pixels
Prerequisites
This chapter assumes you know basic HTML and CSS, including the box model and selectors. You should also be comfortable with units such as px, rem, and percentages. No design tool is needed yet.
What the viewport is
The viewport is the area of the browser window where your page is drawn. On a desktop, it is the window minus the toolbars. On a phone, it is nearly the whole screen.
Every responsive layout starts here. Media queries, percentage widths, and vw units all measure against the viewport. If the browser reports the wrong viewport width, none of your breakpoints will match the real screen.
CSS pixels are not device pixels
A device pixel is one physical dot on the screen. A CSS pixel is the unit your stylesheet uses when you write 16px or 320px. The two are not the same size.
Modern phones pack many device pixels into the space of one CSS pixel. The ratio between them is the device pixel ratio (DPR). A phone with a DPR of 3 uses a 3 × 3 block of device pixels to draw one CSS pixel. This is why text stays readable on a high-density screen, and why a phone that is 1170 device pixels wide can report a viewport of only 390 CSS pixels.
| Device | Physical width | DPR | Viewport width in CSS pixels |
|---|---|---|---|
| Typical phone | 1170 px | 3 | 390 |
| Tablet | 1640 px | 2 | 820 |
| Laptop | 1440 px | 1 | 1440 |
The values are examples, and real devices vary. Breakpoints use CSS pixels, so the table's last column is the number you design against.
The problem the meta tag solves
Early mobile browsers faced pages built for desktops. To avoid showing a cramped, broken layout, they laid the page out on a wide virtual viewport, often around 980 CSS pixels. They then shrank the result to fit the screen. The page looked like a tiny desktop site.
That behavior breaks responsive design. On a 390-pixel phone, a media query for max-width: 600px would never match, because the browser claims the viewport is 980 pixels wide.
The viewport meta tag fixes this. Put it in the <head> of every page:
<meta name="viewport" content="width=device-width, initial-scale=1">width=device-widthsets the layout viewport to the device's width in CSS pixels.initial-scale=1sets the starting zoom so that one CSS pixel maps to one unit of the screen's CSS width.
With the tag in place, media queries match the real screen size.
A minimal page to test with
This complete page shows the effect. Open it on a phone, or in your browser's device mode, with and without the meta tag.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Viewport test</title>
<style>
body { font-family: sans-serif; padding: 1rem; }
.note::after { content: "Narrow screen"; }
@media (min-width: 48em) {
.note::after { content: "Tablet or wider"; }
}
</style>
</head>
<body>
<p class="note"></p>
</body>
</html>With the tag, a phone shows "Narrow screen". Without it, many phones show "Tablet or wider", because the browser treats the page as roughly 980 pixels wide.
How the pieces connect
Common errors
- Forgetting the meta tag on one template, so only some pages break on phones.
- Designing frames in device pixels instead of CSS pixels, which gives widths that are two or three times too large.
- Testing only on a resized desktop window, which does not reproduce the virtual viewport problem.
Exercise
A phone has a physical screen width of 1080 device pixels and a DPR of 3. What viewport width in CSS pixels should you design for, and what happens to your media queries if the page has no viewport meta tag?
Show answer
The viewport width is 1080 ÷ 3 = 360 CSS pixels. Without the meta tag, the browser typically lays the page out on a virtual viewport of about 980 CSS pixels, so media queries see a width near 980 instead of 360. Narrow-screen styles do not apply, and the page appears as a shrunken desktop layout.