Why is js getting the width and height of an element empty?

the following acquisition is empty, how can I get it? Do not use offsetWidth

-sharpdiv {
        width: 100px;
        height: 100px;
        background-color: red;
    }
    
    var div = document.getElementById("div")
   console.log(div.style.width)
Aug.25,2021

if your element is not styled in style , then you can't get it through dom.style.XXX .

you can get it from getComputedStyle . Specific usage

</div>[getComputedStyle]<a>1</a>[currentStyle]<a>2</a>:

var ele = document.getElementById("div");
var style = window.getComputedStyle ? 
    window.getComputedStyle(ele, "") : 
    ele.currentStyle;
Menu