Sub topic 2
Setting the viewport
This chapter assumes you can write a basic HTML document and know that a media query is a CSS rule that depends on a condition such as screen width. Media queries get their own chapter next. Here you will set up the one tag that makes them work on phones.
Why phones need a hint
Before smartphones, most sites were built for desktop screens. To keep those sites readable, mobile browsers render a page on a virtual canvas, often about 980 CSS pixels wide. They then shrink that canvas to fit the physical screen.
This virtual canvas is the layout viewport. Media queries and percentage widths are measured against it. On a phone without the right hint, @media (min-width: 40em) is compared with 980px, not with the roughly 390px of the real screen. Your mobile styles never run, and the page looks like a tiny desktop site.
The meta tag
Put this tag inside <head>:
<meta name="viewport" content="width=device-width, initial-scale=1">It has two settings, separated by a comma:
width=device-widthsets the layout width to the screen width in CSS pixels.initial-scale=1sets the starting zoom to 100%, so one CSS pixel is not enlarged or reduced.
A complete page to test
Save this file and open it on a phone, or in a browser's device mode. The text shows the width that media queries will see.
<!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; }
</style>
</head>
<body>
<p>Layout width: <output id="w"></output> px</p>
<script>
const out = document.getElementById("w");
function show() { out.textContent = window.innerWidth; }
show();
window.addEventListener("resize", show);
</script>
</body>
</html>With the tag, a typical phone reports a number such as 390. Delete the tag and reload, and it reports about 980.
Common errors
- Missing tag. This is the most frequent cause of "my media queries do nothing on my phone."
- Semicolons between values. Write
width=device-width, initial-scale=1with a comma. Browsers are lenient, but the comma form is the standard one. - Fixed widths.
width=600forces a 600px layout on every device. Usedevice-width. - Tag outside
<head>. Keep it in the head with the other metadata. - Disabling zoom. Values such as
user-scalable=noormaximum-scale=1stop people from pinch-zooming.
What the tag does not do
The tag does not make a page responsive. It only makes the browser report the true screen width. Your CSS must still adapt to that width, and a fixed-width element such as a 800px-wide image will still overflow a 390px screen. The following chapters cover the media queries and flexible layouts that handle this.
Exercise
A page has a media query @media (min-width: 40em) { ... } that works in a desktop browser's narrow window but never applies on a 1080px-wide phone with a 390 CSS pixel width. The page has no viewport meta tag. Why does the query match on the phone, and what is the fix?
Show answer
Without the tag, the phone lays the page out on a virtual canvas of about 980 CSS pixels. 40em is about 640px, so the query matches because 980px is wider than 640px, and the desktop styles apply on the phone. The fix is to add <meta name="viewport" content="width=device-width, initial-scale=1"> to the head, so the layout width becomes 390px and the query no longer matches.