Skip to content

Instantly share code, notes, and snippets.

@Arecsu
Created June 26, 2022 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Arecsu/d36a1be2ee727f45e0da03f46e580790 to your computer and use it in GitHub Desktop.
Save Arecsu/d36a1be2ee727f45e0da03f46e580790 to your computer and use it in GitHub Desktop.
Real time REAL viewport height - mobile toolbars included

Javascript code to set a CSS var --real-vh with the real value in px. It updates when the browser gets resized.

function realVH() {
  document.documentElement.style.setProperty('--real-vh', (window.innerHeight) + "px")
}

realVH()

window.addEventListener("resize", event => realVH())

CSS code:

body {
  /* you can do whatever you want with it, like use it inside calc(), etc. */
  /* 100vh is a fallback value */
  height: var(--real-vh, 100vh) 
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment