Sub topic 2
Setting the viewport
This chapter assumes you know basic HTML structure, CSS selectors and the box model, and how to open browser developer tools. It covers the one line of HTML that mobile-first CSS depends on. Without it, your media queries and relative units won't behave as the rest of this tutorial describes.
Why mobile browsers need a hint
Early mobile browsers faced a problem. Almost every website in 2007 was designed for screens about 960 pixels wide. A phone is much narrower. If the browser had laid those pages out at the real screen width, they would have broken. So mobile browsers invented a virtual viewport: they lay the page out as if the screen were wide, typically 980 CSS pixels, and then shrink the result to fit the phone. The user pinches and zooms to read it.
That fallback is still active today. If your page does not say otherwise, a modern phone pretends to be about 980 pixels wide. Your carefully written mobile base styles then render on a canvas that is nearly three times wider than the phone. Text looks tiny, and a media query like (min-width: 48em) (768px) matches on a phone, because the browser reports a width of 980px.
The viewport meta tag tells the browser not to do this.
The tag
<meta name="viewport" content="width=device-width, initial-scale=1">Put it inside <head> on every page. The tag has two settings:
width=device-widthsets the layout viewport width to the width of the device in CSS pixels. On a typical phone that is 360 to 430 pixels, not 980.initial-scale=1sets the zoom level at page load so that one CSS pixel maps to one CSS pixel of the ideal viewport. It also prevents some older iOS versions from zooming the page after a rotation to landscape.
Here is a minimal complete page that uses it:
<!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;
margin: 0;
padding: 1rem;
}
.note {
background: #e8eefc;
padding: 1rem;
}
.note::after {
content: " Narrow: below 48em.";
}
@media (min-width: 48em) {
.note::after {
content: " Wide: 48em or more.";
}
}
</style>
</head>
<body>
<p class="note">This box reports which layout is active.</p>
</body>
</html>Open this on a phone, or in device emulation, with and without the meta tag. Without it, the phone shows "Wide" even though the screen is narrow. With it, the phone shows "Narrow", which is what you want.
Three viewports, one name
Developer documentation uses "viewport" for three different things. Keeping them apart helps when you debug.
| Name | What it is | What changes it |
|---|---|---|
| Layout viewport | The rectangle the page is laid out in. Media queries and `vw` units are based on it. | The meta tag, and rotation |
| Visual viewport | The part of the page the user currently sees. | Pinch zoom, panning, the on-screen keyboard |
| Ideal viewport | The width the device manufacturer considers comfortable for that screen. | Fixed by the device |
width=device-width sets the layout viewport equal to the ideal viewport. When a user pinch-zooms in, the visual viewport shrinks but the layout viewport stays the same, so your media queries do not change while the user zooms the page.
CSS pixels are not device pixels
A phone with a 1170-pixel-wide display can report a device-width of 390. The ratio, 3 in this case, is the device pixel ratio. A CSS pixel is a unit of layout, and the browser decides how many hardware pixels draw it. You never need to divide by the ratio in CSS. Write breakpoints and sizes in CSS units, and the browser handles the sharpness. The ratio matters again when you choose image files, which a later chapter covers.
What not to put in the tag
The content attribute accepts more options, and several of them are common mistakes.
Blocking zoom
<!-- Do not do this -->
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">Both lines stop users from pinch-zooming. People with low vision rely on zoom to read. WCAG success criterion 1.4.4 (Resize Text) requires that text can be enlarged to 200% without loss of content, and blocking zoom fails it. Safari on iOS ignores user-scalable=no today, but other browsers may honor it, so don't depend on that. Leave both options out.
Fixed widths
<!-- Do not do this -->
<meta name="viewport" content="width=1024">A fixed number forces the same wide layout on every device. It also defeats the whole mobile-first approach, because your base styles for narrow screens would never see a narrow screen.
Setting only one half
<meta name="viewport" content="width=device-width"> works in current browsers. Including initial-scale=1 costs nothing and covers older behavior, so use the full form shown above on every page.
Common symptoms and their causes
If a page looks wrong on a phone, check these before you debug the CSS.
- Everything is tiny and looks like a desktop site. The meta tag is missing, misspelled (
viewpointis a frequent typo), or placed outside<head>. - The page scrolls sideways. The tag is correct, but some element is wider than the screen. A fixed-width image, a long unbroken string, or a table is the usual cause. The next chapters cover the fixes, starting with relative units.
- The layout is correct, but the wide layout appears on a small phone. Check that your breakpoint uses
min-widthinemorremunits, and that no stray rule outside a media query applies the wide layout. - Desktop emulation looks right, but a real device does not. Device emulation in browser tools sets the viewport for you. A missing tag may still show up on a physical phone, so test on real hardware too.
Extra options for modern devices
Two features matter for current phones. Both are optional, and you should add them only when you need them.
Notches and safe areas
Some phones have a notch, rounded corners, or a home indicator bar. By default, browsers keep your content inside a safe rectangle that avoids them. If you want a full-bleed design, opt in with viewport-fit=cover and then pad your content yourself using the env() function:
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">.app-footer {
padding-block-end: max(1rem, env(safe-area-inset-bottom));
padding-inline: max(1rem, env(safe-area-inset-left));
}The max() call keeps at least 1rem of padding on devices that have no inset, where the env() value is 0. Without viewport-fit=cover, the insets are zero and this code does nothing harmful.
Viewport height and browser toolbars
Mobile browsers show and hide their address bar as you scroll, so the visible height changes. The classic 100vh unit is based on the largest possible height, which means a 100vh element can extend beneath the toolbar. CSS now offers svh (smallest viewport height), lvh (largest) and dvh (dynamic, which follows the toolbar).
.hero {
min-block-size: 100vh; /* fallback for older browsers */
min-block-size: 100svh; /* never hidden behind the toolbar */
}A browser that doesn't understand svh drops the second declaration and keeps the first. This fallback pattern works for any new unit.
The on-screen keyboard
When the keyboard opens, some browsers shrink only the visual viewport, and others also shrink the layout viewport. The interactive-widget option lets you choose, for example interactive-widget=resizes-content. Support is still uneven, so test any form-heavy page on real devices before you depend on it.
How this connects to the rest of mobile-first CSS
The viewport tag is the foundation for the next steps. Relative units and vw need an accurate layout width to calculate from. Your base styles assume a narrow screen. Your min-width queries only fire at the correct moments if the browser reports the real width. If you skip this tag, none of those later techniques will give the results this tutorial shows.
Exercise
A teammate says a page looks like a shrunken desktop site on phones, and a media query @media (min-width: 48em) applies on a 390px-wide phone. The <head> contains <meta name="viewport" content="width=device-width, initial-scale=1"> inside a <body> tag by mistake. What is the cause, what is the fix, and why does the media query match?
Show answer
The meta tag is in the wrong place. Browsers read the viewport tag from <head>, so it is ignored here. The phone falls back to its virtual viewport of about 980 CSS pixels, which is wider than 48em (768px), so the min-width: 48em query matches. Move the tag into <head>, and the layout width becomes the device width (390px), so the query no longer matches.
Exercise
Your design has a full-height hero section, and on phones its bottom edge is hidden behind the browser toolbar. Which CSS unit fixes this, and how do you keep the page working in browsers that don't support it?
Show answer
Use svh (smallest viewport height), for example min-block-size: 100svh. Write a 100vh declaration first as a fallback, then the 100svh declaration after it. Browsers that don't understand svh ignore that line and keep the 100vh value.