Yogesh Chauhan's Blog

How to get the height and width of an element using JavaScript?

in JavaScript on March 9, 2021

For me, JavaScript is full of magic. IT can do tons of things on the page so nicely and without much server load which make it even more beautiful beast.

We can use the HTML DOM element object and it’s properties to get the height and width of any element. In the HTML DOM Element object is represented by an HTML element, for e.g. p, div, table, a, section, etc.

To get the height of any element we can use scrollHeight property and to get width of the element we can use scrollWidth property.

The scrollHeight property returns the height of an element in pixels. The returned height includes padding, but it doesn’t include the border, margin or scrollbar.

The scrollWidth property returns the width of an element in pixels. The returned width includes padding, but it doesn’t include the border, margin or scrollbar.

Both scrollHeight and scrollWidth properties are read only.

Example


Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
Learn to create your skill bar using CSSCSSJavaScript: Methods for HTML DOM classList PropertyJavaScriptHow to check if radio button is checked or not using JavaScript?JavaScripttransform-origin Property in CSSCSSTwo ways we can use colon(:) in Envision BasicEnvision BasicHow to use images instead of HTML radio buttons using CSS?CSSWhat’s the difference between visibility: hidden and display: none?CSSHTML Copyright SymbolHTMLThe difference between Class declarations and Class expressions in JavaScriptJavaScript3 Common Usability Mistakes In Web DesignUI/UXCreate a responsive pricing table using simple HTML and CSSCSSHow to use $IF operator in Envision Basic?Envision Basic