mardi 1 décembre 2020

why element.style.backgroundColor property gives an empty string as a result?

console.log(document.querySelector(".green").style.backgroundColor);
// gives an empty string as a result in console
.green {
  width: 200px;
  height: 200px;
  background-color: green;
}
<!DOCTYPE html>
<html>
<head>
    <title>test</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="src/styles.css">
</head>
<body>
    <div class="green"></div>
    <script src="src/index.js">
    </script>
</body>
</html>

I know that I can also use

window.getComputedStyle(document.querySelector(".green")).backgroundColor;

which is mentioned in this answer

but I want to know the reason behind this that why it is giving an empty string as a result.




Aucun commentaire:

Enregistrer un commentaire