VisualViewport

This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

The VisualViewport interface of the the Visual Viewport API represents the visual viewport for a given window. For a page containing iframes, each iframe, as well as the containing page, will have a unique window object. Each window on a page will have a unique VisualViewport representing the properties associated with that window.

You can get a window's visual viewport using Window.visualViewport.

Note: Only the top-level window has a visual viewport that's distinct from the layout viewport. Therefore, it's generally only the VisualViewport object of the top-level window that's useful. For an <iframe>, visual viewport metrics like VisualViewport.width always correspond to layout viewport metrics like document.documentElement.clientWidth.

Properties

VisualViewport also inherits properties from its parent, EventTarget.

VisualViewport.offsetleft Read only
Returns the offset of the left edge of the visual viewport from the left edge of the layout viewport in CSS pixels.
VisualViewport.offsetTop Read only
Returns the offset of the top edge of the visual viewport from the top edge of the layout viewport in CSS pixels.
VisualViewport.pageLeft Read only
Returns the x coordinate relative to the initial containing block origin of the top edge of the visual viewport in CSS pixels.
VisualViewport.pageTop Read only
Returns the y coordinate relative to the initial containing block origin of the top edge of the visual viewport in CSS pixels.
VisualViewport.width Read only
Returns the width of the visual viewport in CSS pixels.
VisualViewport.height Read only
Returns the height of the visual viewport in CSS pixels.
VisualViewport.scale Read only
Returns the pinch-zoom scaling factor applied to the visual viewport.

Event handlers

VisualViewport.onresize
An event handler attribute for the resize event.
VisualViewport.onscroll
An event handler attribute for the scroll event.

Example

This example, taken from the Visual Viewport README, shows how to use this API to simulate position: device-fixed, which fixes elements to the visual viewport. A live sample is also available.

var bottomBar = document.getElementById('bottombar');
var viewport = window.visualViewport;
function viewportHandler() {
  var layoutViewport = document.getElementById('layoutViewport');

  // Since the bar is position: fixed we need to offset it by the visual
  // viewport's offset from the layout viewport origin.
  var offsetLeft = viewport.offsetLeft;
  var offsetTop = viewport.height
              - layoutViewport.getBoundingClientRect().height
              + viewport.offsetTop;

  // You could also do this by setting style.left and style.top if you
  // use width: 100% instead.
  bottomBar.style.transform = 'translate(' +
                              offsetLeft + 'px,' +
                              offsetTop + 'px) ' +
                              'scale(' + 1/viewport.scale + ')'
}
window.visualViewport.addEventListener('scroll', viewportHandler);
window.visualViewport.addEventListener('resize', viewportHandler);

Specifications

Specification Status Comment
Visual Viewport API
The definition of 'VisualViewport' in that specification.
Draft Initial definition.

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung Internet
Basic support
Experimental
Chrome Full support 61Edge No support NoFirefox Full support 63
Disabled
Full support 63
Disabled
Disabled From version 63: this feature is behind the dom.visualviewport.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 48Safari No support NoWebView Android Full support 61Chrome Android Full support 61Edge Mobile No support NoFirefox Android Full support 63
Disabled
Full support 63
Disabled
Disabled From version 63: this feature is behind the dom.visualviewport.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Full support 48Safari iOS No support NoSamsung Internet Android ?
height
Experimental
Chrome Full support 61Edge No support NoFirefox Full support 63
Disabled
Full support 63
Disabled
Disabled From version 63: this feature is behind the dom.visualviewport.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 48Safari No support NoWebView Android Full support 61Chrome Android Full support 61Edge Mobile No support NoFirefox Android Full support 63
Disabled
Full support 63
Disabled
Disabled From version 63: this feature is behind the dom.visualviewport.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Full support 48Safari iOS No support NoSamsung Internet Android ?
offsetLeft
Experimental
Chrome Full support 61Edge No support NoFirefox Full support 63
Disabled
Full support 63
Disabled
Disabled From version 63: this feature is behind the dom.visualviewport.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 48Safari No support NoWebView Android Full support 61Chrome Android Full support 61Edge Mobile No support NoFirefox Android Full support 63
Disabled
Full support 63
Disabled
Disabled From version 63: this feature is behind the dom.visualviewport.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Full support 48Safari iOS No support NoSamsung Internet Android ?
offsetTop
Experimental
Chrome Full support 61Edge No support NoFirefox Full support 63
Disabled
Full support 63
Disabled
Disabled From version 63: this feature is behind the dom.visualviewport.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 48Safari No support NoWebView Android Full support 61Chrome Android Full support 61Edge Mobile No support NoFirefox Android Full support 63
Disabled
Full support 63
Disabled
Disabled From version 63: this feature is behind the dom.visualviewport.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Full support 48Safari iOS No support NoSamsung Internet Android ?
onresize
Experimental
Chrome Full support 62
Full support 62
Partial support 61
Edge No support NoFirefox No support NoIE No support NoOpera Full support 49
Full support 49
Partial support 48
Safari No support NoWebView Android Full support 62
Full support 62
Partial support 61
Chrome Android Full support 62
Full support 62
Partial support 61
Edge Mobile No support NoFirefox Android No support NoOpera Android Full support 49
Full support 49
Partial support 48
Safari iOS No support NoSamsung Internet Android ?
onscroll
Experimental
Chrome Full support 62
Full support 62
Partial support 61
Edge No support NoFirefox No support NoIE No support NoOpera Full support 49
Full support 49
Partial support 48
Safari No support NoWebView Android Full support 62
Full support 62
Partial support 61
Chrome Android Full support 62
Full support 62
Partial support 61
Edge Mobile No support NoFirefox Android No support NoOpera Android Full support 49
Full support 49
Partial support 48
Safari iOS No support NoSamsung Internet Android ?
pageLeft
Experimental
Chrome Full support 61Edge No support NoFirefox Full support 63
Disabled
Full support 63
Disabled
Disabled From version 63: this feature is behind the dom.visualviewport.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 48Safari No support NoWebView Android Full support 61Chrome Android Full support 61Edge Mobile No support NoFirefox Android Full support 63
Disabled
Full support 63
Disabled
Disabled From version 63: this feature is behind the dom.visualviewport.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Full support 48Safari iOS No support NoSamsung Internet Android ?
pageTop
Experimental
Chrome Full support 61Edge No support NoFirefox Full support 63
Disabled
Full support 63
Disabled
Disabled From version 63: this feature is behind the dom.visualviewport.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 48Safari No support NoWebView Android Full support 61Chrome Android Full support 61Edge Mobile No support NoFirefox Android Full support 63
Disabled
Full support 63
Disabled
Disabled From version 63: this feature is behind the dom.visualviewport.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Full support 48Safari iOS No support NoSamsung Internet Android ?
scale
Experimental
Chrome Full support 61Edge No support NoFirefox Full support 63
Disabled
Full support 63
Disabled
Disabled From version 63: this feature is behind the dom.visualviewport.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 48Safari No support NoWebView Android Full support 61Chrome Android Full support 61Edge Mobile No support NoFirefox Android Full support 63
Disabled
Full support 63
Disabled
Disabled From version 63: this feature is behind the dom.visualviewport.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Full support 48Safari iOS No support NoSamsung Internet Android ?
width
Experimental
Chrome Full support 61Edge No support NoFirefox Full support 63
Disabled
Full support 63
Disabled
Disabled From version 63: this feature is behind the dom.visualviewport.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 48Safari No support NoWebView Android Full support 61Chrome Android Full support 61Edge Mobile No support NoFirefox Android Full support 63
Disabled
Full support 63
Disabled
Disabled From version 63: this feature is behind the dom.visualviewport.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Full support 48Safari iOS No support NoSamsung Internet Android ?

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
User must explicitly enable this feature.
User must explicitly enable this feature.

See also

  • Web Viewports Explainer — useful explanation of web viewports concepts, including the difference between visual viewport and layout viewport.

Document Tags and Contributors

Contributors to this page: chrisdavidmills, connorshea, jpmedley, bokand, deltab.
Last updated by: chrisdavidmills,