Responsive Mobile Ready WebPage Using ViewPort Meta Tag Example

When you create an Html web page, of course, you want it to be viewable and user-friendly on all screen-sized devices include the mobile device, PC, and tablets. But before the viewport meta tag is introduced in Html, the mobile devices can not display the web page as well as desktop. This article will introduce viewport meta tag usage with examples.

1. ViewPort Meta Tag Example.

1.1 WebPage Without ViewPort Meta Tag.

  1. First, let’s see a web page example as below. There is not the viewport meta tag in it.
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Mobile Ready WebPage Demo</title>
    </head>
    <body onload="javascript:window.scrollTo(0, 10);">
    <h3>ViewPort Meta Tag</h3>
    <p>ViewPort meta tag is very helpful when you want your web page mobile friendly. Because mobile screen is smaller than PC.
    So if do not use viewport meta tag, the web page will have a not easy to view style in mobile device.</p>
    </body>
    </html>
  2. To browse the above Html webpage on an android tablet device, you need first install a web server such as tomcat. You can read the article How To Install Tomcat 9.0 Correctly On Your Machine and How To Install Tomcat In macOS to learn how to do it.
  3. After tomcat installation, you should create a subfolder demo under tomcat_home_directory/webapps folder, and then copy and save the above Html code in a file called mobile-ready-responsive.html ( saved in folder tomcat_home_directory/webapps/demo ).
  4. Now open a web browser on an android tablet and browse url http://10.0.2.2:8089/demo/mobile-ready-responsive.html then you can see the web page like below.
  5. You can see that the content data can not be fully visible without scroll horizontally. This is because the mobile web browser thinks the web page is built for desktop resolution.
    browse-webpage-in-android-tablet-without-viewport-meta-tag

1.2 WebPage Use ViewPort Meta Tag.

  1. To resolve the above issue, we add a viewport meta tag in the Html head section.
  2. The viewport meta tag will tell the mobile web browsers how to display the web page on a mobile device.
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width; user-scalable=0;" />
        <title>Mobile Ready WebPage Demo</title>
    </head>
    <body onload="javascript:window.scrollTo(0, 10);">
    <h3>ViewPort Meta Tag</h3>
    <p>ViewPort meta tag is very helpful when you want your web page mobile friendly. Because mobile screen is smaller than PC.
    So if do not use viewport meta tag, the web page will have a not easy to view style in mobile device.</p>
    </body>
    </html>
  3. As you can see in the above code, the <meta name=”viewport” line of code in the head tag section is just the viewport meta tag. It tells mobile web browsers the web page width will fully fill the device width, and the user can not scale it ( zoom in or out ).
  4. Now copy and save the code to the tomcat demo folder again. Browse the page from a mobile web browser, you can see the below picture. You can see the content data line is wrapped automatically to adapt the device width.
    browse-web-page-that-has-viewport-meta-tag-in-android-tablet-web-browser

2. ViewPort Meta Tag Properties.

  1. Below is a list of viewport meta tag properties that you can use.
  2. width: The width of the viewport, the value can be device-width or number of pixels ( not recommended ).
  3. height: The viewport height. The value can be device-height or number of pixels.
  4. user-scalable: Whether or not the user can scale the screen. 1yes, or true means permit. 0no, or false means not allowed.
  5. initial-scale: The initialize scale of the screen when a web page is loaded. The value can be any number. For example, if the property value is 1.0 that means 1 viewport pixel will be scaled to 1 screen pixel.
  6. minimum-scale: The minimum scale value that can be applied to the screen. (0 to 10 ).
  7. maximum-scale: The maximum scale value that can be applied to the screen. ( 0 to 10 ).

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.