YogeshChauhan . com

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

amazon

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

How to enable and disable button using JavaScript?JavaScriptHow to change style of nth-child using jQuery?jQueryHow to change style of all direct children of an element using jQuery?jQueryIs there a difference between SCSS and Sass?SCSSHow to create a vertically rotating texts using CSS?CSSSelector Lists and Combinators in SCSS (Sass)SCSSHow to center an image horizontally and vertically?CSSKanban vs Scrum: The two frameworks of agile principlesMiscbin2hex() and chr() String Functions in PHPPHPJavaScript Input Validation Theme Park Example using throw StatementJavaScriptHow to concatenate variable with string in Swift?SwiftShould we ever delete data from a database?Misc