I want to convert px
to vw
in JavaScript, how can I do it?
I've been looking for it, but I haven't found anything.
So,
1px → ? vw
Thanks.
1px = (100vw / [document.documentElement.clientWidth] px)
e.g. — If your viewport was 500px
wide (equals by definition to 100vw
) then
1px = (100vw / 500px) = 0.2vw
I used .clientWidth
to exclude any scrollbar from computation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With