Basics: What is a Viewport?
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 }}