HTML & CSSEasy
What is the viewport meta tag?
The viewport meta tag, <meta name="viewport" content="width=device-width, initial-scale=1">, tells mobile browsers to use the device's actual width as the layout width and not to zoom out. Without it, phones render the page as if it were about 980px wide and shrink it, so text is tiny and media queries do not work as expected.
Why it is needed
Early mobile browsers assumed sites were built for desktops. They laid pages out on a virtual 980px viewport and zoomed out to fit. The viewport tag opts your responsive design out of that behavior.
Common values
width=device-width: layout viewport equals the screen width in CSS pixels.initial-scale=1: no zoom on load.viewport-fit=cover: extend into the notch and rounded-corner areas on phones; then pad content withenv(safe-area-inset-*).interactive-widget=resizes-content: makes the layout viewport shrink when the on-screen keyboard opens (supported in Chromium and Firefox).
Avoid
maximum-scale=1oruser-scalable=no: blocks pinch-zoom, which fails accessibility guidelines (WCAG 1.4.4 Resize Text). Modern iOS ignores it for accessibility reasons anyway.- Fixed widths like
width=600.
Related CSS
- Viewport units
vw,vh, and the newersvh,lvhanddvh(small, large and dynamic viewport heights), which handle mobile browser toolbars. - Media queries now match real device widths.
Interview tip
Explain the 980px default and the accessibility reason not to disable zoom.
Code example
html
1<!doctype html>2<html lang="en">3<head>4 <meta charset="utf-8">5 <!-- The standard responsive viewport (do not disable user zoom) -->6 <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">7 <title>Viewport demo</title>8 <style>9 body { font-family: system-ui; margin: 0; padding: 16px; padding-inline: max(16px, env(safe-area-inset-left)); }10 .hero { min-height: 100dvh; display: grid; place-items: center; background: #eef2ff; } /* dvh adapts to mobile toolbars */11 @media (max-width: 600px) { h1 { font-size: 1.5rem; } }12 </style>13</head>14<body>15 <section class="hero"><h1>Readable on every phone</h1></section>16 <p>Viewport width: <span id="w"></span>px</p>17 <script>document.getElementById("w").textContent = window.innerWidth;</script>18</body>19</html>
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the HTML & CSS set.