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 being 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?
Top comments (3)
Another approach: sitelint.com/blog/detect-browser-z...
the article you mentioned also uses the wrong approach, so it has the same problems
This is wrong approach, because any opened sidebar or vertical tabs or reading list reduces the window outerWidth, therefore the zooming result will be completely wrong.