loading...

Determining your browser's zoom percentage

koralarts profile image Karl Castillo ・1 min read

In most browsers specially desktop browsers, you're able to zoom in and out using CTRL/CMD++ or CTRL/CMD+-.

Most of the time, there's no problem as your website should take into account responsiveness but what if you want to update your website based on your user's browser zoom percent?

We can use Javascript to calculate the current browser zoom.

Math.round((window.outerWidth / window.innerWidth) * 100)

Accessibility

Zooming is part of accessibility and having your website usable even when the user is zoomed in is important.

Gotcha

When you have your inspector open and it's docked to the left or right, the calculation becomes inaccurate as the innerWidth doesn't take into account the inspector.

Demo:

Best viewed in debug mode:

https://codepen.io/koralarts/pen/NWxKKdZ


How would you improve the code or do you have other ways on how you determine browser zoom?

Posted on Mar 22 by:

koralarts profile

Karl Castillo

@koralarts

Software Engineer for The Coalition Studio and a Mentor for Lighthouse Labs from Vancouver BC who loves The Golden Girls, Powerlifting and Movies!

Discussion

markdown guide