YogeshChauhan.com

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

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

dreamhost

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 uninstall Cocoapods from the Mac OS? #4 How to Use SQL MAX() Function with Dates? #5 How to add Read More Read Less Button using JavaScript? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Apr 7 Solution for “Yarn build: Failed because of a stylelint error” Apr 7 Make sure your links have descriptive text Apr 7 How to add Laravel to WordPress using Sage theme (and install Tailwind CSS)? Apr 2 How to create a sidebar using pure CSS? Apr 1 How to switch dark and light themes using pure CSS? Apr 1 How to calculate elapsed time in JavaScript?

You might also like these

Essential SQL Commands We Need to KnowSQL/MySQLHow to Block IPs and User Agents using code in Drupal or WordPress?DrupalURL paths in DrupalDrupalOrder By and Group By in PostgresPostgres:in-range and :out-of-range selector in CSSCSSHow to obfuscate JavaScript code to hide it from View Source?JavaScriptWhat are components in Angular?AngularSolution to “Call to undefined function mysql_error()” in RevSlider WordPress PluginWordPressIntroduction to components and templates Part 4: Pipes and DirectivesAngularWhere is the PHP log file located on Mac OS?PHPArray destructuring and Object destructuring in JavaScriptJavaScriptWhy do we need HAVING Clause in SQL?SQL/MySQL