James McGrath

How to calculate vw and vh

Sep 25, 2023
1 minute

Recently I found a need to calculate vh and vw

vw = hundredths of the viewport width. vh = hundredths of the viewport height.

This is how to calculate 1vw or 1vh


// 1vh
100 / document.documentElement.clientHeight;

// 1vw
100 / document.documentElement.clientWidth;

// calculate a px value of 30
(100 / document.documentElement.clientHeight) * 30
(100 / document.documentElement.clientWidth) * 30