.vw and .vh property of CSS
CSS has several different units for expressing a length.
Many CSS properties take "length" values, such as width
, margin
, padding
, font-size
, etc.
Length is a number followed by a length unit, such as 10px
, 2em
, etc.
vw | Relative to 1% of the width of the viewport* |
vh | Relative 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: