Sub topic 2
Setting the viewport
This chapter assumes you can write basic HTML, understand CSS selectors and the box model, and can open browser developer tools. You will learn what the viewport meta tag does, why mobile-first CSS depends on it, and how to avoid the common mistakes.
The problem: the virtual viewport
Early smartphones were released when most websites were built for desktop screens. To avoid showing a cramped, broken page, mobile browsers pretend the screen is wide, usually around 980 CSS pixels. They lay the page out at that width, then shrink the result to fit the real screen.
This wide pretend area is the layout viewport. The part of it that is actually visible is the visual viewport. A page without any instructions gets the wide layout viewport, so text looks tiny and users must pinch to zoom.
This also breaks media queries. A phone that is 390 CSS pixels wide reports a width of about 980 pixels. A rule such as @media (min-width: 48em) would match, and your mobile base styles would never be used.
The fix: the viewport meta tag
Add this tag inside 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 real width of the device in CSS pixels.initial-scale=1sets the first zoom level to 100%, so one CSS pixel maps to one CSS pixel on screen.
With the tag in place, media queries match the real screen, and the small-screen base styles you write in the next chapters apply on phones.
A complete page
Here is a minimal page that is ready for mobile-first styling:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Viewport demo</title>
</head>
<body>
<h1>Hello, small screen</h1>
<p>This text is laid out at the real device width.</p>
</body>
</html>Common errors
- Forgetting the tag. The page looks like a shrunken desktop site and your media queries behave strangely. Check this first when mobile layouts look wrong.
- Fixing the width. Writing
width=600forces that width on every device. Usedevice-width. - Blocking zoom. Adding
user-scalable=noormaximum-scale=1stops people from zooming. Users with low vision rely on zoom, and this fails accessibility guidance (WCAG). Leave these out. - Putting the tag in the wrong place. It belongs in
<head>, not<body>. - Relying on the tag alone. The tag only sets the width. If your CSS uses fixed widths such as
width: 900px, the page will still overflow and scroll sideways.
Checking your work
You can confirm the tag works in a few steps:
- Open the page in your browser's device emulation mode.
- Select a phone size, such as 390 pixels wide.
- Run
window.innerWidthin the console. It should print about 390. - Remove the tag and reload. On many emulators the value jumps to about 980.
Exercise
Exercise
A phone is 390 CSS pixels wide. Your page has no viewport meta tag, and your CSS contains @media (min-width: 48em) { .layout { grid-template-columns: 2fr 1fr; } }. Which layout does the phone show, and why? What single line fixes it?
Show answer
The phone shows the two-column layout. Without the tag, the browser uses a virtual layout viewport of about 980 CSS pixels, which is wider than 48em (768px at the default font size), so the media query matches. Adding <meta name="viewport" content="width=device-width, initial-scale=1"> to the head makes the layout width 390px, so the query no longer matches and the single-column base styles apply.