.vw and .vh property of CSS

.vw and .vh property of CSS
Photo by Lee Campbell / Unsplash

CSS has several different units for expressing a length.

Many CSS properties take "length" values, such as widthmarginpaddingfont-size, etc.

Length is a number followed by a length unit, such as 10px2em, etc.

vwRelative to 1% of the width of the viewport*
vhRelative to 1% of the height of the viewport*

Example of .vw is as follows:

<!DOCTYPE html>
<html>
<head>
<style>
h1 {
  font-size: 20vw;
}
</style>
</head>
<body>

<h1>Hello</h1>

<p>Resize the width of the browser window to see how the font-size of h1 changes.</p>
<p>1vw = 1% of viewport width.</p>

</body>
</html>

The output is as follows:

The example of .vh is as follows:

<!DOCTYPE html>
<html>
<head>
<style>
h1 {
  font-size: 20vh;
}
</style>
</head>
<body>

<h1>Hello</h1>

<p>Resize the height of the browser window to see how the font-size of h1 changes.</p>
<p>1vh = 1% of viewport height.</p>

</body>
</html>

The output is as follows:

Read more