0

I want to convert px to the viewport units (vh and vw). So I used these with no success?

var newWidth = yourElement.outerWidth / document.documentElement.clientWidth *100;
var newHeight = yourElement.outerHeight / document.documentElement.clientHeight *100;

I just get NaN.

I put my calculation in a timeout to be sure I'm selecting the element only when it's rendered.

How can I convert px to viewport units correctly?

1 Answers1

0

yourElement.offsetWidth should work for you Check out this answer to identify the sizes properly: How do I retrieve an HTML element's actual width and height?

V.Volkov
  • 731
  • 3
  • 12