How to get the background color of an HTML element?

Rakesh Juyal picture Rakesh Juyal · Dec 11, 2009 · Viewed 156.5k times · Source

How can I get the background color of any element, like a div, using JavaScript? I have tried:

Answer

phnghue picture phnghue · Sep 23, 2014

Get at number:

window.getComputedStyle( *Element* , null).getPropertyValue( *CSS* );

Example:

window.getComputedStyle( document.body ,null).getPropertyValue('background-color');  
window.getComputedStyle( document.body ,null).getPropertyValue('width');  
~ document.body.clientWidth