Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transform: scale using Viewport Units

I want to use a transform: scale where my width changes to 80vw and my height is auto. So this is what i want = transform: scale(80vw, auto);. Unfortunately, this doesnt work. Is there another way to get this effect. Preferably without using javascript.

like image 626
Cornelis Keijnemans Avatar asked Mar 17 '26 14:03

Cornelis Keijnemans


1 Answers

var range = 1 / 400;
var vw = range * Math.min(window.innerWidth, window.innerHeight);

document.documentElement.style.setProperty('--vw-scale', `${vw}`);

window.addEventListener('resize', () => {
  document.documentElement.style.setProperty('--vw-scale', `${range * Math.min(window.innerWidth, window.innerHeight)}`);
});
.scale-element {
  width: 400px;
  transform: scale(var(--vw-scale));
  transform-origin: left top;
}

h1 {
  font-size: 25px;
}
<div class="scale-element">
  <h1>Test title</h1>
  <p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Sed, et. Deserunt officiis provident quod perferendis dolore dolores enim sint veniam ea odio ratione, repudiandae distinctio, aliquid possimus commodi cupiditate voluptas!
  </p>
</div>

Even though the calc function is not used directly, it works close to it. for test and resize screen you should view it in full page option.

like image 92
Fatih Ertuğral Avatar answered Mar 20 '26 05:03

Fatih Ertuğral



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!