DEV Community

Cover image for Why do you need to add a meta viewport tag inside <head> element? (with example)
Noman Gul
Noman Gul

Posted on • Updated on

Why do you need to add a meta viewport tag inside <head> element? (with example)

Have you ever wondered why you need to add:

<meta name="viewport" content="width=device-width, initial-scale=1">
Enter fullscreen mode Exit fullscreen mode

inside <head> tag and why it is useful?

You're in the right place!

Example

First of all, open this web page in a new tab: http://www.pakistan.gov.pk/

Open developer tools and enable device mode in your browser. This mode simulates the behavior of the web app on a mobile device. Notice that the page is zoomed out to fit the fixed-width content on the screen. This is not a good experience because the content will likely be too small for most users, forcing them to zoom and pan.

bad viewport

Set the visual viewport

1- Edit head element:

Edit head element

2- Add viewport:

Then add viewport meta tag and check the page in device mode. Notice the page is no longer zoomed out and the scale of the content matches the scale on a desktop device. If the content behaves unexpectedly in the device emulator, toggle in and out of device mode to reset it.

<meta name="viewport" content="width=device-width, initial-scale=1">
Enter fullscreen mode Exit fullscreen mode

Add viewport


Explanation

A meta viewport tag gives the browser instructions on how to control the page's dimensions and scaling. The width property controls the size of the viewport. It can be set to a specific number of pixels (for example, width=500) or to the special value device-width, which is the width of the screen in CSS pixels at a scale of 100%. (There are corresponding height and device-height values, which can be useful for pages with elements that change size or position based on the viewport height.)

The initial-scale property controls the zoom level when the page is first loaded. Setting the initial scale improves the experience, but the content still overflows past the edge of the screen.

So then you need to use CSS flex or grid and other properties to make it responsive. But this is the part of another blog story šŸ˜œ

Thanks, Awesome DEVs!

Top comments (8)

Collapse
 
mike_hasarms profile image
Mike Healy

Like in your code it's good to not enforce max-scale=1. Thankfully that's becoming less common now.

If a user wants to pinch and zoom in, even on your responsive design, they probably have a reason to.

Collapse
 
dayvster profile image
Dayvster šŸŒŠ

Fully agree with this statement, the user should not be locked into the settings that the developer sets for them.

Collapse
 
jlrxt profile image
Jose Luis Ramos T.

Interesante gracias por tu aporte

Collapse
 
nomangul profile image
Noman Gul

Gracias hombre

Collapse
 
sararf22 profile image
SaraRF22

Thanks for the info, great post šŸ‘

Collapse
 
rohansawant profile image
Rohan Sawant

It's the little things which make all the difference, isn't it?

Great post! Man!

Collapse
 
nomangul profile image
Noman Gul

Thanks, Buddy! šŸ¤—

Collapse
 
aslasn profile image
Ande

Bruh, the image was appealing. So clicked. But can't just get the time to read the whole. I'm really a slow reader.