Skip to content

Instantly share code, notes, and snippets.

@wklsh
Last active June 12, 2020 08:58
Show Gist options
  • Save wklsh/aedaa19c36ee7437b8d73dbd27557c6d to your computer and use it in GitHub Desktop.
Save wklsh/aedaa19c36ee7437b8d73dbd27557c6d to your computer and use it in GitHub Desktop.
Responsive 1vh css variable
// ...
:root {
--vh: 1vh;
}
window.addEventListener("resize", () => {
// First we get the viewport height and we multiple it by 1% to get a value for a vh unit
const vh = window.innerHeight * 0.01;
// Then we set the value in the --vh custom property to the root of the document
document.documentElement.style.setProperty("--vh", `${vh}px`);
});
.my-element {
height: 100vh; /* Fallback for browsers that do not support Custom Properties */
height: calc(var(--vh) * 100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment