Hi,
How to get actual height and width of element in HTML using JavaScript.
<div style="width: 300px; height: 1000px"> <h3>ASPSnippets</h3> </div>
Hi makenzi.exc,
Use theJavaScript offsetHeigh and offsetWidth property.
Refer below example.
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title></title> <style type="text/css"> body { font-family: Arial; font-size: 10pt; } </style> </head> <body> <div id="dvContent" align="center" style="background: #61028D; width: 300px; height:50px; border: 1px solid #61028D"> <div style="width: 300px"> <h3 style="color:white">Welcome to ASPSnippets</h3> </div> </div> <br /> <input type="button" value="Get Dimension" onclick="GetDimension()" /> <script type="text/javascript"> function GetDimension() { // Reference Element. const element = document.getElementById('dvContent'); // Get Height without border. const height = element.offsetHeight - 2; // Get Width without border. const width = element.offsetWidth - 2; alert("Height is " + height + "px.\nWidth is " + width + "px."); } </script> </body> </html>
Demo
Screenshot
Downloads
Download Sample
© COPYRIGHT 2025 ASPSnippets.com ALL RIGHTS RESERVED.