Why can’t I recover the background color with the gift

Asked

Viewed 48 times

1

I’m trying to recover the color of my div but I can’t, I’d like to know why this is happening

My code

<!DOCTYPE html>
<html>
  <head>
    <title></title>
    <meta charset="utf-8">

    <style type="text/css">
        #conteudo{
            height: 200px;
            width: 200px;
            border-radius: 100%;
            background-color: #610B38;
        }
    </style>

</head>
<body>
    <div id="conteudo">oi</div>

    <script type="text/javascript">
        let div = document.getElementById('conteudo')
        console.log(div.style.backgroundColor)
    </script>   
</body>

1 answer

2


The background color of this element is set in CSS; it is not a property of the element, so obviously when you access the property backgroundColor it is empty.

To recover the computed style of the element, you need to use the function getComputedStyle in that way:

let div = document.getElementById('conteudo')
console.log(getComputedStyle(div).backgroundColor)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.