In short
The <meta name="viewport"> tag tells the mobile browser to display the page at the real width of the screen instead of simulating a desktop screen. Without it, the site displays as a miniature and the visitor has to zoom. Once the task is done, every page contains a single tag, in the <head>, with the value width=device-width, initial-scale=1, and zooming remains allowed.
Why it matters
Without instructions, a mobile browser lays out the page on a virtual window wider than the screen, then shrinks it to fit. MDN gives the example of a 640 px screen on which the page is laid out at 980 px before being shrunk. The result: tiny text, links impossible to aim at, and media queries in your CSS that never fire, since the browser believes it has a large screen.
The width=device-width value matches the layout width to the device width, and initial-scale=1 sets the starting zoom to 100%. The documentation for the Lighthouse “Optimize viewport for mobile” insight requires both: a defined width and an initial-scale greater than or equal to 1, present from the first render. It specifies that a poorly configured viewport can delay touch interactions by up to 300 ms, the time the browser needs to check that it is not a double tap.
On the search engine side, Google indexes the mobile version of pages. A page without a viewport remains indexable, but it is tedious to use on the phone where most visitors arrive.
What it improves
- Mobile display The page displays at the right width and the responsive CSS applies.
- Responsiveness No wait for a double tap on interactions, which helps INP (Interaction to Next Paint, responsiveness to interactions).
- Accessibility By leaving zoom allowed, people with low vision can enlarge the page.
- Lighthouse audit The “Optimize viewport for mobile” insight turns green, and the accessibility audit on blocked zoom goes away.
- Rankings No proven direct effect. It is a basic condition for correct mobile display, not a signal in itself.
How to check
Read the tag in the page's raw HTML:
curl -s -A "Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0 Mobile Safari/537.36" https://example.com/ | grep -io '<meta[^>]*name=["'"'"']\?viewport[^>]*>'
Compliant result, a single line:
<meta name="viewport" content="width=device-width, initial-scale=1">
No line means there is no tag in the initial HTML. Two lines mean a duplicate, often the theme plus a plugin. Repeat the test on an article page, a product page and a category page: a template may lack it.
In the developer tools console, this command gives the number of tags and their content after JavaScript has run:
[...document.querySelectorAll('meta[name="viewport"]')].map((meta) => meta.content)
Other ways:
- PageSpeed Insights, in mobile mode: the “Optimize viewport for mobile” insight in the Performance section, and an accessibility audit flagging
user-scalable="no"or amaximum-scalebelow 5. - Screaming Frog connected to the PageSpeed Insights API: Mobile tab, “Viewport Not Set” filter, to check the whole site.
- Visual test: on a phone, a page without a viewport looks like a miniature desktop site.
How to implement
Before you start
The recommended value, to place in the <head>, right after <meta charset>:
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
Rules that apply everywhere:
- A single tag per page. With two tags, the behavior depends on the browser.
- In the initial HTML, not added by JavaScript after loading.
- Do not block zooming. Remove
user-scalable=no,maximum-scale=1and anymaximum-scalevalue below 5. MDN points out that blocking zoom prevents people with low vision from reading the page, and WCAG criterion 1.4.4 requires that text can be enlarged to 200%. Safari on iOS has in fact ignored these values by default since iOS 10. - No fixed width such as
width=1024: the page would no longer adapt to screens.
Useful options, to add only if you know what you are doing:
| Value | Effect | When to use it |
|---|---|---|
viewport-fit=cover |
The page fills the whole screen, including under the notch | Only with env(safe-area-inset-*) margins in the CSS |
interactive-widget=resizes-content |
The virtual keyboard shrinks the layout area | Bars or buttons fixed to the bottom of the screen hidden by the keyboard; browser support varies |
Example with the notch handled:
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
.site-header {
padding-left: max(1rem, env(safe-area-inset-left));
padding-right: max(1rem, env(safe-area-inset-right));
}
WordPress
Versions WordPress 5.9 and later, up to 7.1.
- Block theme (Twenty Twenty-Two and later): WordPress adds
<meta name="viewport" content="width=device-width, initial-scale=1" />to the<head>itself. Nothing to do, except remove a duplicate added by a plugin. - Classic theme: the tag must be in the theme's
header.php. If it is missing, do not edit the parent theme, which the next update would overwrite: add it from a child theme or a small plugin.
// wp-content/themes/mon-theme-enfant/functions.php
add_action('wp_head', function (): void {
echo '<meta name="viewport" content="width=device-width, initial-scale=1">' . "\n";
}, 1);
If the tag exists but blocks zooming, copy header.php into the child theme and fix the line. Then clear the cache of the caching plugin and of the CDN.
Shopify
Versions Horizon and Dawn Online Store 2.0 themes, all plans, November 2026.
The official themes already contain the tag: Dawn declares <meta name="viewport" content="width=device-width,initial-scale=1"> in layout/theme.liquid; Horizon declares width=device-width,initial-scale=1,viewport-fit=cover in snippets/meta-tags.liquid, called from layout/theme.liquid. To check or fix it: Online Store > Themes, the theme's “…” menu, “Edit code”, then the file in question. Duplicate the theme before any change, then fix any maximum-scale=1 or user-scalable=no value inherited from an older theme.
PrestaShop
Versions PrestaShop 8 and 9, Classic and Hummingbird themes.
The Classic and Hummingbird themes declare the tag in templates/_partials/head.tpl, head_viewport block. To change it in a child theme, override this block:
{extends file='parent:_partials/head.tpl'}
{block name='head_viewport'}
<meta name="viewport" content="width=device-width, initial-scale=1">
{/block}
Clear the cache in Advanced Parameters > Performance.
Magento
Versions Magento Open Source and Adobe Commerce 2.4.6 to 2.4.9, Blank and Luma themes.
The Blank theme, and Luma which inherits from it, declare the tag in Magento_Theme/layout/default_head_blocks.xml with width=device-width, initial-scale=1. A custom theme can override it in app/design/frontend/Fournisseur/theme/Magento_Theme/layout/default_head_blocks.xml (Fournisseur stands for your vendor name):
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
</head>
</page>
Then bin/magento cache:flush.
Drupal
Versions Drupal 10 and 11.
Drupal core adds the viewport tag (width=device-width, initial-scale=1.0) to every page, whatever the theme. Do not add it a second time in html.html.twig. If a contributed theme has done so, remove the line from the template, then clear the caches (drush cr).
Joomla
Versions Joomla 5 and 6, Cassiopeia template.
The Cassiopeia template already declares it in its index.php ($this->setMetaData('viewport', 'width=device-width, initial-scale=1')). For another template, check templates/nom-du-gabarit/index.php (the folder of your template) and add the same PHP line if it is missing, preferably in a copy of the template.
Wix
Versions Wix Editor and Wix Studio, all plans, November 2026.
Automatic. Wix manages the viewport tag and the mobile version; it cannot be changed.
Squarespace
Versions Squarespace 7.1, all plans, November 2026.
Automatic, with no setting. Do not add a second tag through code injection.
Webflow
Versions Webflow, all site plans, November 2026.
Automatic: Webflow publishes <meta content="width=device-width, initial-scale=1" name="viewport"> on every page. Do not add another one in the <head> custom code.
Laravel
Versions Laravel 10 to 13.
In the main layout, for example resources/views/layouts/app.blade.php. The layouts provided by Laravel and its starter kits already include it:
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>@yield('title')</title>
</head>
Check secondary templates (error pages, emails displayed in the browser, payment layouts) that do not inherit from the main layout.
Symfony
Versions Symfony 6.4, 7.4 and 8.x.
In templates/base.html.twig. The TwigBundle recipe only includes it since Symfony 8.2: projects created with an earlier version only have <meta charset="UTF-8">, so add the line yourself:
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{% block title %}Home{% endblock %}</title>
</head>
Next.js
Versions Next.js 14 to 16 (App Router); Next.js 13 to 16 (Pages Router).
App Router
Next.js automatically adds <meta name="viewport" content="width=device-width, initial-scale=1">. Do not place it in app/layout.tsx by hand. To customize it, export a viewport object, without reintroducing zoom blocking:
// app/layout.tsx
import type { Viewport } from 'next'
export const viewport: Viewport = {
width: 'device-width',
initialScale: 1,
viewportFit: 'cover',
themeColor: '#ffffff',
}
The example in the Next.js documentation shows maximumScale: 1 and userScalable: false: do not copy them.
Pages Router
Place the tag in pages/_app.js with next/head, not in _document.js: Next.js advises against it there, because the tag cannot be deduplicated in that file.
// pages/_app.js
import Head from 'next/head'
export default function App({ Component, pageProps }) {
return (
<>
<Head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</Head>
<Component {...pageProps} />
</>
)
}
Nuxt
Versions Nuxt 3 and 4.
Nuxt adds width=device-width, initial-scale=1 by default. To change the value, in nuxt.config.ts:
export default defineNuxtConfig({
app: {
head: {
viewport: 'width=device-width, initial-scale=1, viewport-fit=cover',
},
},
})
Astro
Versions Astro 5 to 7.
Astro's starter templates (basics, minimal) contain <meta name="viewport" content="width=device-width" />, without initial-scale. Complete it in your layout, for example src/layouts/Layout.astro:
<meta name="viewport" content="width=device-width, initial-scale=1" />
Nginx
Versions Nginx 1.24 and later, ngx_http_sub_module module.
For a legacy application you cannot change, Nginx can insert the tag on the fly with the sub_filter module, included in common packages (check with nginx -V 2>&1 | grep -o http_sub_module). In the server block that acts as a proxy:
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header Accept-Encoding "";
sub_filter_types text/html;
sub_filter_once on;
sub_filter '<head>' '<head><meta name="viewport" content="width=device-width, initial-scale=1">';
}
Accept-Encoding "" forces the application to respond without compression, otherwise Nginx cannot read the HTML. The substitution fails if the <head> tag has attributes. This is a stopgap: fix the template as soon as possible. Reload with sudo nginx -t && sudo systemctl reload nginx.
Apache
Versions Apache 2.4, mod_substitute module.
The same stopgap with mod_substitute (sudo a2enmod substitute), in the virtual host:
<IfModule mod_substitute.c>
AddOutputFilterByType SUBSTITUTE text/html
Substitute "s|<head>|<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">|i"
</IfModule>
The filter cannot read a response already compressed by the application. Reload with sudo apachectl configtest && sudo systemctl reload apache2.
Cloudflare
Versions Cloudflare Workers, all plans, including the Free plan, November 2026.
Without access to the code, a Worker can remove a faulty viewport tag and insert the right one. In Workers & Pages, create a Worker, paste this code, then attach it to the example.com/* route:
export default {
async fetch(request) {
const response = await fetch(request);
const contentType = response.headers.get('content-type') || '';
if (!contentType.includes('text/html')) {
return response;
}
return new HTMLRewriter()
.on('head', {
element(head) {
head.prepend('<meta name="viewport" content="width=device-width, initial-scale=1">', { html: true });
},
})
.on('meta[name="viewport"]', {
element(meta) {
meta.remove();
},
})
.transform(response);
},
};
The tag inserted at the top of the <head> is not touched by the second selector, which only sees the original tags. The Workers Free plan has a daily request quota: check that it covers your traffic.
Common mistakes
- Blocking zoom with
user-scalable=noormaximum-scale=1, often to prevent Safari's automatic zoom on form fields. The right fix is text of at least 16 px in the fields. See ensure legible text size (opens in a new tab). - Two viewport tags: theme plus plugin, or layout plus component. Keep one.
- Tag added by JavaScript: it arrives after the first render, too late.
- Forgetting a template: error pages, hosted payment pages, AMP pages or landing pages built outside the theme.
viewport-fit=coverwithout safe-area margins: content slips under the iPhone's notch or home indicator.- Believing the tag is enough: it lets the responsive CSS apply; it does not make a site responsive if it is not.
Frequently asked questions
What value should the viewport meta tag have?
Use width=device-width, initial-scale=1, in a single tag placed in the head of every page. The first value matches the layout width to the screen width, the second sets the starting zoom to 100%. Do not add user-scalable=no or maximum-scale=1, which stop visitors from zooming. Add viewport-fit=cover only if your CSS handles the notch margins.
Should you block zooming with user-scalable=no?
No. Blocking zoom prevents people with low vision from enlarging text and goes against the WCAG criterion that requires text to be resizable up to 200%. Lighthouse flags it in its accessibility audits, and Safari on iPhone has ignored these values by default since iOS 10. To avoid automatic zoom on form fields, give them text of at least 16 px.
Can Google index a page without a viewport tag?
Yes, a missing viewport tag does not prevent indexing. However, the page displays on mobile like a miniature desktop site, with tiny text and links that are hard to tap. Google indexes the mobile version of pages and takes the mobile experience into account among other signals: without a viewport, you degrade the experience of most visitors, which costs you traffic and conversions.
Does Next.js add the viewport tag automatically?
Yes, with the App Router, Next.js automatically adds the viewport tag with the value width=device-width, initial-scale=1, without any configuration. To customize it, export a viewport object from the layout: only the fields you provide replace the default values. With the Pages Router, place the tag in pages/_app.js with the Head component, not in _document.js.
Resources
- MDN: viewport meta tagdeveloper.mozilla.org
- Chrome for Developers: Optimize viewport for mobile insightdeveloper.chrome.com
- W3C: Understanding SC 1.4.4 Resize Textw3.org
- Next.js: generateViewport and the viewport objectnextjs.org
- Nuxt: SEO and Metanuxt.com
- Cloudflare: HTMLRewriterdevelopers.cloudflare.com
- Nginx: sub_filter modulenginx.org
Updated on
Topic checklist (PDF)