
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.

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.
const pageZoomLevel = (window.outerWidth / window.innerWidth) * 100;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.
const pageZoomLevel = Math.round(((window.outerWidth) / window.innerWidth) * 100) / 100;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:
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.
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.
const pageZoomLevel = Math.round(window.devicePixelRatio * 100);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.
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.
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.
It's not working solution at all, because the browser window can have different sidebars opened, reading list of vertical tabs or whatever and these sidebars reduce innerWidth of the window. Please thoroughly test your solution before posting it. devicePixelRatio can only be used for standard monitors with 96 dpi resolution, but anything bigger results in wrong zoom detection. For example devicePixelRatio is 2 on Retina displays by default without any zooming.
These points are valid concerns, as the solution may not account for all possible browser configurations and screen resolutions. True, that sidebars reduce the innerWidth. Maybe a potential solution could use a combination of innerWidth and other properties, such as document.documentElement.clientWidth, to get a more accurate measurement of the browser's content area. We were testing the solution and also provided an example of a testing page. However, that doesn't mean it is free of issues. You're more than welcome to provide your suggestions or ideas on how to improve the solution.