Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can anyone confirm "getBoundingClientRect" has a bug Chrome?

<!DOCTYPE html>
<html>
<head>
  <style>
  body {
    margin: 0;
  }
  .main {
    width: 200px;
    height: 200px;
    background-color: gray;
    padding: 20px;
  }
  #box {
    width: 100px;
    height: 100px;
    zoom: 10%;
    background-color: red;
  }
  </style>
</head>

<body>
  <div class="main">
    <div id="box">box</div>
  </div>
  <script>
    console.log(document.getElementById('box').getBoundingClientRect());
    document.write(`<p>left: ${document.getElementById('box').getBoundingClientRect().left}</p>`);
    // left and top should be 20, but they are 200. They are divided by element's "zoom" attribute.
  </script>
</body>
</html>


What I have tried:

I get different results for Chrome and Edge, and I understand Chrome is wrong.
Edge returns 20, Chrome returns 200.
Posted
Updated 9-Nov-20 4:32am

1 solution

Not sure how you've got Edge to return 20 - are you still on the old version?

Demo[^]
  • Firefox 82 displays 20;
  • Edge 86 displays 200;
  • Chrome 86 displays 200;


This appears to be connected to the zoom property. If you comment out the property, Chrome and Edge both report 20:
Updated demo[^]

NB: zoom is a non-standard property, with limited browser support. It is not supported in Firefox or the old version of Edge.
zoom - CSS: Cascading Style Sheets | MDN[^]

It looks like there is no consensus about how this property should interact with the bounding rect:
429140 - chromium - An open-source project to help move the web forward. - Monorail[^]

You should consider using transform:scale() instead of zoom. However, this will not affect the layout size of the element.

With transform:scale(0.1) replacing zoom:10%, all three browsers return the same value:
Demo[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900