Skip to content
uditbhPublic
forked from Hiswe/vh-check

About

mobile vh unit utility

Resources

Stars

0 stars

Watchers

0 watching

Forks

 
 

Repository files navigation

vh-check

npm version

the problem

Browsers don't always compute the vh unit the same way. Some mobile browsers compute the vh CSS unit without taking care of the url bar. That means that a 100vh div will overflow the viewport by the size of the url bar.

This is the current behavior for:

As explained in the chrome post, that make sense but make it hard to have a full hero top block.

This script will measure the difference and put it in a CSS var.

why not use viewport-units-buggyfill?

It's doing a very good job:

https://github.com/rodneyrehm/viewport-units-buggyfill

But it has some problems with media-queries:

rodneyrehm/viewport-units-buggyfill#13

use

as a global variable

<script src="https://unpkg.com/vh-check/dist/vh-check.min.js"></script>
<script>
  (function () {
    // initialize the test
    var isNeeded = vhCheck('vh-test');
  }());
</script>

as a commonJS module

npm install vh-check
var vhCheck   = require('vh-check');
// isNeeded will be true || false
var isNeeded  = vhCheck();

as a ES module module

npm install vh-check
import vhCheck from 'vh-check';
const isNeeded  = vhCheck();

how it works

  • It will update the vh-check CSS custom property if needed
  • vh-check will be updated on orientation change
  • vh-check will not be updated on scroll event

configuration

You can pass the CSS var name as a param to vhCheck() (default vh-check)

vhCheck('browser-address-bar')

In your CSS you will have to reference:

:root {
  --browser-address-bar: 0px;
}
main {
  min-height: calc(100vh - var(--browser-address-bar));
}

example

in your javascript

vhCheck()

in your CSS

:root {
  --vh-offset: 0px;
}
main {
  /* If you need to support browser without CSS var support */
  height: 100vh;
  /* enable vh fix */
  height: calc(100vh - var(--vh-offset));
}

about browser support

  • vh unit – supported since IE9+
  • calc – supported since IE9+
  • CSS custom properties – supported since IE Edge and iOS 9.3+ IE11 & below will need a fallback without CSS var
  • concerned browsers – as for now:
    • Safari since iOS7+
    • Chrome Android since version 56

demo

github pages

https://hiswe.github.io/vh-check/

local

related

About

mobile vh unit utility

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages