I was always under the impression that CSS transforms are purely visual and don't influence the actual site layout in any way. I recently found a weird bug in my project, where an image which is transformed out of the viewport (to later fly in) changed the width of the page.
My coworkers and I are extremely confused by this. I provided an example below (also hosted here: https://transform-confusion.netlify.app/) that shows the phenomenon with a transform: scale(2) applied to a div completely changing the width of the browser.
Do I just misunderstand how CSS works, is this a weird browser bug or what is going on here?
Source code used here and in the video:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<style>
body {
border: 2px dotted black;
}
.scale {
transform: scale(2);
transform-origin: top left;
}
.scalable{
background: lightblue;
padding: 5px;
}
</style>
</head>
<body>
<h1>This is a test</h1>
<button>Toggle transform</button>
<div class="scalable">
Why does the Site start scrolling horizonally from a simple transform?
<pre>transform: scale(2)</pre>
</div>
<script>
const scalable = document.querySelector(".scalable");
document.querySelector("button").addEventListener("click", () => {
scalable.classList.toggle("scale");
})
</script>
</body>
</html>
