The result of a getComputedStyle
contains a property named \"margin\", but the property is always an empty string (\"\"
) in Mozilla Firefox or Appl
The getComputedStyle()
function should not evaluate the values of shorthand properties (such as margin
, padding
), only longhand properties (such as margin-top
, margin-bottom
, padding-top
). In the case of shorthand properties it should only return an empty string.
var el = document.body.appendChild(document.createElement('div'));
el.style.margin = '2px';
var computed = getComputedStyle(el);
var longhands = ['margin-top', 'margin-bottom', 'margin-left', 'margin-right'];
longhands.forEach(function(e) { console.log(e + ': ' + computed.getPropertyValue(e)) });
In addition, you can take a look at this link for a cross-browser solution, which uses currentStyle
for internet explorer