Responsive Web Design - The Viewport

    What is the Viewport

    Simply put, the viewport defines the display area of a web-page, and it can change from one device to another. For instance, a mobile phone will have a small viewport, and a desktop would have a large viewport. When the web was only available for desktop, developers would build static and fixed-sized web-pages, but now the web is not limited to the desktop. Now with IoT, any devices can access the internet and web-sites, and the display size of all these devices vary by a large margin. If we visit a static or fixed site web-page using a mobile or tablet, we have to zoom in to see the content of the web page. Because to fit the complete website on mobile, the browser scale down the web-page content.

    Viewport in HTML5

    In HTML5 W3C introduced the <meta> tag to control the viewport of a web-page. Add this <meta> tag code to your web-page to make your page more responsive.

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

    The browser reads this tag and automatically adjusts the web-page dimensions. The width=device-width specifies that the width of the page should be equal to the width of the device. And for every device, it automatically changes the overall with of the page. The initial-scale=1.0 specifies the zoom level of the page when the browser loads the HTML document. And by specifying the value 1.0, the zoom level of the page becomes 1. let’s see two examples with and without <meta> viewport tag

    Example with a meta viewport

    <!DOCTYPE html>
    <html>
    <head>
    
        <title>With Meta viewport</title>
    
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
    </head>
    
    <body>
    
        <img src="img.jpg" width="100%" height="auto">
        <p>
             ecived rehtona no neve - ffo tfel uoy erehw srebmemer droW ,dne eht hcaer uoy erofeb gnidaer pots ot deen uoy fI .tnaw uoy txet eht no sucof dna tnemucod eht fo strap espalloc nac uoY .weiv gnidaeR wen eht ni ,oot ,reisae si gnidaeR
             ngis sulp eht kcilc neht dna ,nmuloc a ro wor a dda ot tnaw uoy erehw kcilc ,elbat a no krow uoy nehW .ti ot txen sraeppa snoitpo tuoyal rof nottub a dna ti kcilc ,tnemucod ruoy ni stif erutcip a yaw eht egnahc oT .meht deen uoy erehw pu wohs taht snottub wen htiw droW ni emit evaS
             emeht wen eht hctam ot egnahc sgnidaeh ruoy ,selyts ylppa uoy nehW .emeht wen ruoy hctam ot egnahc scihparg trAtramS dna ,strahc ,serutcip eht ,emehT wen a esoohc dna ngiseD kcilc uoy nehW .detanidrooc tnemucod ruoy peek pleh osla selyts dna semehT
             seirellag tnereffid eht morf tnaw uoy stnemele eht esoohc neht dna tresnI kcilC .rabedis dna ,redaeh ,egap revoc gnihctam a dda nac uoy ,elpmaxe roF .rehto hcae tnemelpmoc taht sngised xob txet dna ,egap revoc ,retoof ,redaeh sedivorp droW ,decudorp yllanoisseforp kool tnemucod ruoy ekam oT
             tnemucod ruoy stif tseb taht oediv eht rof enilno hcraes ot drowyek a epyt osla nac uoY .dda ot tnaw uoy oediv eht rof edoc debme eht ni etsap nac uoy ,oediV enilnO kcilc uoy nehW .tniop ruoy evorp uoy pleh ot yaw lufrewop a sedivorp oediV
        </p>
    </body>
    </html>

    Example without meta viewport

    <!DOCTYPE html
    <html>
    <head>
    
        <title>Without Meta viewport</title>
    </head>
    <body>
    
        <img src="a.jpg" width="100%" height="auto">
    
        <p>
             ecived rehtona no neve - ffo tfel uoy erehw srebmemer droW ,dne eht hcaer uoy erofeb gnidaer pots ot deen uoy fI .tnaw uoy txet eht no sucof dna tnemucod eht fo strap espalloc nac uoY .weiv gnidaeR wen eht ni ,oot ,reisae si gnidaeR
             ngis sulp eht kcilc neht dna ,nmuloc a ro wor a dda ot tnaw uoy erehw kcilc ,elbat a no krow uoy nehW .ti ot txen sraeppa snoitpo tuoyal rof nottub a dna ti kcilc ,tnemucod ruoy ni stif erutcip a yaw eht egnahc oT .meht deen uoy erehw pu wohs taht snottub wen htiw droW ni emit evaS
             emeht wen eht hctam ot egnahc sgnidaeh ruoy ,selyts ylppa uoy nehW .emeht wen ruoy hctam ot egnahc scihparg trAtramS dna ,strahc ,serutcip eht ,emehT wen a esoohc dna ngiseD kcilc uoy nehW .detanidrooc tnemucod ruoy peek pleh osla selyts dna semehT
             seirellag tnereffid eht morf tnaw uoy stnemele eht esoohc neht dna tresnI kcilC .rabedis dna ,redaeh ,egap revoc gnihctam a dda nac uoy ,elpmaxe roF .rehto hcae tnemelpmoc taht sngised xob txet dna ,egap revoc ,retoof ,redaeh sedivorp droW ,decudorp yllanoisseforp kool tnemucod ruoy ekam oT
             tnemucod ruoy stif tseb taht oediv eht rof enilno hcraes ot drowyek a epyt osla nac uoY .dda ot tnaw uoy oediv eht rof edoc debme eht ni etsap nac uoy ,oediV enilnO kcilc uoy nehW .tniop ruoy evorp uoy pleh ot yaw lufrewop a sedivorp oediV
        </p>
    </body>
    </html>

    To see the difference, inspect both the pages with ctrl + shift + I

    Rules for Content size and viewport

    Generally, users like to scroll up and down to see the content of the page. The web-page interface experience gets poor when the user has to scroll horizontally or zoom in to see the content. There are some conventional rules a front-end developer and web-designer should follow when designing a web-page.

    1. Do not use absolute values.
    2. Do not set a content dependency on the specific viewport width.
    3. Use CSS media queries for better different media devices.

    Do not use absolute values.

    When styling an element width, height, or any other length related area, do not use the absolute value. Because the viewport of the page change according to the browser display size and a fixed length of an element will cause a rendering problem. So, when designing a responsive web-page, always use the relative values.

    Do not set a content dependency on Specific viewport width.

    The viewport of the web-page will change according to the user display. So never set a content which only depends on the specific viewport width. This may lead to a problem where the two different users will not have the same display content.

    Use CSS media queries for different media devices.

    @media Queries are specially designed to set different styling for different mediatype. Always try to set the @meda rule for your web-page. This helps the web-page to look good on every device regardless of the screen area.

    Summary

    • The viewport describes the display area of the web-page.
    • In HTML5 W3C introduce the <meta> viewport tag to control the viewport size of the webpage.