How to get an HTML element actual width and height in JavaScript

1 Answer

0 votes
<!DOCTYPE html>
<html>
  <head>
    <style>
      #divid {
        height: 200px;
        width: 250px;
        padding: 10px;
        margin: 15px;
        border: 3px solid blue;
      }
    </style>
  </head>
  <body>
    <div id="divid">
      <p>javascript programming</p>
    </div>
    <button onclick="GetWidthHeight()">Click To get width and height</button>
    <script>
      function GetWidthHeight() {
        let element = document.getElementById("divid");
        let width = "Width: " + element.offsetHeight + "px";
        let height = "Height: " + element.offsetWidth + "px";
        console.log(width);
        console.log(height);
      }
    </script>
  </body>
</html>



<!--
run:

"Width: 226px"
"Height: 276px"

-->

 



answered Feb 3, 2021 by avibootz

Related questions

1 answer 232 views
1 answer 224 views
224 views asked Nov 16, 2020 by avibootz
2 answers 352 views
1 answer 211 views
211 views asked May 25, 2016 by avibootz
2 answers 297 views
1 answer 194 views
...