Setting the dimensions of HTML elements properly is highly significant when structuring the layout of a web page because the right dimensions enhance the overall look of your website which in return boosts the user experience. jQuery provides many methods that help you perform this task with great ease.

Dimensions of HTML elements can be set using the below-mentioned jQuery methods.

  1. width() method
  2. height () method
  3. innnerWidth() method
  4. innnerHeight() method
  5. outerWidth() method
  6. outerHeight() method

Let’s explore them in detail.

width() Method

For the purpose of setting or fetching the width of HTML elements, the width() method is used.

This method works in a way that when it is used only to fetch the width of an element it returns the width of the first matched element, however, when used for setting the width, it sets the width of all matched elements.

Syntax

For fetching the width of an element.

For setting the width of an element.

Example

Suppose you want to change the width of a

element using jQuery width() method. Use the code below.

HTML

<div style=“background-color: bisque; height:100px; width:200px; border:2px solid gray;”></div><br>

<button id=“button”>Set width</button>

In the above HTML code, we have created a

, and a

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/B49B6FDCE1F74B3598C4C9BF564AEBEC-150×150.jpg621ecddfbfcec.jpg" height="112" src="data:image/svg xml,” width=”112″>

Naima Aftab

I am a software engineering professional with a profound interest in writing. I am pursuing technical writing as my full-time career and sharing my knowledge through my words.