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 How to check if radio button is checked or not using JavaScript? #2 How to add Read More Read Less Button using JavaScript? #3 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #4 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #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
Is monitoring an employee’s work on a computer a desirable or undesirable activity?MiscellaneousWhat’s a page re-rendering in React?ReactHow to hide a DIV on clicks outside of it using jQuery?jQueryHow to convert a number rounding to a specified number of decimals in JavaScript?JavaScriptHow to Install PHP composer in Mac OS Catalina?PHPContent types in DrupalDrupalHow to create a Horizontal Scroll on button click using JavaScript?JavaScriptHow to create a new HTML element programmatically using JavaScript?HTMLCrypto.getRandomValues() method in JavaScriptJavaScriptHow to add MySQL alternate port and sockets/pipes in WordPress?WordPressOrder By and Group By in PostgresPostgresHow to create a CSS full page background image?CSS