Last active
June 12, 2020 08:58
-
-
Save wklsh/aedaa19c36ee7437b8d73dbd27557c6d to your computer and use it in GitHub Desktop.
Responsive 1vh css variable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... | |
:root { | |
--vh: 1vh; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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