New Viewport Unit

This article is a translation of
this article. The translator is the front-end development engineer of 360 Qiwu Troupe.
Original Title: New Viewport Units
Original Author: Ahmad Shadeed
Original Address: https://ishadeed.com/article/new-viewport-units/

We've been using CSS viewport units since 2012. They are useful to help us resize elements based on viewport width or height.

However, using vh units on mobile devices is problematic. The reason is that the viewport size does not include the browser's address bar UI.

To fix this, we now have new viewport units. Let's learn about them in this article.

CSS Viewport Units

For example, when we need to resize an element according to the viewport size. The viewport units are vw, vh, vmin, and vmax.

Consider the following diagram:

5fa34db81831a609e5568e2120df59af.png

A value of 50vw means: Give the element a width equal to 50% of the viewport width.

question

When resizing the element with 100vh to occupy the full height of the mobile viewport, it will be larger than the space between the top and bottom bars. This happens in browsers that shrink the UI when scrolling, such as Safari or Chrome on Android.

The image below shows how the top and bottom UI differ for each mobile browser.

edb5c4a36bbabb53e0bef73a6874a9d0.png b2d4eb021c1a752307b2dfccb7e8e342.png

Let's say we have a loading view that fills the entire screen.

/* 我知道我们可以使用bottom: 0代替height: 100vh,但这里是故意突出这个问题。 */
.loading-wrapper {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  height: 100vh;
  display: grid;
  place-items: center;
}

Consider the following diagram:

7f4b8763d6e65c5fdba505c10e89f599.png

The loading icon is centered in the CSS, but visually it appears to be slightly at the bottom. Why is this happening?

00f8c0c6f1a314d0c47f2e0bbdcc488e.png

For browsers, height: 100vh means the element will fill the viewport height, but it will not calculate the value dynamically. This means the bottom address bar and toolbar won't be counted.

So we expect 100vh to be equal from the top of the viewport to the top of the address bar UI.

6528e49c628fdd45c2e0b6e4f7b83284.png

As we scroll down, the address bar UI shrinks its size. This is great because it gives users more vertical space to browse the page. At the same time, it also kind of breaks the UI.

In the image below, when the address bar is visible, the center of the vertical space is not as expected. When scrolling, it looks fine.

a0b50cb47929a79ee8304f1a4f029557.png

Notice how I've highlighted the invisible areas. It becomes visible when scrolling down. How to handle this in CSS?

Small, Large, and Dynamic Viewport Units

To address this, the CSS Working Group agreed to a new set of units: svh, lvh, and dvh. They represent small viewport, large viewport, and dynamic viewport respectively.

bd20d9be224189d03dc83e0ebf9c4b7c.png

small viewport

svh represents the viewport height when the address bar UI has not been scaled down.

38d7635d648dab415a93d081d679844d.png

large viewport

lvh indicates the viewport height of the reduced size of the address bar UI.

018b918134de308f96df0301efcd8755.png

dynamic viewport

From the name, this unit is dynamic. This means it will use small, middle and large units depending on whether the address bar UI is shrinking or not.

6fe1fd9b206b9fba8a2b892b97600a00.png

During initial scrolling, the dynamic viewport units will change as the browser UI shrinks. Here's a video showing dynamic viewport changes:

3f4491c783e9b05e9fcd3594ec21c082.gif

Use Cases and Examples

Popup with sticky header and footer

141925cbdeddc069e932877d3ad8d7a7.png

In this example we have a popup with a sticky header and footer. If the content is long enough, the middle part should scroll.

Consider the following CSS:

.modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 100vh;
}

Using 100vh will make the bottom part of the popover invisible. In the example, this means that the footer will not be visible, which breaks the user experience.

Here's how traditional and new viewport units behave on iOS:

679ec8949ca21ace230135af4f1b7cc5.png

..plus Chrome and Firefox on Android:

1d8e5adc8847a1c8d7ba7d51006c08da.png

To solve this problem, we can use svh or dvh units.

Here's a video that shows the difference between dvh and vh.

b499b2e317c477780db7f7222fbc76f0.gif

Home Banner

a7adf24c55c102e8036efd5bddb123ed.png

This is a common situation where we need to make the height of the homepage banner equal to the entire viewport height minus the header height. In this case, using traditional vh will fail in browsers such as iOS Safari, Firefox, and Chrome for Android that shrink the UI when scrolling.

First, we need to make sure the header height is fixed. I used min-height for this.

:root {
  --header-height: 60px;
}

.site-header {
  position: sticky;
  top: 0;
  min-height: var(--header-height, initial);
}

After that, I add min-height to the homepage banner and use calc().

.hero {
  min-height: calc(100svh - var(--header-height));
}

When using vh, the decoration elements (purple) are not visible at all. In fact, if you look closely, you'll see that it appears blurry beneath the address bar UI in iOS Safari, and is cropped in Android browsers.

Here's a comparison of svh and vh on Safari iOS.

c6ed8e8457aa3c09c970d45de78b2dca.png

..plus Chrome and Firefox on Android:

d9413877ef2a1809afb8e599fd708f8e.png

Watch the video below and find out the difference between using svh and vh.

9b4a6a49c5067bd2d350cdf75688b73c.gif

In this case, using svh will do the trick.

Is it possible to set Dvh as default unit?

At first, the answer was "yes, why not?". Then I thought, the dvh value changes as you scroll, so when it's used for something like font-size it can make for a confusing experience.

h1 {
  font-size: calc(1rem + 5dvh);
}
7db6d985e48a3522d2a740d66cb7d7b8.png

Watch the following video and notice how the font size changes when the address bar UI shrinks:

89700da6bc59f4e17fe6aa5392420b39.gif

Demo

Be careful with Dvh viewport units

Dynamic viewport units can affect the performance of the page because the browser spends a lot of work recalculating styles as the user scrolls up or down.

I haven't had a chance to do intensive performance testing, but I would use it with care. I wish I had time to update this here.

Other places where the new viewport units are useful

These new viewport units may not only apply to mobile browsers. In fact, you can now browse the web on your TV. Who knows what kind of browser will appear on the TV with a UI that changes when scrolling, resizing the viewport?

For example, here's a sample home banner viewed on Android TV:

c6faedba1879a9340d940f5522324bc0.png

Works great, even the dynamic UI continues to work.

- END -

About Qi Wu Troupe

Qi Wu Troupe is the largest front-end team of 360 Group, and participates in the work of W3C and ECMA members (TC39) on behalf of the group. Qi Wu Troupe attaches great importance to talent training. There are engineers, lecturers, translators, business interface people, team leaders and other development directions for employees to choose from, and supplemented by providing corresponding training on technical skills, professional skills, general skills, leadership skills, etc. course. Qi Dance Troupe welcomes all kinds of outstanding talents to pay attention to and join Qi Dance Troupe with an open and talent-seeking attitude.

f50e3e34ff1b532040b5e5f30d06057a.png

Guess you like

Origin blog.csdn.net/qiwoo_weekly/article/details/131842596