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.


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 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
4 different ways to create JavaScript ObjectsJavaScriptA Step by Step Guide to Make RSS in XML For Any Website or Blog For FreeMiscellaneousLearn how to add Scroll Indicator using CSS and JavaScript?CSSHere’s what we can do with PHP date() functionPHPHow to change style of nth-child using jQuery?jQueryJavaScript String fromCharCode() MethodJavaScriptHow to create a Random Hex Color generator using JavaScript?JavaScriptWordPress: How to find all posts with a specific custom field value?WordPressHow to create ‘share on LinkedIn’ link using just HTML?HTMLHow to solve “framework not found” error in Swift?SwiftUnary Operators in JavaScriptJavaScriptRecursive WITH Queries in Postgres (Common Table Expressions)Postgres