Sub topic 3
Setting the viewport so the page renders at device width
This chapter assumes you know basic HTML structure, CSS units, and how to open your browser's developer tools. By the end, you will know what the viewport meta tag does, why a page needs it, and how to spot the common mistakes.
The problem: mobile browsers pretend to be wide
Early phones met websites built for desktop monitors. To avoid showing a tiny slice of a page, mobile browsers began rendering every page on a wide virtual canvas, often around 980 CSS pixels. They then scaled that canvas down to fit the screen.
That is why a page without any mobile setup looks like a shrunken desktop site. The text is tiny, and the user must pinch and zoom. Your media queries also misbehave, because the browser reports a width of about 980px even on a phone that is 390px wide.
Two viewports
Two terms help here.
- The layout viewport is the area the browser uses to lay out your page. CSS widths, percentages, and media queries are measured against it.
- The visual viewport is the part of the page the user sees on the screen right now. Pinch-zoom changes it.
Without instructions, the layout viewport is wide and the visual viewport is a zoomed-in window onto it. The meta tag tells the browser to make the layout viewport match the device width.
The tag
Put this in the <head> of every page:
<meta name="viewport" content="width=device-width, initial-scale=1">It has two parts:
width=device-widthsets the layout viewport to the screen width in CSS pixels. A phone that reports 390 CSS pixels gets a 390px layout.initial-scale=1sets the starting zoom to 100%, so one CSS pixel maps to one CSS pixel at load.
A CSS pixel is not a hardware pixel. A modern phone may have three hardware pixels per CSS pixel, and the browser handles that conversion for you.
A complete page to try
Save this as viewport.html and open it on a phone or in device emulation:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Viewport demo</title>
<style>
body {
font-family: system-ui, sans-serif;
font-size: 1rem;
line-height: 1.5;
margin: 1rem;
}
</style>
</head>
<body>
<h1>Viewport demo</h1>
<p>This text should be readable without zooming.</p>
</body>
</html>Now delete the viewport line and reload. On a real phone, or in emulation, the text shrinks because the browser renders at the wide default width and scales it down. Put the line back and the text returns to a readable size.
Why this is the base of mobile-first CSS
Every min-width query in the next chapters relies on the browser reporting a true width. If the layout viewport is 980px, a rule such as @media (min-width: 40em) matches on a phone and your "enhanced" layout loads on the smallest screen. The single-column base layout can only work when the viewport tag is in place.
Common mistakes
- Leaving the tag out. The page renders at a wide width and is scaled down.
- Misspelling a value. Browsers ignore what they do not recognise, so
device-widhtfails with no error. - Placing it outside
<head>. Put the tag in the head, before any rendering depends on it. - Fixed-width content. A 1000px-wide image or table still overflows a 390px viewport. The tag does not shrink content, and it only sets the width you are laying out into.
- Disabling zoom. Adding
user-scalable=noormaximum-scale=1stops people from enlarging text. This harms users with low vision, and you should avoid it.
Checking your work
- Open developer tools and turn on device emulation.
- Pick a phone profile and reload.
- Confirm the text is readable at 100% zoom and that there is no horizontal scrollbar.
- Confirm
window.innerWidthmatches the profile's width.
The diagram shows what the tag changes.
Exercise
A page has no viewport meta tag. On a phone, the rule @media (min-width: 40em) { .card-list { grid-template-columns: repeat(2, 1fr); } } applies even though the phone screen is narrow. Why does this happen, and what is the fix?
Show answer
Without the tag, the mobile browser lays the page out at a wide default width of about 980 CSS pixels, which is wider than 40em (640px at the default font size). The media query therefore matches. Add <meta name="viewport" content="width=device-width, initial-scale=1"> inside the <head> so the layout width equals the real device width and the query only matches on wider screens.