I get 0 in all values when I use getBondingClientRect

49 Views Asked by At

I trying to know the information about a div a element cuz I'm just learining about this function but I get 0 in all the values or sometimes I get this error cannot read properties of null (reading 'getboundingclientrect')

 <style> 
    .box{
        width: 100px;
        height: 100px;
        background:#009879 ;
        margin-top: 500px;
    }
    </style>
</head>
<body>
    <h2 id="title">Element.getBondingClinetRect - JS DOM - dcode</h2>
   <div class="box"></div> 


   <script>
    const box = document.querySelector("div.box");
    
    const rect = box.getBoundingClientRect().top;
    
    console.log(rect);

   </script>
</body>
0

There are 0 best solutions below