📜  vh vs vw css (1)

📅  最后修改于: 2023-12-03 14:48:18.967000             🧑  作者: Mango

VH VS VW in CSS

VH and VW are units of measurement in CSS that allow designers and developers to create responsive layouts without complicated calculations.

VH stands for viewport height, and VW stands for viewport width. They represent a percentage of the browser window's height or width.

Usage

To use VH or VW in CSS, simply add the unit to your values. For example:

div {
  height: 50vh;
  width: 25vw;
}

This will set the height of the div to 50% of the viewport height, and the width to 25% of the viewport width.

Differences

The main difference between VH and VW is how they scale. When the browser window is resized, VH and VW will scale proportionally to the height or width of the viewport.

For example, if div is set to 50vh when the viewport height is 1000px, the height of the div will be 500px. If the viewport height is then resized to 500px, the height of the div will be 250px. The same thing happens with VW.

Best Uses

VH and VW are best used for creating layouts that need to be responsive and fill the height or width of the viewport. Here are a few examples:

  • Hero images that fill the width of the viewport
  • Full-screen sections that fill the height of the viewport
  • Navigation menus that need to be responsive and adjust to the viewport size
Conclusion

In summary, VH and VW are extremely useful units of measurement in CSS that make it easy to create responsive layouts. They scale proportionally to the height or width of the viewport, making them perfect for creating full-screen elements or responsive menus.