In this article, we’re going to focus on another important concept of responsive design, i.e. the ‘Viewport Meta Tag’.
The viewport meta tag was introduced by Apple to help web developers improve the way web pages are displayed on the iPhone but obviously now it’s used for all mobile devices. This basically tells a mobile device, that the current page is optimized for mobile and it should display it in a certain way and not in the default web layout. In simple words: “A meta viewport tag provides the instructions to the browser on how to manage the page’s dimensions and scaling.” The zooming in and out of the page that you can do is because of this whole viewport business only. Let’s look at how we use the meta tag:
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
In the above example, this meta tag instructs the mobile device to not zoom. This allows the responsive design for mobile devices to be loaded in the scripts.
Virtual Viewport can be split into two:
Visual Viewport: The part of the page which is currently shown on the screen.
Layout Viewport: Visual Viewport + The part of the page which currently lies outside the display area. This is basically the mobile equivalent of desktop resolution. Safari iPhone uses 980px, Opera 850px, Android WebKit 800px, and IE 974px as the default.
In the above examples, we saw only two properties of the viewport meta tag: width and initial-scale; but there are others which are listed in the following table:
|width||The width of the virtual viewport of the device.|
|device-width||The physical width of the device’s screen.|
|height||The height of the “virtual viewport” of the device.|
|device-height||The physical height of the device’s screen.|
|initial-scale||The initial zoom when visiting the page. 1.0 does not zoom.|
|minimum-scale||The minimum amount the visitor can zoom on the page. 1.0 does not zoom.|
|maximum-scale||The maximum amount the visitor can zoom on the page. 1.0 does not zoom.|
|user-scalable||Allows the device to zoom in and out. Values are yes or no.|
Above mentioned properties of the viewport would be added in the ‘Head’ section of our website. Below are some examples which show how we can implement these viewport elements:
- In case your mobile design is set at 320px, we can specify the viewport width:
<meta name=”viewport” content=”width=320″>
- For fluid layouts, we need to match our layout width to the device width:
<meta name=”viewport” content=”width=device-width”>
- To take the advantage of the entire landscape mode and to manage full screen mode of device browser, we add the attribute ‘initial-scale=1’, which command the device browsers to form a 1:1 relationship between “CSS Pixels” and the “Device Independent Pixels (DIPs)” irrespective of device orientation:
<meta name=”viewport” content=”initial-scale=1″>
- Arrange an accessible viewport: In addition to set the initial-scale, we can also control the following attributes on the viewport:
These attributes are essential to restrict the user’s ability to zoom the viewport, potentially creating content render issues.
- Combining Viewport Values: The viewport meta tag can add individual values as well as multiple values, allowing multiple viewport properties to be set at once.
** Whenever we need to add or combine multiple viewport attributes in a single set, we have to use “Comma(,)” after the attribute. E.g.:
<meta name=”viewport”content=”width=device-width, initial-scale=1, maximum-scale=1″>
CSS Viewport Rule:
“This specification provides a way for an author to specify, in CSS, the size, zoom factor, and orientation of the viewport that is used as the base for the initial containing block.”
Basically, this rule allows one to attain the functionality of the viewport meta tag by way of CSS @viewport keyword without cluttering up their HTML. This method helps to keep the style separately for content and provide a more semantic approach. Like viewport meta tag was introduced by Apple, this approach was first proposed by Microsoft in the light of IE10 explorer ignoring viewport meta tag in some situations.
End of this Session
In my last two blogs, I talked about the ‘Media Queries’ for responsive design. These are follows –
- 1. HTML5 Responsive Design- media Queries Tutorial 01
- 2. HTML5 Responsive Design- media Queries Tutorial 02
In this session, I have tried to cover as many aspects of ‘ViewPort’ as to give the reader a good enough understanding of its role in responsive design. However, the field of responsive design in itself is quite broad, so I’ll be coming up with some new related topics in my next articles. Hope you’re finding these sessions enjoyable and informative. Thanks!
For more Information, Visit : HTML5 Responsive Design