In CSS, what does 'vh' unit stand for?

Understanding the 'vh' Unit in CSS

In Cascading Style Sheets (CSS), the unit 'vh' stands for 'Viewport Height'. This unit is used to measure the size of elements and spacing on a web page relative to the height of the viewpoint of the browser, and is immensely useful in creating responsive web designs that adapt to different browser or device sizes.

The 'vh' unit is equal to 1% of the height of the viewport. So, if your viewport has a height of 900 pixels, then 1vh will be equal to 9 pixels. This can be leveraged to set the height of an element to be a certain percentage of the viewport height. For instance, setting a div element's height to 50vh will make its height half of the viewport height.

div {
  height: 50vh;
}

This code means that no matter the size of the browser window, the div's height will always be 50% of the viewport. Such applications of 'vh' lend flexibility and responsiveness, proving handy especially in an era where web content is consumed through diverse screen sizes.

It's also worth noting that 'vh' is different from 'vw' in CSS. While 'vh' is relative to the height of the viewport, 'vw' (or 'Viewport Width') is relative to the width of the viewport.

When using 'vh', it’s important to consider its impact on page layout. Since its value is relative to the viewport size, changes in the viewport (like resizing the browser window) may cause layout shifts. Therefore, best practices suggest using 'vh' thoughtfully and possibly in conjunction with other fixed or relative units to create a harmonious, responsive design.

Do you find this helpful?