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 page zoom and text scaling

The user can zoom the entire page using CTRL + +/- (Windows, Ubuntu) or Command + +/- (Mac). This affects all page elements equally: layout, images, and text scale together.

A separate text-only zoom (available in some browser settings) only changes font size without affecting layout. However, in most modern browsers (Chrome, Edge, Firefox), the keyboard shortcuts actually trigger page zoom, not text scaling. The distinction matters because different detection methods respond to these differently.

The other type of zoom is the pinch zoom on touch devices. 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 page 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 page zoom level, version 1
const pageZoomLevel = (window.outerWidth / window.innerWidth) * 100;

Important limitation: this method is unreliable because outerWidth includes browser chrome (toolbars, scrollbars, address bar) and changes when the user resizes the window, not just when zooming. It also varies significantly across browsers and operating systems. Use with caution and only as a fallback.

If you want to roughly account for scrollbar size, you can change window.outerWidth to window.outerWidth - 10. The number 10 is an approximate average for scrollbar width.

Method 2: using outerWidth and innerWidth properties with different calculations

Here is a modified version number 2, but it doesn’t work in all browsers, e.g., in Chrome. It works, however, in Safari / macOS.

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

Note: this variant suffers from the same fundamental limitations as Method 1. It is not recommended for production use.

Method 3: using screen.width and window.innerWidth properties

You can use the ratio of screen.width to window.innerWidth to estimate the page zoom level of the browser. Here’s a simple formula:

Determine the page zoom level, version 3
const pageZoomLevel = screen.width / window.innerWidth;

This formula attempts to work because screen.width returns the total width of the screen in physical pixels, while window.innerWidth returns the width of the window’s content area in CSS pixels.

This method is fundamentally flawed and should NOT be used in production.

Here’s why: screen.width reports physical device pixels, while window.innerWidth reports CSS pixels. The ratio changes with display DPI scaling (e.g., Retina displays, Windows display scaling), screen resolution, window size, and browser zoom simultaneously. You cannot isolate zoom from these other factors. A ratio of 2 could mean 200% zoom, or a Retina display at 100% zoom, or a 4K monitor with 150% OS scaling. This method produces unreliable and misleading results.

Method 4: 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.

Determine the page zoom level, version 4
const pageZoomLevel = Math.round(window.devicePixelRatio * 100);

Important limitation:window.devicePixelRatio reflects both browser zoom and system display scaling (DPI). On a standard display at 100% zoom, the value is 1. But on a Retina display (MacBook Pro) at 100% zoom, it starts at 2. On a Windows machine with 150% display scaling, it starts at 1.5. This means you cannot distinguish between a user zooming to 200% on a standard display and a user at 100% zoom on a Retina display – both return 2. Use this method only when you understand this limitation.

Final code

Use the most reliable methods sequentially. Prefer the VisualViewport API (shown later for pinch zoom) as the primary method. Only fall back to devicePixelRatio when necessary. Avoid Methods 1, 2, and 3 in production.

Determine the page zoom level – improved version
function getPageZoomLevel() {
  // Best: VisualViewport API (most accurate for page zoom)
  if (window.visualViewport?.scale !== undefined) {
    return Math.round(window.visualViewport.scale * 100);
  }
  
  // Acceptable fallback: devicePixelRatio (includes DPI scaling)
  if (typeof window.devicePixelRatio === 'number') {
    return Math.round(window.devicePixelRatio * 100);
  }

  // Poor last resort: outer/inner ratio (window resizing affects this)
  if (window.outerWidth && window.innerWidth) {
    return Math.round((window.outerWidth / window.innerWidth) * 100);
  }

  // Default assumption
  return 100;
}

Identifying the pinch zoom level in a browser

Accurately detecting browser zoom levels can be complex due to factors such as sidebar interference and high-DPI displays. This approach leverages the modern VisualViewport API to overcome these challenges. Specifically, it utilizes the scale property of the VisualViewport interface, which provides the pinch-zoom scaling factor applied to the visual viewport, ensuring a reliable zoom level detection.

For browsers that do not support the VisualViewport API, a fallback method using devicePixelRatio is provided. Note that the traditional hidden div measurement trick is no longer reliable because modern browsers apply zoom at the compositor level without affecting DOM element measurements consistently.

Determine the pinch zoom level using VisualViewport API scale property
function getPinchZoomLevel() {
  // Primary method: VisualViewport API (most accurate)
  if (window.visualViewport?.scale !== undefined) {
    return Math.round(window.visualViewport.scale * 100);
  }
  
  // Fallback: devicePixelRatio (less accurate but works)
  const zoomLevel = window.devicePixelRatio || 1;
  
  return Math.round(zoomLevel * 100);
}

Practical example of detecting browser zoom levels

See Detect browser zoom level example on SiteLint lab.

Related posts

Comments

2 Replies to “Detect browser zoom level”

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.