A surprisingly common response when asking people about things they’d fix about anything in CSS, is to improve the handling of viewport units.
One thing that comes up often is how they relate to scrollbars. For example, if an element is sized to 100vw
and stretches edge-to-edge, that’s fine so long as the page doesn’t have a vertical scrollbar. If it does have a vertical scrollbar, then 100vw
is too wide, and the presence of that vertical scrollbar triggers a horizontal scrollbar because viewport units don’t have an elegant/optional way of handling that. So you might be hiding overflow on the body when you otherwise wouldn’t need to, for example. (Demo)
Another scenario involves mobile browsers. You might use viewport units to help you position a fixed footer along the bottom of the screen. But then browser chrome might come up (e.g. navigation, keyboard, etc), and it may cover the footer, because the mobile browser doesn’t consider anything changed about the viewport size.
Matt Smith documents this problem:
And a solution of sorts:
body {
min-height: 100vh;
min-height: -webkit-fill-available;
}
html {
height: -webkit-fill-available;
}
The above was updated to make sure the html
element was being used, as we were told Chrome is updating the behavior to match Firefox’s implementation.
Does this really work? […] I’ve had no problems with any of the tests I’ve run and I’m using this method in production right now. But I did receive a number of responses to my tweet pointing to other possible problems with using this (the effects of rotating devices, Chrome not completely ignoring the property, etc.)
It would be better to get some real cross-browser solution for this someday, but I don’t see any issues using this as an improvement. It’s weird to use a vendor-prefixed property as a progressive enhancement, but hey, the world is weird.
It’s useful to read the linked article’s comments as they go into further tests on this. Also useful to note that the style used here WILL change down the road, so as long as this is considered a “progressive enhancement” as Chris says then fine, but not if you’re relying on this feature.
Personally I can’t wait for the day when we have a simple and reliable CSS fix for the 100vh issue in the vein of this one-liner down the road. The fact that it’s 2020 and we still don’t have a fix for this is ridiculous.
JS but I like Hiswe/vh-check: https://github.com/Hiswe/vh-check
Tested:
* Android 10 running Chrome 83.0.4103.106
* iPad Mini 2 running iOS 12.4
* iPhone SE (320 x 568vp) running iOS 13.5
-webkit-fill-available
worked like a charm.I solved this method problem in Chrome and released the hack as a PostCSS plugin
https://github.com/postcss/postcss-100vh-fix
It works without JS in all browsers.
I tried it and it seems Chrome and firefox don’t support hyphens in the current versions. both on android as well as windows.
It doesn’t work on iPhone devices when you have a fixed element and keyboard opened. It pushes the element up without resizing the viewport of the element.