[EN] Viewport units in CSS: Beyond VH
Tags: css, frontend, mobile, ux
Published: 2023-10-23 20:53:28

Well, this text comes from someone who's just discovered something and wants to tell the whole world about it. Up until a few hours ago, I didn't know there were other viewport units besides vh/vw. how did I notice that?? By facing responsiveness issues and searching for solutions. I had implemented a modal that needed to take up 100% of the page height. So '100vh' worked pretty well... on desktop. The real problem emerged when I tested it on a mobile device. Below, I explain the problem and how I solved it.

In this article, I'll try to discuss the 'new' CSS viewport units: svh, lvh, and dvh. But first, let's understand a few basics before we delve deeper.
</br>
</br>


## Summary of content
<ul>
    <li>
        <a href="#viewport">What is a Viewport?</a>
    </li>
    <li>
        <a href="#problems">VH Problems</a>
    </li>
    <li>
        <a href="#svh">SVH — Small Viewport Height</a>
    </li>
    <li>
        <a href="#lvh">LVH — Large Viewport Units</a>
    </li>
    <li>
        <a href="#dvh">DVH — Dynamic Viewport Units</a>
    </li>
    <li>
        <a href="#compatibility">Browser Compatibility</a>
    </li>
    <li>
        <a href="#enhancement">Progressive Enhancement</a>
    </li>
</ul>

<b><b>


## What is a Viewport? <a id="viewport" href="#viewport">&#128279;</a>
In a nutshell, the viewport is the visible area of a web page on a device. The size of the viewport varies according to the device, the browser, and can even be changed by the user if they resize the browser window.

![Browsers UI examples](https://res.cloudinary.com/practicaldev/image/fetch/s--9gkGPnW0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yt4hx7gfr7d8h3o3q4ws.png)

## VH — Viewport Height 
"VH" was introduced in CSS module 3 and is a relative CSS unit. It allows webpage elements to be sized based on the screen size, creating a responsive layout. Thus, 'vh' represents a percentage of the viewport height.

_1vh=1% of the viewport height._

```
.foo {
height: 50vh; /* The element will be 50% of the viewport height */
}
```

## VH Problems <a id="problems" href="#problems">&#128279;</a>
While 'vh' brought many improvements, especially for desktops, mobile devices posed some challenges. Some browsers have a navigation bar that occupies screen space. This bar usually overlays the webpage content and can dynamically expand or retract based on the direction the user scrolls. Therefore, 'vh' can behave unexpectedly on some devices.




![Browsers UI examples](https://res.cloudinary.com/practicaldev/image/fetch/s--YqtSunyQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i6c23u8uh2wjqu9gyboj.png)

![Browsers UI examples](https://res.cloudinary.com/practicaldev/image/fetch/s--LaEb2qST--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lwwnky4on30d24oygocs.png)

Because of these problems, the community discussed and came up with some solutions:

## SVH — Small Viewport Height <a id="svh" href="#svh">&#128279;</a>
'svh' is defined considering the viewport size when the navigation bar (or other browser items) is expanded. So when the browser occupies as much space as possible, the page content will be the smallest.

In the example below, '100svh' will occupy the screen space considering the expanded navigation bar, without content "hanging" beneath the bar.

![Browsers UI examples](https://res.cloudinary.com/practicaldev/image/fetch/s--qpE7SgDt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kbglt1p9lvjrsw7pg8wo.png)



```
.container {
height: 100svh; /* The element will be the viewport height considering the expanded UI */
}
```

**When to use:**
When you want to ensure content won't be obscured by the browser's UI.

**When to avoid:**
If the browser's UI is retracted, as there might be extra white space.

## LVH — Large Viewport Units <a id="lvh" href="#lvh">&#128279;</a>
Conversely, 'lvh' is the opposite of 'svh'. It's defined considering the viewport size when the navigation bar or other browser items are retracted. Essentially, 'lvh' behaves similarly to 'vh'.

```
.container {
height: 100lvh; /* The element will be the viewport height considering the retracted UI */
}
```
**When to use:**
To maximize content space when the browser's UI is retracted.

**When to avoid:**
If the browser's UI is expanded, as content might be obscured.

## DVH — Dynamic Viewport Units  <a id="dvh" href="#dvh">&#128279;</a>
As the name suggests, Dynamic Viewport is dynamic. It automatically adjusts the content size based on the browser's UI, whether expanded or retracted. Although this might seem perfect, one must be cautious: using 'dvh' could cause content to resize when the user scrolls, which could be distracting and degrade user experience, not to mention potential performance costs.
```
.container {
height: 100dvh; /* The element's height will dynamically adjust with the browser's UI */
}
```
**When to use:**
When you want dynamic height adjustments as the browser's UI changes.

**When to avoid:**
If dynamic resizing could distract the user or negatively impact user experience.

<b>
## Browser compatibility <a id="compatibility" href="#compatibility">&#128279;</a>

| <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/au2ph8juz4l6gu8m6v99.png" width="18" height="18" alt="chrome" /> | <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oc95bmrx1ic1yv2ufxnp.png" width="18" height="18" alt="edge" /> | <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wnv0yb558uyvttyxnsz7.png" width="18" height="18" alt="firefox"/> | <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ae4m3sedx2hcpf5nkr0a.png" width="18" height="18" alt="safari"/> |
| --- | --- | --- | --- |
| 108 | 108 | 101 | 15.4 |
<b>
<b>


## Progressive Enhancement <a id="enhancement" href="#enhancement">&#128279;</a>

`@supports` is a conditional CSS rule that allows you to check whether or not a browser supports a certain property or value. It's very useful if you want to apply advanced or experimental styles and ensure that if the browser doesn't support it, it offers a valid alternative to the browser without harming the user experience.
This is "progressive enhancement".
<b>
Here's a basic example:
```
/* Standard styles for all browsers */
div {
    height: 50vh; /* using the traditional vh unit */
}

/* Specific styles for browsers that support svh */
@supports (height: 50svh) {
    div {
        height: 50svh; /* using the new svh unit */
    }
}
```
<b>
This ensures that if the browser doesn't support SVH, it will use VH.
<b>

Thanks <a href=“https://dev.to/lixeletto” target='_blank'>Camilo </a>, for the wonderful suggestions for this article, you're amazing!