This post will discuss how to get the width and height of an HTML element with jQuery.

There are several alternatives to get the width and height of an element with JavaScript. This post provides an overview of some of the available alternatives in jQuery.

1. Using width() and height() method

jQuery offers the .width() and the .height() method, which returns the actual width and height of an element’s content, respectively.

For example, the following code returns ‘500 × 100’ value, which is the actual space the displayed content takes up.

JS


HTML


CSS



Edit in JSFiddle

2. Using css() method

Note that both .width() and .height() returns a unit-less value. If you need values with units intact like ‘500px’, use .css() method instead. Here’s a working example, which returns actual width and height of an element in pixels:

JS


HTML


CSS



Edit in JSFiddle

3. Using .outerWidth() and .outerHeight() method

If you need to include padding and border information, and optionally the margin, consider using the .outerWidth() and .outerHeight() method.

JS


HTML


CSS



Edit in JSFiddle

That’s all about getting the width and height of an HTML element with jQuery.

 
Also See:

Get width and height of an element with JavaScript