← Back to all tutorials

The Viewport Tag

Learn how to use the viewport meta tag — syntax, properties, and best practices for mobile-friendly pages.

The Viewport Tag

The viewport meta tag is the single most important line of HTML for responsive design. Without it, mobile browsers will render your page at 960–980px width and zoom out, making responsive CSS useless.

The Essential Viewport Tag

<meta name="viewport" 
      content="width=device-width, initial-scale=1.0">

This tag goes inside the <head> of your HTML document, before any CSS links.

Breaking Down the Properties

PropertyValueWhat It Does
widthdevice-widthSets the layout viewport width to match the device screen width
initial-scale1.0Sets the initial zoom level to 100% (no zooming)

What Happens With and Without the Tag

ScenarioLayout ViewportResult
No viewport tag~980px on mobilePage renders at desktop width, then zoomed out to fit — tiny & unreadable
With viewport tag375px (device width)Page renders at actual device width — media queries work correctly

Additional Viewport Properties

The viewport tag supports several other properties:

<meta name="viewport" content="
    width=device-width,
    initial-scale=1.0,
    maximum-scale=5.0,
    minimum-scale=1.0,
    user-scalable=yes
">
PropertyValuesDescription
widthdevice-width or pixelsSets the viewport width
initial-scale0.1 – 10Initial zoom level
maximum-scale0.1 – 10Maximum zoom the user can do
minimum-scale0.1 – 10Minimum zoom the user can do
user-scalableyes or noWhether the user can zoom at all

Important: Never Disable Zooming

You may see websites use:

<!-- ❌ DON'T DO THIS -->
<meta name="viewport" content="width=device-width, 
      initial-scale=1.0, user-scalable=no, maximum-scale=1.0">

Never disable user zooming. This is an accessibility violation:

  • Users with low vision need to zoom in to read text
  • It's flagged as an issue by Lighthouse and accessibility audits
  • Many modern mobile browsers ignore user-scalable=no for this reason
  • WCAG guidelines require that users be able to zoom to at least 200%

The Recommended Viewport Tag

<!-- ✅ Best Practice -->
<meta name="viewport" 
      content="width=device-width, initial-scale=1.0">

This is all you need in the vast majority of cases. Just two properties.

Setting a Fixed Width (Avoid This)

<!-- ❌ Avoid: forces a specific viewport width -->
<meta name="viewport" content="width=1024">

This forces the layout viewport to 1024px regardless of the device, defeating the purpose of responsive design.

Viewport Units in CSS

CSS also has viewport-relative units that are very useful in responsive design:

UnitWhat It RepresentsExample
vw1% of viewport widthwidth: 50vw; = half the screen width
vh1% of viewport heightheight: 100vh; = full screen height
vmin1% of the smaller dimensionUseful for square elements
vmax1% of the larger dimensionLess commonly used
/* Full-screen hero section */
.hero {
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Responsive font size */
h1 {
    font-size: 5vw;      /* Scales with viewport width */
    font-size: clamp(24px, 5vw, 64px);  /* With min/max limits */
}

Complete HTML Starter Template

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, initial-scale=1.0">
    <title>My Responsive Page</title>
    <link rel="stylesheet" href="css/styles.css">
</head>
<body>
    <!-- Your responsive content here -->
</body>
</html>

Key Takeaways

  • The viewport meta tag is required for responsive design to work on mobile
  • Use width=device-width, initial-scale=1.0 — that's all you need
  • Never disable zooming — it's an accessibility violation
  • CSS viewport units (vw, vh) size elements relative to the viewport
  • The clamp() function creates responsive values with minimum and maximum limits