Without a viewport meta tag, phones render your page at desktop width and shrink it. Learn what each value does, the one line most sites need, and which settings to avoid.
The short answer
#The viewport meta tag tells mobile browsers how wide the page's layout should be. Without it, phones lay the page out as if the screen were about 980 pixels wide and then zoom out, so text is tiny and your media queries never fire. Almost every site needs exactly this line in the head:
_10<meta name="viewport" content="width=device-width, initial-scale=1" />
Why it happens
#When the first smartphones arrived, most websites were built for desktop screens. To avoid breaking them, mobile browsers pretended to have a wide screen (the layout viewport) and showed a scaled-down view of it (the visual viewport). People pinched to zoom in.
That default still applies today. If your page has no viewport tag, a phone uses a wide layout viewport. A media query such as @media (max-width: 600px) does not match, because as far as the CSS is concerned the screen is 980 pixels wide.
The viewport tag opts out of that behaviour. width=device-width makes the layout viewport match the device's width in CSS pixels, and initial-scale=1 starts the page at 100 percent zoom.
How to fix it
#Use the standard value
Add the line from the short answer to every page, usually straight after the charset meta tag. Most frameworks and starter templates include it already. Next.js, for example, adds it for you unless you override it with the viewport export.
Know what the other values do
| Value | What it does | Should you use it? |
|---|---|---|
width=device-width | Layout width equals the device width | Yes |
initial-scale=1 | Starts at 100 percent zoom | Yes |
maximum-scale=1 | Stops zooming in past 100 percent | No |
user-scalable=no | Turns off pinch zoom | No |
minimum-scale | Limits zooming out | Rarely needed |
viewport-fit=cover | Lets the page draw under notches and rounded corners | Only with safe area padding |
interactive-widget | Controls how the on-screen keyboard resizes the page | Optional |
Do not block zooming
maximum-scale=1 and user-scalable=no stop people with low vision from zooming in, which fails WCAG's resize text requirement. iOS Safari ignores them for accessibility reasons anyway, but other browsers do not.
A common reason for adding them is to stop iOS zooming in when someone taps a form field. That zoom happens when the field's font size is under 16 pixels. Fix the cause instead:
_10input,_10select,_10textarea {_10 font-size: 1rem;_10}
Handle notches with viewport-fit
If you want a background or header to run edge to edge on phones with a notch, add viewport-fit=cover and then pad content with the safe area environment variables so it is not hidden.
_10<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
_10.site-header {_10 padding-top: env(safe-area-inset-top);_10 padding-left: env(safe-area-inset-left);_10 padding-right: env(safe-area-inset-right);_10}
Control the keyboard with interactive-widget
By default, when the on-screen keyboard opens, most mobile browsers shrink only the visual viewport and leave the layout alone. interactive-widget=resizes-content asks the browser to shrink the layout viewport instead, which helps chat apps keep an input bar above the keyboard.
_10<meta name="viewport" content="width=device-width, initial-scale=1, interactive-widget=resizes-content" />
This is supported in Chromium browsers and Firefox on Android, but not in Safari yet, so treat it as an enhancement.
Common pitfalls
#- Adding a fixed width such as
width=1024. That brings back the desktop layout on phones. - Leaving out the tag on some pages, such as error pages or pages rendered by a different template.
- Adding the tag twice with different values. Browsers use one of them, and it may not be the one you expect.
- Using semicolons between values. Separate them with commas.
- Disabling zoom to hide a layout bug. Find the element that is wider than the screen instead; it is usually a long word, a table or an image without
max-width: 100%.
Related reading
#Was this resource helpful?