
The viewport meta tag controls how a web page is displayed on different devices. It can set the viewport width and the initial zoom level, and it can affect whether a user can zoom in or out.
12. Configuring the viewport meta tag
A responsive page needs an appropriate viewport. Without one, a mobile browser may lay out the page as though it were on a larger screen, then shrink it. This can prevent your media queries from behaving as expected and make text difficult to read.
Place the viewport declaration in the <head> of the HTML document.
<meta name="viewport" content="...">
| Property | What it controls |
|---|---|
width | The viewport width. Use width=device-width to match the device width in CSS pixels. |
initial-scale | The initial zoom level. initial-scale=1.0 sets the initial scale to 1. |
minimum-scale and maximum-scale | The minimum and maximum zoom levels. |
user-scalable | Whether zooming is allowed. Some modern browsers may override attempts to disable zoom. |
height | The viewport height. Most responsive pages do not need to set it. |
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My web page</title>
</head>
<body>
<!-- Page content -->
</body>
</html>
12.1. Examples that try to limit zoom
The following examples illustrate two settings you may encounter in existing code. A maximum scale of 1 attempts to prevent zooming in above the initial scale. The user-scalable=no setting attempts to disable zoom. Browser behavior varies. Avoid either restriction on a public page, since users may need to enlarge text or controls.
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<!-- Alternative, also best avoided -->
<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1">
12.2. A responsive viewport that allows zoom
Set the width to device-width and leave out settings that restrict zoom. On a touchscreen, users can then pinch to zoom while keeping the page’s initial responsive layout.
<meta name="viewport" content="width=device-width, initial-scale=1.0">
Practice exercise
Add the viewport meta tag to a page in your own project. Open it on a phone or use your browser’s responsive mode. Check that the layout adapts to the screen and that you can still zoom in.