Languages
[Edit]
EN

JavaScript - change web browser zoom level on click event

0 points
Created by:
Laylah-Walsh
654

In this article, we would like to show you how to change web browser zoom level on click event using JavaScript.

Quick solution:

document.body.style.zoom = '50%';

Note:

CSS zoom property is non-standard, transform: scale() should be used instead of this property, if possible.

 

Practical example

In this example, we create a function that changes CSS zoom property on click event.

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <script>

    function setZoom() {
        document.body.style.zoom = '50%';
    };

  </script>
</head>
<body>
  <input type="button" value="Click me!" onclick="setZoom()" />
</body>
</html>

Alternative solution

In this solution, as an alternative trick to change zoom, we use transform property to scale document body.

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <script>

    function setZoom() {
        document.body.style.transform = 'scale(0.5)';
        document.body.style.transformOrigin = 'left center';
    };

  </script>
</head>
<body>
  <input type="button" value="Click me!" onclick="setZoom()" />
</body>
</html>

Note:

This approach requires additional container sizing controls that need to be adjusted.

 

References

  1. zoom - CSS: Cascading Style Sheets | MDN
  2. scale() - CSS: Cascading Style Sheets | MDN
  3. transform-origin - CSS: Cascading Style Sheets | MDN

Alternative titles

  1. JavaScript - changing web browser zoom level on click event
  2. JavaScript - dynamically change web browser zoom level on click event
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join