Skip to content

Instantly share code, notes, and snippets.

@joeyred
Created September 15, 2021 20:12
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 joeyred/f9c9243c665a5462efa15b82d50b1e96 to your computer and use it in GitHub Desktop.
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.
/**
* 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