Skip to content

Instantly share code, notes, and snippets.

@esedic
Created December 5, 2023 11:42
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 esedic/bf6451b8ddfdc654c23adea97095780c to your computer and use it in GitHub Desktop.
Save esedic/bf6451b8ddfdc654c23adea97095780c to your computer and use it in GitHub Desktop.
Calculate em to pixel value with JavaScript
function em2px(em) {
return Number(em) * Number(window.getComputedStyle(document.body).getPropertyValue('font-size').match(/\d+/)[0]);
}
console.log(em2px(2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment