Skip to main content

The viewport meta tag in a website's HTML is used to control how the webpage is displayed on a mobile device or in a web browser.  It defines the viewport's behaviour and dimensions.  The viewport meta tag helps create a responsive and mobile-friendly design by adjusting the layout and scaling to fit various screen sizes and orientations, particularly on mobile devices.  It helps ensure that text is displayed at a readable and consistent size across different devices and screen sizes.  Here's what the viewport meta tag does and some of its common attributes:

 

Initial Scale

The initial-scale attribute specifies the initial zoom level when the page is loaded. It is often set to 1 to indicate that the page should be displayed at its original size.  A proper initial-scale value can help maintain font size consistency across devices.  Such as:

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

 

Width

The width attribute sets the width of the viewport to the width of the device screen, making sure that the content fits the screen width.  Such as:

<meta name="viewport" content="width=device-width">

By setting width=device-width, you instruct the browser to make the width of the viewport match the width of the device's screen. This is particularly important for responsive design.

The relationship between viewport width and font size is that it helps the browser determine the available width for displaying content.

CSS units like vw (viewport width) can be used to make font sizes and other elements respond to the viewport width.

 

User Scalable

The user-scalable attribute controls whether the user can zoom in or out of the page. Setting it to yes allows the user to zoom, while setting it to no disables zooming.  Allowing users to zoom can be important for accessibility, as it enables them to adjust text size for readability. Such as:

<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes">

 

Minimum and Maximum Scale

You can also set the minimum (minimum-scale) and maximum (maximum-scale) zoom levels.  The minimum-scale and maximum-scale attributes control the range of allowed zoom levels for the page.  They can impact font size by restricting or extending the zoom range. For example, setting a minimum scale can prevent text from becoming too small, ensuring readability.

<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=0.5, maximum-scale=2">

 

Viewport Orientation

You can also control how the page behaves when the device changes orientation (portrait to landscape or vice versa) using the viewport tag.  Furthermore, the viewport tag can lock the orientation to either portrait or landscape, ensuring consistent font size and layout based on the chosen orientation.

For example, you can use the orientation attribute to lock the orientation to either portrait or landscape:

<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes, minimum-scale=0.5, maximum-scale=2, orientation=portrait">

 

In summary, the viewport meta tag is crucial for ensuring that web pages are displayed correctly and responsively on various devices. By setting the attributes appropriately, you can control the scaling, width, and behaviour of your web page to provide a better user experience on different screens and orientations.

 

What happens if the viewport isn't set?

If the viewport meta tag isn't set in a web page, the browser will use its default settings for displaying web content. These default settings may vary between browsers and devices, but some common outcomes of not setting the viewport meta tag include:

 

Inconsistent Layout

Without a specified viewport, the web page may appear zoomed out or too small on mobile devices, making the content challenging to read and interact with.

 

Fixed Width

The page may be displayed at a fixed width, typically 980 pixels wide, which is the default width for many web pages designed for desktop browsers. This can lead to horizontal scrolling on mobile devices.

 

Zooming

Users may be able to zoom in and out of the web page, which can disrupt the layout and cause unintended text resizing. This can impact the page's readability and usability.

 

Incompatibility

Without responsive design considerations, the web page may not adapt to different screen sizes and orientations, resulting in a poor user experience on mobile devices.

 

Text Size Issues

Text may appear too small or too large, depending on the device's screen size and the default settings of the browser.

Not setting the viewport meta tag can lead to a non-optimised and inconsistent display of web content on mobile devices. It can result in readability and usability issues and may not provide an optimal user experience. To ensure that your web page looks and functions well on various devices, it's recommended to use the viewport meta tag with appropriate settings to control how the page is displayed and scales on different screens.