Open In App

Why to use viewport value in <meta> tag in HTML ?

The viewport is the content area on the web page that is visible to the user or the page visitor. The viewport does not have a fixed size, it varies with the screen size as the size decreases or increases the viewport.

In earlier times when phones and tablets were not used to access the web pages, the static and fixed-size designs were common to use, but when phones and tablets were introduced to the internet, the fixed-size pages were very large for those screen sizes. Then, to resolve it HTML viewport was introduced.



Syntax:

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

This is the common implementation of the viewport that is used for various device varying web pages.



HTML <Meta> Tag properties:

Example 1: The below example illustrates how the content will look on the web page if we do not use <meta> tag with viewport value. The bottom text is not shown fully as the <meta> tag is not used.




<!DOCTYPE html>
<html lang="en">
  
<head>
    <title>Use of viewport value in meta tag</title>
</head>
  
<body>
    <div class="image">
        <img src=
        alt="GeeksforGeeks">
    </div>
  
    <h2>Welcome to GFG!</h2>
    <h4>A computer science portal for all geeks.</h4>
    <h3>Our Achievements: </h3>
      
    <p>
        Billions of Users, Millions of Articles 
        Published, Thousands Got Hired by Top 
        Companies and the numbers are still 
        growing.
    </p>
</body>
  
</html>

Output:

Example 2: The following code demonstrates the viewport output using the <meta> tag. The output is shown with respect to the iPhone viewport.




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
    <title>Use of viewport value in meta tag</title>
</head>
  
<body>
    <div class="image">
        <img src=
        alt="GeeksforGeeks">
    </div>
  
    <h2>Welcome to GFG!</h2>
    <h4>A computer science portal for all geeks.</h4>
    <h3>Our Achievements: </h3>
      
    <p>
        Billions of Users, Millions of Articles 
        Published, Thousands Got Hired by Top 
        Companies and the numbers are still 
        growing.
    </p>
</body>
  
</html>

Output:


Article Tags :