I just learned about a new and uncommon CSS unit. vh
and vw
measure the percentage of height and width of the viewport respectively.
I looked at this question from Stack Overflow, but it made the units look even more similar.
The answer specifically says
vw and vh are a percentage of the window width and height, respectively: 100vw is 100% of the width, 80vw is 80%, etc.
This seems like the exact same as the %
unit, which is more common.
In Developer Tools, I tried changing the values from vw/vh to % and viceversa and got the same result.
Is there a difference between the two? If not, why were these new units introduced to CSS3
?
100%
can be100%
of the height of anything. For example, if I have a parentdiv
that's1000px
tall, and a childdiv
that is at100%
height, then that childdiv
could theoretically be much taller than the height of the viewport, or much smaller than the height of the viewport, even though thatdiv
is set at100%
height.If I instead make that child
div
set at100vh
, then it'll only fill up100%
of the height of the viewport, and not necessarily the parentdiv
.