Viewport meta tag

On this page 7

The viewport meta tag is an HTML element in the page head, written as meta name="viewport" with a content attribute, that tells mobile browsers how wide the layout viewport should be and how the page should be scaled when it first loads. In the mobile web, it is the switch that turns off desktop emulation: without it, phones render pages as if on a screen about 980 CSS pixels wide and then shrink them to fit.

The standard declaration is width=device-width, initial-scale=1. It makes the layout width equal to the device width in CSS pixels, so media queries and responsive layouts respond to the real screen size instead of a virtual desktop.

How the viewport meta tag changes rendering

The viewport meta tag changes rendering by setting the size of the layout viewport, the area the browser uses to calculate CSS layout, percentages and media queries. Early smartphone browsers adopted a wide default layout viewport so that desktop-only sites would not break, letting users pinch to zoom into them.

Mobile browsers distinguish two viewports. The layout viewport is the box the page is laid out in, while the visual viewport is the part currently visible on screen, which shrinks when the user zooms or when the on-screen keyboard appears. The meta tag controls the layout viewport and the initial zoom level of the visual viewport.

AspectWithout the tagWith width=device-width, initial-scale=1
Layout widthDefault of about 980 CSS pixelsDevice width, often 360 to 430 CSS pixels on phones
Media queriesMatch desktop breakpointsMatch the real phone width
Initial appearanceZoomed-out page, tiny textReadable text at normal size
Text inflationBrowser may enlarge text heuristicallyUsually not applied
Tap behaviorPossible delay for double-tap zoom detectionImmediate taps in modern browsers
Rendering with and without the viewport meta tag

CSS pixels are not device pixels. A phone with a device pixel ratio of 3 that is 390 CSS pixels wide has 1,170 physical pixels across. The browser handles that mapping; device-width refers to the width in CSS pixels, which is what designers should work with.

Viewport meta tag properties

The content attribute accepts a comma-separated list of key-value properties. Only width and initial-scale are needed for most sites; the others should be used deliberately.

PropertyTypical valueEffect
widthdevice-widthSets the layout viewport width
initial-scale1Sets the zoom level on first load
minimum-scale and maximum-scaleUsually omittedLimit how far users can zoom
user-scalableUsually omittedCan disable pinch to zoom, harms accessibility
viewport-fitcoverExtends content under notches and rounded corners
interactive-widgetresizes-contentControls how the on-screen keyboard resizes the page
Properties of the viewport content attribute

With viewport-fit=cover, content extends into the areas around a display cutout, and the page must use the CSS env() function with safe area inset values to keep text and buttons clear of notches and the home indicator. The interactive-widget property, supported in Chromium-based browsers, decides whether the on-screen keyboard shrinks the layout viewport or only the visual viewport.

Zoom restrictions and accessibility

Setting user-scalable=no or maximum-scale=1 prevents users from zooming, which creates a serious accessibility barrier for people with low vision. Accessibility guidelines such as WCAG require that text can be resized to 200 percent without loss of content or function, and blocking pinch to zoom works against that requirement.

Browsers have responded by ignoring zoom restrictions in many cases: Safari on iOS has allowed users to zoom regardless of these settings for years, and several Android browsers offer a setting to force zoom. The restriction therefore rarely achieves its aim and mainly causes audit failures and frustration on browsers that respect it.

A common reason developers add zoom restrictions is to stop iOS Safari from zooming into form fields. That zoom happens when an input’s font size is below 16 CSS pixels; the correct fix is to set form controls to at least 16 px, not to disable zoom for everyone.

The viewport meta tag is the foundation of responsive design on phones, but it only sets the stage. Layout decisions are made by CSS, and several newer features address viewport behavior that the tag cannot control.

TechniqueWhat it doesRelation to the viewport meta tag
Media queriesApply styles by viewport width and featuresOnly reflect real widths when the tag is set
Mobile-first designStarts styles at small widths and adds complexityAssumes device-width layout
Dynamic viewport unitssvh, lvh and dvh account for browser toolbarsSolve height issues the tag cannot
Web app manifestControls display mode of installed web appsSeparate file, works alongside the tag
Visual Viewport APIReports the visible area and keyboard changesLets scripts react to zoom and keyboards
Related techniques and how they complement the tag

A CSS rule for viewport settings was once proposed as a replacement for the meta tag but was abandoned. The meta tag remains the standard mechanism and is included in essentially every modern page template.

Common mistakes

The most common mistake is omitting the tag entirely, which makes a responsive stylesheet appear broken on phones because the browser lays out the page at desktop width. Search engines that evaluate mobile usability flag such pages as not mobile-friendly.

  • Fixed widths such as width=600 break on devices of other sizes; use device-width.
  • Disabling zoom harms accessibility and is often ignored anyway.
  • Multiple viewport tags produce browser-dependent results; declare one in the head.
  • Fixed-width elements wider than the screen still cause horizontal scrolling despite a correct tag.
  • Relying on 100vh for full-height layouts leads to content hidden behind browser toolbars.

Put simply

The viewport meta tag is a short line of code at the top of a web page. It tells a phone’s browser to fit the page to the phone’s real screen. Without it, the browser pretends the screen is as wide as a computer. It then shrinks the page, and the text becomes tiny.

It is like telling a tailor your real measurements. With the right size, the page fits and can rearrange itself neatly for a small screen.

In practice

A city library launches a redesigned website with a responsive stylesheet that stacks columns below 600 pixels. On phones, however, visitors see a miniature desktop layout with three columns and unreadable text, and the mobile usability report lists every page as having content wider than the screen.

A developer finds that the page template lacks the viewport declaration. Adding width=device-width, initial-scale=1 to the head makes the single-column mobile layout appear immediately. The developer also removes a maximum-scale=1 setting copied from an old template and raises form field text to 16 px, so the search box no longer triggers automatic zoom and visitors can still pinch to zoom into book covers and opening hours.