Skip to content
Create a gist now

Instantly share code, notes, and snippets.

emToPx - convert global em-based values to pixels
/*
* emToPx: convert a global em-based value to pixels
* Copyright 2011, Scott Jehl, scottjehl.com
* MIT License
* Usage: emToPx function accepts a single number/float argument, returns a number
*/
var emToPx = (function( win ){
var doc = win.document,
body = doc.body,
prop = "fontSize",
dSize = 16,
valCache = {},
fBody;
return function( val ){
if( !valCache[ val ] ){
//no body yet?
if( !body ){
fBody = doc.createElement( "body" );
var docElem = doc.documentElement;
docElem.insertBefore( fBody, docElem.firstElementChild || docElem.firstChild );
body = fBody;
}
//get body's font size
if( body.currentStyle ){
dSize = body.currentStyle.fontSize;
}
else if( window.getComputedStyle ){
window.getComputedStyle( body,null ).getPropertyValue( "height" );
}
//cache val for repeat lookups
valCache[ val ] = Math.round( val * dSize );
}
return valCache[ val ];
};
}( this ));
@paulirish
window.getComputedStyle( body,null ).getPropertyValue( "height" )

to

getComputedStyle( body,null ).height

otherwise... :+1:

@beep
beep commented Apr 28, 2011

Awesome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.