skip to content

Meta Tags and Viewport

The core meta tags and what each actually controls — most importantly the viewport tag, without which a responsive stylesheet does nothing on a phone. Interviewers ask why media queries appear to be ignored on mobile, and this is the answer.

part ofHTMLoverview, primer and where to startread it →
on this pageshow

questions

5

A responsive page opens on a phone rendered at desktop width and zoomed out, and its media queries appear to be ignored. What does `<meta name="viewport" content="width=device-width, initial-scale=1">` change, and why is the page broken without it?

level: juniorimportance: must knowfreq 80%

answer

  1. two viewports, layout and visual
  2. browser assumes a wide default page
  3. about 980 CSS pixels, then scaled down
  4. media queries measure the layout viewport
  5. width=device-width, initial-scale=1

basics

~20 s

Without a viewport meta tag, phone browsers lay a page out in a pretend wide viewport (about 980 CSS pixels) and shrink the result to fit the screen. width=device-width, initial-scale=1 makes the layout width match the device, so width-based media queries see real sizes.

solid answer

~50 s

Mobile browsers keep two viewports: the **layout viewport** the page is laid out into, and the **visual viewport** you actually see. To keep old desktop-only sites usable, a phone browser defaults the layout viewport to roughly 980 CSS pixels and then scales the whole rendering down to fit the screen. That is why the page looks like a shrunken desktop site and why `@media (max-width: 600px)` never matches: the layout viewport really is 980 pixels wide, so the query is honestly false. `<meta name="viewport" content="width=device-width, initial-scale=1">` tells the browser to size the layout viewport to the device's own width in CSS pixels and start at 100% zoom, so media queries, percentage widths and `vw` units all measure the real screen. The tag does not make a page responsive by itself — it just stops the browser from lying about the width your CSS is designed against.

code

html · 11 lines
html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Responsive page</title>
  </head>
  <body>
    <p>Media queries now measure the real device width.</p>
  </body>
</html>

go deeper

for a junior

Be able to write <meta name="viewport" content="width=device-width, initial-scale=1"> from memory and say plainly that without it a phone renders the page at a fake desktop width and shrinks it.

for a middle

Explain the layout viewport versus visual viewport split, the roughly 980-pixel legacy default, and why a width media query is evaluated in CSS pixels against the layout viewport rather than against the hardware pixel count.

for a senior

Show you would diagnose this from the symptom: page legible but shrunken on device, breakpoints behaving as if desktop. Know that it is invisible on a laptop and that emulation or a real device is the check, and know where templates lose the tag.

for a principal

Own it as a platform default: the tag belongs in the one shared document shell every page type inherits, with a check that catches any route rendering its own head, so no team can ship a page that silently loses mobile layout.

## Why phones have two viewports When smartphones shipped, essentially every site on the web assumed a desktop-sized window. If a phone had laid those pages out at its real width — 320 to 430 CSS pixels — fixed 960-pixel layouts would have overflowed and broken. So mobile browsers introduced a split: - The **layout viewport** is the rectangle the page is laid out into. It is what percentage widths resolve against, what `vw` units measure, and what width-based media queries test. - The **visual viewport** is the part of that layout the user is currently looking at. Pinch-zooming and scrolling move the visual viewport around inside the layout viewport; they do not re-run layout. By default a phone browser sets the layout viewport to a wide value — roughly 980 CSS pixels, with the exact number varying by browser — lays the page out there, then scales the rendering down so the whole thing fits the screen. A legacy desktop site therefore appears intact but tiny, and the user zooms in to read it. That default is a compatibility hack for the pre-mobile web, and it is still the behaviour you get today when the tag is missing. ## What the tag actually does The viewport meta tag opts a page out of that hack: ```html <meta name="viewport" content="width=device-width, initial-scale=1"> ``` The `content` attribute is a comma-separated list of key/value pairs: - `width=device-width` sets the layout viewport width to the device's width in CSS pixels. You can also give a fixed number (`width=480`), but that re-creates the original problem on a different device. - `initial-scale=1` sets the zoom level at which the page is first displayed: 1 means 1 CSS pixel maps to 1 device-independent pixel, i.e. no initial zoom. It also matters historically: on older iOS, a page with only `width=device-width` could fail to re-lay-out correctly after rotation, and pairing it with `initial-scale=1` was the standard fix. The pair is now the boilerplate. - `minimum-scale` / `maximum-scale` bound how far the user may zoom out or in. - `user-scalable=yes|no` says whether the user may zoom at all. - `viewport-fit=cover` lets the page paint into the display cutout area on notched devices instead of being letterboxed into the safe area, and is what enables the `env(safe-area-inset-*)` values so you can pad content back out of the notch. Note that `width` and `initial-scale` interact: if you give only `initial-scale=1`, the browser derives a layout width from the scale, which on most devices ends up equal to `device-width` anyway. Specifying both is the unambiguous form. ## Why media queries "stop working" This is the interview payoff. A width media query is evaluated against the layout viewport, not against the physical screen and not against the device's pixel count. Without the tag the layout viewport is ~980 CSS pixels on every phone, so: ```css @media (max-width: 600px) { /* never matches on a phone without the viewport tag */ } ``` is correctly reporting that the viewport is wider than 600 pixels. Nothing is wrong with the CSS; the browser was told nothing about the device, so it used the legacy default. Add the tag and the same query matches immediately. A related confusion is device pixel ratio. A phone advertising a 1170-pixel-wide screen is usually 390 CSS pixels wide at a ratio of 3. Media queries work in CSS pixels, so `max-width: 600px` is about the CSS width, never the hardware pixel count. ## What the tag is not - It is not responsiveness. It does not resize images, reflow columns, or scale text. It only fixes the width your stylesheet is measured against; the layout work is still yours. - It is not a desktop concern. Desktop browsers lay out into the window and ignore the tag, which is why a bug caused by its absence is invisible until you open the site on a real phone or in device emulation. - It is not a place for clever values. `width=1024` or a hard `maximum-scale` pins users to your assumptions; `width=device-width, initial-scale=1` is the answer in nearly every real project. ## How it fails in practice The tag lives in `<head>` and is read by the parser as the document is built. Typical production bugs are boring: the tag was never added to a hand-written template, a page type renders its own head and omits it, or an email/webview wrapper strips it. The symptom is always the same — the page is legible but shrunken on mobile and every breakpoint behaves as if the browser were a desktop.

  • What does `initial-scale=1` add if `width=device-width` is already present?
    It fixes the starting zoom at 100% rather than letting the browser pick one, and the two keys interact: given only a scale, the browser derives a layout width from it. The pair also became boilerplate because older iOS versions failed to re-lay-out correctly on rotation with `width=device-width` alone. Specifying both is the unambiguous, portable form.
  • What does adding `viewport-fit=cover` to the content list change?
    By default a notched or rounded-display phone lays the page out inside the safe area, leaving bars at the edges. `viewport-fit=cover` extends the layout to the full display so backgrounds reach edge to edge, and it enables the `env(safe-area-inset-top/right/bottom/left)` values so you can pad interactive content back out from under the cutout and home indicator.
  • Do desktop browsers do anything with the viewport meta tag?
    No. On desktop the layout viewport is simply the browser window, so the tag has no effect and its absence is invisible. That is exactly why this bug ships: it looks perfect on the developer's laptop. Device emulation in developer tools does honour the tag, which makes it a reliable way to catch a missing one before release.

Without the tag a phone behaves like someone printing a poster on A0 paper and then photocopying it down to a postcard: everything is there, proportionally correct, and unreadably small.

saying these in an interview costs you the question

  • Says the viewport tag makes a page responsive by itself
  • Thinks media queries measure physical device pixels
  • Adds width=1024 or another fixed pixel width
  • Believes missing viewport is a CSS bug in the media query
  • Assumes desktop browsers need the tag too

context

open as a page

What does `<meta name="description">` actually do for a page in search results, and is it a ranking signal?

level: middleimportance: should knowfreq 45%

basics

~20 s

A meta description is not a ranking signal. It is a candidate for the snippet shown under the title in search results, and engines often rewrite it using page text that better matches the query. Its practical value is click-through rate, not position.

open as a page

A team ships `<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">`. What do those last two values do, and why will an accessibility reviewer flag them?

level: middleimportance: should knowfreq 45%

basics

~20 s

maximum-scale=1 and user-scalable=no stop the user from pinch-zooming the page. That removes the main way low-vision users enlarge content, so it fails accessibility review; Safari on iOS has ignored these values since iOS 10, but Android browsers still honour them.

open as a page

In a document's `<head>`, what can `<meta http-equiv="…">` actually deliver, and how does it differ from sending the same directive as an HTTP response header?

level: seniorimportance: should knowfreq 35%

basics

~20 s

Only a short list of pragma directives works in a meta tag — content-type, refresh, default-style, content-security-policy and the legacy x-ua-compatible. Others such as X-Frame-Options and Set-Cookie are ignored in markup, and a meta directive only applies once the parser reaches it.

open as a page

What do `<meta name="theme-color">` and `<meta name="color-scheme">` each control, and which one stops a white flash before a dark stylesheet applies?

level: middleimportance: nice to knowfreq 30%

basics

~20 s

theme-color tints browser UI such as the Android Chrome toolbar and the Safari tab bar. color-scheme declares which schemes the page supports, so the browser paints its default canvas, form controls and scrollbars accordingly — and that is what removes the white flash before dark CSS loads.

open as a page