What does vh mean in css
Content on WhatAnswers is provided "as is" for informational purposes. While we strive for accuracy, we make no guarantees. Content is AI-assisted and should not be used as professional advice.
Last updated: April 4, 2026
Key Facts
- vh is a relative CSS unit measuring viewport height.
- 1vh equals 1% of the viewport's height.
- 100vh represents the full height of the viewport.
- vh units are useful for creating responsive layouts.
- vh can be used with various CSS properties like height, margin, and padding.
What does vh mean in CSS?
In the world of Cascading Style Sheets (CSS), units are crucial for defining the size and spacing of elements on a web page. Among these units, `vh` is a particularly useful one for creating responsive designs that adapt to different screen sizes. Understanding `vh` is key to mastering modern web layout techniques.
Understanding Viewport Units
Before diving into `vh`, it's important to understand the concept of the 'viewport'. The viewport is the user's visible area of a web page. On a desktop browser, it's the window you see the website in. On a mobile device, it's the screen itself. The dimensions of the viewport can change as the user resizes their browser window or rotates their device.
CSS provides several units that are relative to the viewport's dimensions. These are known as viewport units. The primary viewport units are:
- `vw` (viewport width): 1vw is equal to 1% of the viewport's width.
- `vh` (viewport height): 1vh is equal to 1% of the viewport's height.
- `vmin` (viewport minimum): 1vmin is equal to the smaller of `vw` or `vh`.
- `vmax` (viewport maximum): 1vmax is equal to the larger of `vw` or `vh`.
What is vh in CSS?
As mentioned, `vh` stands for 'viewport height'. It is a relative unit that allows you to set the size of an element based on the height of the browser window. For example, if you set an element's height to `50vh`, its height will be exactly half of the viewport's current height, regardless of the user's screen resolution or zoom level.
How to Use vh Units
The `vh` unit can be applied to almost any CSS property that accepts length values, such as `height`, `margin`, `padding`, `font-size`, `top`, `bottom`, `transform`, and more.
Example 1: Full-Height Sections
One of the most common uses of `vh` is to make elements take up the full height of the viewport. This is perfect for creating hero sections or distinct content blocks that span the entire screen height.
.hero-section {height: 100vh;background-image: url('hero-image.jpg');background-size: cover;display: flex;justify-content: center;align-items: center;color: white;text-align: center;}In this example, the `.hero-section` will always occupy the full height of the user's browser window. This ensures a consistent and immersive experience, especially on the initial load of a webpage.
Example 2: Responsive Typography
You can also use `vh` to make text scales with the viewport height. While not always recommended for primary body text due to potential readability issues on very small screens, it can be effective for headings or call-to-action elements.
h1 {font-size: 8vh;}This would make the `h1` element's font size dynamically adjust based on the viewport's height. A larger viewport height would result in a larger font size, and vice versa.
Example 3: Spacing and Positioning
`vh` units can also control spacing and positioning relative to the viewport height. For instance, you might want to ensure an element is always a certain distance from the top or bottom of the screen.
.footer {position: absolute;bottom: 5vh;width: 100%;text-align: center;}Here, the footer is positioned 5% of the viewport height up from the bottom edge.
Considerations and Potential Issues
While `vh` units are powerful, they come with a few considerations:
- Mobile Browser UI Elements: On mobile devices, browser navigation bars (address bar, toolbars) can appear and disappear as the user scrolls. This dynamic behavior can cause elements set to `100vh` to temporarily exceed the visible screen space when these bars are present, or not fill the screen when they are hidden. This can lead to unintended scrollbars or elements being cut off. Developers often use JavaScript or specific CSS techniques (like `dvh` - dynamic viewport height) to mitigate this.
- Readability: Using `vh` for font sizes can sometimes lead to text becoming too large on tall screens or too small on short screens, impacting readability. It's often better to use `rem` or `em` units for body text and reserve `vh` for larger display text or layout dimensions.
- Content Overflow: If an element's height is set to a large `vh` value (e.g., `100vh`) and its content exceeds that height, the content might overflow, leading to scrollbars within the element or content being hidden.
vh vs. vw
It's important to distinguish `vh` from `vw`. While `vh` is based on the viewport's height, `vw` is based on the viewport's width. Choosing between them depends on whether you want your element's size to be relative to the screen's height or width. For full-screen sections, `100vh` is typically used. For elements that should span the full width, `100vw` would be appropriate.
Conclusion
In summary, `vh` is a valuable CSS unit that represents a percentage of the viewport's height. It's instrumental in creating fluid and responsive web designs, allowing elements to scale proportionally with the browser window. By understanding its behavior and potential pitfalls, developers can effectively leverage `vh` to build modern, visually appealing, and adaptable web experiences.
More What Does in Daily Life
Also in Daily Life
More "What Does" Questions
Trending on WhatAnswers
Browse by Topic
Browse by Question Type
Sources
Missing an answer?
Suggest a question and we'll generate an answer for it.