Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Get computed styles
function getStyle(el, styleProp) {
var value, defaultView = el.ownerDocument.defaultView;
// W3C standard way:
if (defaultView && defaultView.getComputedStyle) {
// sanitize property name to css notation (hypen separated words eg. font-Size)
styleProp = styleProp.replace(/([A-Z])/g, "-$1").toLowerCase();
return defaultView.getComputedStyle(el, null).getPropertyValue(styleProp);
} else if (el.currentStyle) { // IE
// sanitize property name to camelCase
styleProp = styleProp.replace(/\-(\w)/g, function(str, letter) {
return letter.toUpperCase();
value = el.currentStyle[styleProp];
// convert other units to pixels on IE
if (/^\d+(em|pt|%|ex)?$/i.test(value)) {
return (function(value) {
var oldLeft =, oldRsLeft = el.runtimeStyle.left;
el.runtimeStyle.left = el.currentStyle.left; = value || 0;
value = + "px"; = oldLeft;
el.runtimeStyle.left = oldRsLeft;
return value;
return value;
Copy link

RafaelLinux commented May 6, 2016

Thank you. It's a great job and make my work easier.

Copy link

guilhermejcgois commented Aug 31, 2017

I've adapted to TS here. Thanks for that gist! 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment