Skip to content

Instantly share code, notes, and snippets.

@kflorence
Last active December 16, 2015 22:20
Show Gist options
  • Save kflorence/5506380 to your computer and use it in GitHub Desktop.
Save kflorence/5506380 to your computer and use it in GitHub Desktop.
Convert from em to px and vice versa.
function unit( scope ) {
return parseInt( window.getComputedStyle( scope || document.body ).fontSize, 10 );
}
function pxToEm( px, scope ) {
return ( parseInt( px, 10 ) / unit( scope ) ).toFixed( 8 ) + 'em';
}
function emToPx( em, scope ) {
return Math.round( parseFloat( em ) * unit( scope ) ) + 'px';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment