OneBite.Dev - Coding blog in a bite size

How to make zoom effect in HTML with CSS

Learn how to make zoom effect in HTML with CSS. We will play with scale transformation on CSS to make the element chosen bigger/smaller

Let’s learn how to make zoom effect in HTML with CSS. We will play with scale transformation on CSS to make the element chosen bigger/smaller

The properties we need to set are:

 transform: scale(1);
 transformOrigin: 0% 0% 0px;

Play with button zoom in and out

Now we just need to add a javascript function for this.

<button onclick="zoomIn"> zoom in </button>
<button onclick="zoomOut"> zoom out </button>

<div id="content">
 Content
</div>

Now for the script

<script>
let scaleLevel = 1; // default
function zoomIn() {
    scaleLevel = scaleLevel + 0.2 //increase level
    document.getElementById('pdf-viewer').style.transform = `scale(${scaleLevel})`
}

function zoomOut() {
    scaleLevel = scaleLevel - 0.2 //increase level
    document.getElementById('pdf-viewer').style.transform = `scale(${scaleLevel})`
}
</script>
HTML CSS javascript