Repository navigation
Description
Description
Web developers struggle to measure the viewport in a consistent and predictable manner — especially the viewport in the block dimension on mobile devices, when often the browser UI changes size or disappears/reappears. After years of hearing developer frustrations about VH in particular, the CSSWG has defined new units to cover all usecases and web developers would benefit greatly from rapid implementation by browsers. -webkit-fill-available, window.inner-height, percent lengths, and environment variables (with & without fixed or sticky positioning) are also used to measure the viewport and size content/layout to fit. Ideally all of these would be as close to interoperable as possible, recognizing that browser UI is not something we standardize across browsers.
Specification
- New viewport units in CSS Values and Units Module Level 4
- CSS Environment Variables Module Level 1
- window.innerHeight in CSSOM View Module
Tests
TBD
Rationale
Looking at data from multiple surveys, and having talked to web developers for years (teaching about viewport units from stage), it's clear many web developers struggle to size content on mobile screens. They want to use viewport units instead of JavaScript to size elements, are often trying to create an app-like experience that fits the screen exactly, or are otherwise confounded by the browsers that have UI growing or shrinking at the top/bottom of a mobile screen.
The problem is a complex one, and developer often don't know how to express a solution. They instead describe the problem in a variety of ways. For example, from very early results of this year’s State of CSS survey alone:
- “viewport units”
- ”Full screen height is a pain to work with”
- “Can't use viewport units easily, because of Safari”
- “Viewport height/width (100vh/100vw being different on macOS vs. Windows, or different between mobile versus desktop)”
- “Screen Height”
- “iOS viewport sizing”
- “100% viewport width”
- “ios safari: vh and scroll behavior are always trouble”
- “100vh in Safari iOS”
- “state-dependent styling (e.g. styling of stuck, snapped, or on-screen elements)”
and even more.
Developers often center their frustrations around iOS, but the same problem exists in mobile browsers on Android & other operating systems. The actual viewport size changes anytime a browser’s top/bottom UI changes state. And, it turns out, the vh/vw/vmin/vmax units alone do not solve everyone’s use cases at the same time.
The CSSWG did an in-depth analysis of this problem over the last several years, and came to the conclusion that the best solution to solve this is to create additional viewport units.
Let’s focus on viewport height.
- The new
SVHunit will always return the height of the viewport if all of the UI was maximized (leaving the viewport the smallest possible size). LVHwill always return the height of the viewport if all of the UI was minimized (leaving the viewport the largest possible size.)DVHstands for dynamic viewport height, a measurement that changes based on the current state of the UI, always returning the actual current value of the viewport height.
There are similar units for width, max and min — and to cover logical layout, the new inline and block dimensions. Please read the CSS Values and Units Module Level 4 specification to read about all of the new units.
We would like to propose inclusion of all of these new units in Interop 2022. They will solve most of the frustrations web developers are having with sizing their content to the viewport.
Developers have been begging for a solution to this for years.
Activity
jensimmons commentedon Oct 29, 2021
WebKit: https://bugs.webkit.org/show_bug.cgi?id=219287
Gecko: https://bugzilla.mozilla.org/show_bug.cgi?id=1610815
Chromium: https://bugs.chromium.org/p/chromium/issues/detail?id=1093055
foolip commentedon Nov 3, 2021
This is a great proposal, thanks @jensimmons!
Frustration around this was also very prominent in the MDN browser compat survey and interview, see viewport findings for a summary. At the time I linked w3c/csswg-drafts#4329 and I'm happy to see the new units have been defined now.
Here's something I noted in the report back then:
That was in early 2020. Judging by the State of CSS 2021 responses, it seems like the impression still persists that 100vh does something different across mobile browsers. Some of this is probably a lack of awareness, but I wonder if there are some differences around "filling the viewport" that are still causing frustration. @bokand researched Input/Scrolling Interop Investigation 2020 and back then there were differences in when resize events are fired, for example.
Taking a close look together at differences in existing behavior would be great, so that if there's something many developers are running into, we can address that too and have a compelling message that we've made viewport units all around interoperable in 2022.
tomayac commentedon Nov 4, 2021
+1 to this being a massive issue by talking to developers. Articles like The trick to viewport units on mobile (check the number of comments) are widely shared on Twitter and "solutions" like the
-webkit-fill-availablehack keep making the rounds, too.foolip commentedon Nov 4, 2021
I've now analyzed the "Are there any CSS features you have difficulties using because of differences between browsers?" questions from State of CSS 2021 in #7, and the Viewport category there is largely about this issue. Being up there close to Container Queries and
:focus-visibledespite not being mentioned previously in the survey, is a pretty strong signal of importance to me.jgraham commentedon Nov 5, 2021
Apologies, I just added an issue template for proposals, which I should have done much earlier.
Would you be able to rewrite the issue description to match that template?
jensimmons commentedon Nov 5, 2021
I think it's a terrific idea to make this area be about nailing interoperability for the various ways web developers attempt to measure the Viewport — including JavaScript.
height: 100vhheight: 100svh,height: 100lvh,height: 100dvhheight: 100%position: fixed; height: 100%-webkit-fill-availablewindow.inner-heightenv(safe-area-inset-bottom))See examples of these at: http://smfr.org/misc/viewport-units.html
The work to be done:
[-]New Viewport Units[/-][+]Viewport Measurement (including new Viewport Units)[/+]foolip commentedon Nov 12, 2021
Does anyone have a good grasp on the testing situation around this? I've searched for svh, lvh and dvh in web-platform-tests but can't find any existing tests.
Hopefully some of the tests in https://wpt.fyi/results/css/css-values are relevant, a bunch of them include "100vh" at least.
https://wpt.fyi/results/css/css-env spontaneously looks like too few tests to be covering
env()well but I didn't look closer.There seems to be just a single test using
-webkit-fill-available, but actually testing something else. I haven't dug intoinnerHeight, 100% height, "resize" event timing, etc.I think this proposal is on very solid ground when it comes to the rationale and would definitely be noticed by web developers, so I'd hate for a lack of tests today to rule it out. I'm not sure how realistic it is to flesh out the testing sufficiently in the remainder of 2021, but maybe that's doable. Alternatively, I hope we can find a structure for cross-vendor collaboration on solving this issue in 2022, even if doesn't form part of the Interop 2022 metric.
gsnedders commentedon Nov 12, 2021
The WebKit tests for the new viewport units are Obj-C++ so that they can configure the viewport appropriately.
Certainly simply writing tests that assert the new viewport units are supported isn't too hard, nor that they are related to the UA-default viewport appropriately. Testing the dynamic behaviour isn't currently easily automatable in a cross-browser way (and regardless we aren't running tests for wpt.fyi on mobile presently which is where all the interesting dynamic functionality exists).
foolip commentedon Nov 13, 2021
For the new viewport units, I wonder if we could have shallow automated tests, but also write manual tests for things like the behavior when the address bar gets hidden on scroll.
In parallel we could work out what automation would be required for these tests, and maybe automate them for an Interop 2023 metric.
jensimmons commentedon Nov 15, 2021
I might suggest we think of this project in distinct parts, some of which should be included & some of which should not:
Include the new units and anything else that’s already well-specified in Interop 2022. Create/collect tests as best we can. (Definitely do this. This is the MVP.)
Research the current state of implementations in browsers. Document what’s found. This document could be a resource for various working groups to reference as they understand what else they could do to improve this area. Primary goal: document current state. Secondary goal: file bug tickets where browsers are different than the existing specs.
Make a clear list of what’s specified where regarding viewport sizing — highlighting how these interact, and where there is confusion or underspecification.
Drive spec works that’s needed. (Definitely do not do this. Spec work is outside the scope of Interop 2022.)
foolip commentedon Nov 22, 2021
Looking for some
vhtests, https://wpt.fyi/results/css/css-values/viewport-units-css2-001.html is the only one that reveals any difference between browsers. However, I think we could still include all the ones starting with vh* or viewport* in https://wpt.fyi/results/css/css-values.@jensimmons what kinds of tests would you be looking for when it comes to "percent lengths, eg: height: 100%"? In https://wpt.fyi/results/css/css-values there are a bunch of tests with "percent" in the name, but none of those contain "height: 100%". Plenty of other tests (in other directories, in particular css/CSS2/) include "height: 100%" but are any of those relevant?
gsnedders commentedon Dec 1, 2021
So I think the concrete suggestion here is:
height: 100%in various contexts.I don't think any of these are particularly much work.
jensimmons commentedon Dec 1, 2021
Igalia said:
So let me propose a clear, more narrow scope for Viewport Measurements:
svh,lvh,dvh; alsodvb,dvi, etc.foolip commentedon Dec 10, 2021
The new viewport units were accepted as part of Interop 2022 in #39. We'll now need the simple automated tests for those units. @jensimmons @gsnedders is this something that Apple could work on?
There was also support for the cross-browser investigation into other Viewport Measurements. @jensimmons given #4 (comment), is this something that you'd like to take charge of? Google would like to participate in this work.
jensimmons commentedon Dec 10, 2021
I volunteer at Apple's rep for a cross-browser investigation into the overall state of viewport measurements, as described above. I can help organize/facilitate/chair is that's desired.
jensimmons commentedon Dec 10, 2021
I think there's an open question whether we want to:
or
I've recently heard some people articulate an expectation that we are doing 1, and other people that we are doing 2.
Perhaps this depends on whether such texts can return a length value and evaluate whether/not it's correct. Or if automated tests only test if the unit is supported by the browser.
foolip commentedon Dec 14, 2021
For my own part, I would strongly support including automated tests for all aspects of viewport measurement that we can find, so option 2. It's a fair amount of work to identify those tests, but this one at least has some failures:
https://wpt.fyi/results/css/css-values/viewport-units-css2-001.html?label=experimental&label=master&aligned
gsnedders commentedon Dec 16, 2021
https://gist.github.com/07ca400d2e4dd01caa4a8f0c984be068 is all the existing list of files using viewport units in WPT; note there's no tests using the new viewport units. Most of the usage outside of css/css-values is rather incidental, so even if we do include the original viewport units we should probably exclude what's outside of that.
This is the current status of those tests within css/css-values.
foolip commentedon Jan 17, 2022
In #44 we decided to only include tests for the new viewport units in the Interop 2022 metric, and work in #41 towards more tests for existing viewport measurement issues, as well as potentially testing the new units better than we can currently do in automation.
The scope of the tests here are what @gsnedders suggested in #4 (comment):
The test haven't been written yet, and may not be in WPT when the RFC is updated, but this is clear enough so that we can all, as @jgraham said, imagine what those tests will look like.
flackr commentedon Jan 19, 2022
We could probably write a single manual test where the user hides the top controls and we simultaneously test many different things (e.g. all the various CSS units and the resize event).
gsnedders commentedon Jan 19, 2022
Current plan:
CSS.supports("width", "10dvh"); do we just want to check the values can be parsed for width rather than any other properties? (i.e., assume it’s just a valid<length>everywhere)I don't know off-hand if we can actually test the interesting mobile cases using
window.scroll(as I don't know whether that affects browser chrome in different browsers/platforms), because at least in principle it would be nice to have them in a form which would be automated once we eventually start running mobile browsers on upstream WPT.4eb0da commentedon Feb 16, 2022
This issue mainly about viewport height, units, and so on, but I've experienced several other issues about viewport.
I can create seperate issue, if you think so
Inconsistency in "zoom on input focus" behaviour
Several years ago Safari introduced new feature: zoom the page on input focus, if font-size of it is too small (even on mobile-friendly sites)
Firefox for android also worked for this recently, but it doesn't check font-size and uses other logic. I've seen zoom around 110% on focus in FF, which I think is not expected behavour
Both of them can be "fixed" with
user-scalable=no, which is bad for a11y. This solution is popular on resources like stackoverflowSo all browsers requires different approach there. If you don't want this "zoom on focus" behavour and you want to be able to pinch-zoom:
Safari:
Use "user-scalable=no". Safari will ignore it and allow to pinch-zoom, but it will disable the "zoom on focus" feature
FF on android:
Use "user-scalable=no". Page will lose pinch-zoom, but it also disables "zoom on focus" feature
Hope that new solution with "touch-action: manipulation" will come soon and "user-scalable=no" can be removed
Chrome on android:
No "zoom on focus", as far as I know. Doesn't require "user-scalable=no" and the page can be pinch-zoomed as usual
Inconsistency around viewport width
Sometimes your site is not mobile-friendly, but you whant to provide better experience for tablet users,
And sometimes tablets in portrait mode have a width is around 600, which is too low for your site. It means that you cannot safely use "width=device-width"
Also there is no value like "width=max(device-width, 980)"
I cannot rembember it exactly, but I saw:
Problems in browsers with different approaches:
Moreover, Safari on iPad can behave differently, than Chrome on the same iPad
As I said, I can't remember exactly all the nuances, and maybe there is a solution which I've missed, but I can do research, if someone interested in it