How to get all CSS of element

It's 2020 and we can finally just do

getComputedStyle(element).cssText

Example:

newElement.style.cssText = getComputedStyle(element).cssText


Beware that getComputedStyle literally returns the computed values. Meaning relative units such as em will be returned as computed px values (as seen in the computed tab of your browser's devtools).

Depending on your desired outcome, this might completely invalidate it as a viable solution. The other answers - including the accepted one - have failed to mention it entirely, though.

Not enough rep to comment hence this separate answer.


MyDiv001.style.cssText will return only inline styles, that was set by style attribute or property.

You could use getComputedStyle to fetch all styles applied to element. You could iterate over returned object to dump all styles.

function dumpCSSText(element){
  var s = '';
  var o = getComputedStyle(element);
  for(var i = 0; i < o.length; i++){
    s+=o[i] + ':' + o.getPropertyValue(o[i])+';';
  }
  return s;
}