Created
September 15, 2021 20:12
-
-
Save joeyred/f9c9243c665a5462efa15b82d50b1e96 to your computer and use it in GitHub Desktop.
Coverts pixel values to em based on the font size set to the html tag.
This file contains hidden or 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
/** | |
* Converts pixel values to em values based on the `html` level `font-size`. | |
* @method pixelsToEms | |
* @param {Number|String} pixels - pixel value to be converted to ems. | |
* @return {Number} - value in ems. | |
*/ | |
function pixelsToEms(pixels) { | |
let html = document.querySelector('html'); | |
return pixels / parseFloat(getComputedStyle(html)['font-size']); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment