← Back to HTML & CSS, page 2

html-css-0028 · Easy fresher

What does the viewport meta tag do, and why is it needed for responsive pages?

Mobile browsers pretend the page is about 980 pixels wide unless told otherwise. The viewport meta tag with width=device-width, initial-scale=1 makes layout use the real device width, so media queries and fluid layouts behave properly. Do not disable user zoom; it harms accessibility.

Full answer

Phones were designed to show pages built for desktop screens. To make those pages usable, a mobile browser lays out the page on a virtual screen about 980 CSS pixels wide, and then shrinks it to fit the real screen. The text is tiny and media queries based on width do not match what you expect.

The viewport meta tag tells the browser to use the real device width as the layout width instead:

On a phone that is 390 CSS pixels wide, this page reports 390 and the min-width: 600px rule stays off. If you remove the meta tag, a phone usually reports about 980, because it falls back to the virtual width, and the page appears zoomed out.

width=device-width sets the layout width to the device width. initial-scale=1 sets the first zoom level to 100 percent. Do not add user-scalable=no or a small maximum-scale. Pinch zoom is an accessibility feature for people with low vision, and blocking it fails accessibility guidelines.

The tag only affects mobile browsers. Desktop browsers ignore it, which is why a responsive page can look fine on a laptop and wrong on a phone when the tag is missing.

html
<!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 { margin: 0; font: 16px/1.5 sans-serif; }
    .box { padding: 1rem; background: #e8eefc; }
    @media (min-width: 600px) { .box { background: #fde9c9; } }
  </style>
</head>
<body>
  <div class="box" id="box">Layout width: <span id="w"></span> CSS px</div>
  <script>document.getElementById("w").textContent = window.innerWidth;</script>
</body>
</html>

This is a complete page. In Chrome mobile emulation at 390px wide it reported 390 with the tag and 980 without it.

Key points

  • Without the tag mobile browsers lay out at a wide virtual width and scale down.
  • width=device-width uses the real device width in CSS pixels.
  • initial-scale=1 sets the first zoom level to 100 percent.
  • Avoid user-scalable=no and tiny maximum-scale values.
  • Desktop browsers ignore the tag.

Common mistakes

  • testing responsive layouts only by resizing a desktop window. Use the DevTools device mode, which honours the meta tag, and test on a real phone.
  • disabling zoom with user-scalable=no to stop layout shifts. Fix the layout instead.

Follow-ups

  • what are CSS pixels?
  • What is the device pixel ratio?
  • Why might a page look fine on a laptop but wrong on a phone?

Tips

  • Make the tag part of your base template so no page forgets it.
  • Put the tag in your base template and say so in the answer.
  • Explain a CSS pixel in one line; it often comes next.