On this page
Laptop with the message "Detect Browser Zoom Level" and magnifier next to it.

Detect browser zoom level

Learn how to check the browser zoom level in JavaScript.

You can use multiple methods in JavaScript to detect the browser zoom level, depending on the browser and the specific requirements of your application. Here are a few approaches to think about.

Differentiating pinch zooming and text scaling

The user can change the font size of the browser either permanently via the browser’s settings or on-the-fly via the keyboard keys CTRL+/- (Windows, Ubuntu) or Command+/- (Mac) and that refers to the text scaling.

The other type of zoom is the pinch zoom. Pinch zooming zooms the entire page – layout, formatting, and text size – at the same time. Elements keep their size and shape, reducing the need for us to compensate for text scaling. In effect, the browser bears the burden of relative size.

Pinch zoom gesture
Touchpad and a hand that shows the pinch gesture

Determining the text zoom level in a browser

Method 1: using outerWidth and innerWidth properties

This method uses the read-only Window properties window.outerWidth and window.innerWidth.

Determine the text zoom level, version 1
const textZoomLevel = (window.outerWidth / window.innerWidth) * 100;

If you want to count scrollbar size, then change window.outerWidth to window.outerWidth - 10. The number 10 is an average number for the scrollbar size.

Method 2: using outerWidth and innerWidth properties with different calculations

Method version 1 works inconsistently across different browsers. Here is a modified version number 2.

Determine the text zoom level, version 2
const textZoomLevel = Math.round(((window.outerWidth) / window.innerWidth) * 100) / 100;

Method 3: using window.devicePixelRatio property

Another version uses window.devicePixelRatio property which provides the ratio of the resolution in physical pixels to the resolution in CSS pixels for the current device, which can be used to determine the text zoom level.

Determine the text zoom level, version 3
const textZoomLevel = Math.round(window.devicePixelRatio * 100);

Identifying the pinch zoom level in a browser

This method uses modern VisualViewport API. The scale read-only property of the VisualViewport interface returns the pinch-zoom scaling factor applied to the visual viewport.

Determine the pinch zoom level using VisualViewport API scale property
const pinchZoomLevel = window.visualViewport.scale;

Practical example of detecting browser zoom levels

See Detect browser zoom level example on SiteLint lab.

Related posts

Comments

Leave a Reply

Search in sitelint.com

Elevate your website’s accessibility with SiteLint

Your reliable source for inclusive online experiences. Our cutting-edge tools and services enable businesses to create user-friendly web environments. Join the digital inclusivity movement to discover new ways to engage and grow. Discover the SiteLint platform today and transform your online presence into a beacon of accessibility.

Real-user monitoring for Accessibility, Performance, Security, SEO & Errors (SiteLint)