Basics: What is a Viewport?
The HTML5 umbrella covers a vast range of specifications, APIs, techniques, and design approaches to web development. Several of these technologies are intended to help developers build web applications optimized for mobile devices. Not all platforms support the same features in the same way, however. Moreover, specific hardware differences often require special treatment beyond the W3C spec, no matter how the platform implements the spec.
This Refcard is intended to bring you up to speed, and help you jump head-first, into mobile HTML5 development. The card first covers the most important HTML5 mobile technologies, including key variations by platform and device, then offers a cream-of-the-crop selection from the vast ecosystem of tools, frameworks, and communities that have sprung up to support mobile HTML5 development. The card assumes basic knowledge of core web development technologies (JavaScript, HTML, CSS).
A viewport allows us to normalize different devices to get the best sizes for a mobile website or webapp and to avoid initial zooming.
All platforms support viewport definitions through <meta> tags:
<meta name="viewport" content="{comma-separated options}">
The most useful version is
<meta name="viewport" content="width=device-width"
Viewport Options
Option | Values |
---|---|
width | Width of the virtual viewport that the browser will expose to our website in CSS pixels or the constant device-width |
height | Height of the virtual viewport that the browser will expose to our website in CSS pixels or the constant device-width |
user-scalable | no/yes |
initial-scale | Float value (1=no zoom) |
minimum-scale | Float value |
maximum-scale | Float value |
target-densitydpi | Integer value (70 to 400) in DPI or one of the following constants: device-dpi, high-dpi, medium-dpi, low-dpi. Not available on Safari for iOS |
Viewport Through CSS
Internet Explorer since v10 also supports @viewport on CSS:
@-ms-viewport {
width: device-width;
}
On Windows 8, including tablets, IE can work in snap state. We can define the viewport only when in this mode:
@media screen and (max-width: 400px) {
@-ms-viewport { width: 320px; }
}
Device-Width Values
When using width=device-width as the viewport's width, the final width that we'll have available may be (measured in CSS pixels):
Option | Values |
---|---|
320 | The most common value on smartphones including iPhone, Windows Phone, Android (medium screen sizes < 4") |
360 | Large screen Android smartphones (< 5"), such as Galaxy SIII & SIV |
400 | Phablets (>5"), such as Galaxy Note |
600 | Small tablets |
768 | Large tables |
Even on high-resolution screens, such as Retina displays, you will always get a width in CSS pixels with a value of 320. Therefore, the available width for the canvas is the same for all devices.
Landscape viewport
Safari for iPhone will not use the available space on the viewport on landscape and it will zoom in the content. To avoid this behavior we can use the code on https://gist.github.com/901295
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}