The Wayback Machine - https://web.archive.org/web/20250121094722/https://www.geeksforgeeks.org/what-is-vh-in-css/
Open In App

What Is VH in CSS?

Last Updated : 16 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

When designing responsive websites, it's important to create layouts that adapt to different screen sizes and orientations. CSS offers several units to help with this, and one of the most powerful for responsive design is the vh unit. In this article, we’ll explore what vh is in CSS, how it works, and when to use it effectively to create flexible, dynamic layouts.

What is vh in CSS?

The vh unit stands for "viewport height." It is a relative length unit in CSS that represents a percentage of the viewport's height. One vh equals 1% of the viewport height, which means that 100 vh equals the full height of the viewport.

Key Characteristics of vh:

  • Viewport-Based: vh is calculated based on the height of the viewport, not the element’s parent or the document size.
  • Dynamic: It adjusts automatically as the viewport size changes, making it ideal for responsive layouts.
  • Useful for Height Control: Commonly used for setting the height of sections, headers, footers, or any element that needs to adjust to the screen size.

Uses of vh in CSS

CSS term vh is used in many cases which provide many benefits. Some of them are as follows:

  • Responsiveness: When the height of the element is specified concerning the browser window, then it helps in terms of responsiveness.
  • Set margins and paddings: The margin can be set concerning the previous element and this ensures the kind of gap that we have initialized within the start remains the same in every case.
  • Better Typography: vh helps in making font sizes suitable for any kind of screen size. For this we would not have to create responsive classes, specifying width & height of each screen & then set the responsive font size for each screen size.

Approach To Set Height in vh

  • Take 3 boxes in HTML and add a heading at the start that we are learning about vh in CSS.
  • Add CSS for the three boxes, give them a color, and write a text in that box.
  • Add the boxes to a container and apply flex property in them.
  • Now, start by specifying a height in each box, one 20vh, the other one 40vh, and the third one 60vh.
  • Finally, run the file through the Live Server.

Example: This HTML document demonstrates the use of the CSS vh (viewport height) unit by displaying three colored boxes (red, green, and blue) with varying heights of 20vh, 40vh, and 60vh, respectively.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>vh in CSS</title>
    <style>
        body {
            margin: 0;
            font-family: Poppins;
            background-color: #f0f0f0;
            display: flex;
            flex-direction: column;
            align-items: center;
        }

        h1 {
            margin: 20px;
        }

        .container {
            display: flex;
            justify-content: center;
            width: 100%;
        }

        .box {
            color: white;
            text-align: center;
            font-size: 24px;
            flex: 1;
        }

        .red {
            background-color: red;
            height: 20vh;
        }

        .green {
            background-color: green;
            height: 40vh;
        }

        .blue {
            background-color: blue;
            height: 60vh;
        }
    </style>
</head>

<body>
    <h1>Learning viewport height (vh) in CSS</h1>
    <div class="container">
        <div class="box red">Red Box</div>
        <div class="box green">Green Box</div>
        <div class="box blue">Blue Box</div>
    </div>
</body>

</html>


Output:

Differences Between vh and Other Units

Understanding when to use vh compared to other units is crucial for effective design:

  • px (Pixels): Fixed unit that does not scale with the viewport.
  • % (Percentages): Relative to the parent element, not the viewport.
  • em and rem: Relative to font sizes, useful for scaling text and layout elements but not directly tied to viewport size.
  • vw (Viewport Width): Similar to vh but based on viewport width, useful for horizontal scaling.

Key Difference: vh scales specifically with the viewport height, making it uniquely suited for responsive height adjustments.


Next Article
Article Tags :

Similar Reads

three90RightbarBannerImg