Style reading issues in JavaScript

Only styles specified via the style attribute can be read through the style property. This means that the styles set in the CSS file cannot be read.

Let's take a look at an example. Let's say we have the following element:

<div id="elem"> text </div>

Let the following styles be set for it in the CSS file:

#elem { color: red; }

Let's get a reference to an element into a variable:

let elem = document.querySelector('#elem');

Let's try to read the style value:

console.log(elem.style.color); // shows ''
enru