HTML Viewport Metadata for Responsive Pages
Match the device width
The layout viewport is the area used for page layout, measured in CSS pixels. width=device-width and initial-scale=1 are a useful starting point for mobile-friendly pages. CSS must still adapt the layout.
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <title>Metadata practice</title> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <h1>Reading guide</h1> <p>Resize the viewport.</p> </body></html>The declaration is available in head. Mobile layout is tested separately.
Allow user zoom
Do not use user-scalable=no or a restrictive maximum-scale to prevent zoom. Users may need magnification; browser enforcement varies. Keep the ordinary declaration without restrictive zoom directives.
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <title>Metadata practice</title> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <p>Readable text remains important.</p> </body></html>No zoom restriction is declared. This does not simulate a pinch gesture.
Metadata does not fix wide elements
This example uses one CSS rule, max-width, to limit an image to its container. The viewport declaration alone cannot fix oversized content. The data SVG is a self-contained test image.
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <title>Metadata practice</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <style>img { max-width: 100%; height: auto; }</style> </head> <body> <img width="800" height="100" src="data:image/svg+xml,%3Csvg%20xmlns=%27http://www.w3.org/2000/svg%27%20width=%27800%27%20height=%27100%27%3E%3C/svg%3E" alt="Empty sample banner"> </body></html>The image fits the available body width at a narrow viewport.
Common mistakes and practice
Test narrow widths, large text and zoom. CSS pixels are not necessarily physical screen pixels.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
HTML Viewport Metadata for Responsive Pages — Knowledge Check
Apply the metadata concepts taught in this lesson.
What does width=device-width configure?
Does viewport metadata replace responsive CSS?
Why avoid disabling zoom?
What does initial-scale=1 express?
Where is the viewport declaration normally placed?