The@viewport
CSS at-rule lets you configure the viewport through which the document is viewed. It's primarily used for mobile devices but is also used by desktop browsers that support features like "snap to edge" (such as Microsoft Edge). The at-rule contains a set of nested descriptors in a CSS block that is delimited by curly braces.
Lengths specified as percentages are calculated relative to the initial viewport, which is the viewport before any user agent or authored styles have had an opportunity to adjust the viewport. This is typically based on the size of the window on desktop browsers that aren't in full screen mode.
On mobile devices (or desktop devices that are in full screen mode), the initial viewport is usually the portion of a device's screen that is available for application use. This may be either the full screen or the full screen area minus areas controlled by the operating system (such as a taskbar) or the application-available screen area (either the full screen or the screen minus any areas owned by the operating system or other applications).
@viewport { width: device-width; }
A zoom factor of 1.0
or 100%
corresponds to no zooming. Larger values zoom in. Smaller values zoom out.
Browser support for @viewport
is weak at this time, with support being largely available in Internet Explorer and Edge. Even in those browsers, only a small number of descriptors are available. Browsers will ignore @viewport
if they don't support it, and will ignore any descriptors that they don't recognize.
min-width
max-width
width
min-width
and max-width
min-height
max-height
height
min-height
and max-height
zoom
min-zoom
max-zoom
user-zoom
orientation
@viewport { <group-rule-body> }
@viewport { min-width: 640px; max-width: 800px; } @viewport { zoom: 0.75; min-zoom: 0.5; max-zoom: 0.9; } @viewport { orientation: landscape; }
Specification | Status | Comment |
---|---|---|
CSS Device Adaptation The definition of '@viewport' in that specification. | Working Draft | Initial definition |
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 29 (behind a flag) [4] | (Yes) | No support [2] | 10 -ms | 11.10 Removed in 15 Reintroduced behind a flag in 16 | No support [3] |
Feature | Android | Chrome for Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | 4.4 | 29 | (Yes) | No support [2] | 10-ms[1] | 11.10 Removed in 15 Reintroduced behind a flag in 16 | No support [3] |
[1] There is a bug in IE Mobile 10 on older versions of Windows Phone 8, where device-width
, when used within @-ms-viewport
, evaluates to the screen width in physical pixels rather than normalized CSS pixels, which is wrong according to the specification. However, when used within a viewport
<meta>
tag, device-width
evaluates correctly. According to Microsoft, this bug was fixed in Windows Phone 8 Update 3 (a.k.a. GDR3), although there are some reports that the Lumia Black GDR3 update did not fix the bug (at least on the Lumia 920). For more details and a workaround, see Tim Kadlec's blog post "Windows Phone 8 and Device-Width".
[2]: See bug 747754 – Implement @viewport at-rule
[3]: See WebKit bug 95959
[4]: See Chromium issue #235457: Enable @viewport on all platforms
<meta>
, specifically <meta name="viewport">
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/CSS/@viewport