I want to convert px to vw in JavaScript, how can I do it?
px
vw
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
500px
100vw
1px = (100vw / 500px) = 0.2vw
I used .clientWidth to exclude any scrollbar from computation
.clientWidth
1.4m articles
1.4m replys
5 comments
57.0k users