Set the Width and Height of an Element using JavaScript

avatar

Borislav Hadzhiev

Thu Dec 30 20213 min read

banner

Photo by Seb

Set the Width and Height of an Element using JavaScript #

Use the style.width and style.height properties to set the width and height of an element, e.g. box.style.width = '100px'. The width and height properties set the element's width and height to the supplied values.

Here is the HTML for the examples in this article.

index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <div id="box" style="background-color: salmon">Box 1</div> <script src="index.js"></script> </body> </html>

And here is the related JavaScript code.

index.js
const box = document.getElementById('box'); // ✅ Set width to 100px box.style.width = '100px'; // ✅ Set height to 100px box.style.height = '100px';

The style object allows us to set, read or update any CSS property on the element.

index.js
const box = document.getElementById('box'); // ✅ Set width to 100px box.style.width = '100px'; // ✅ Set height to 100px box.style.height = '100px'; console.log(box.style.width); // 👉️ "100px" console.log(box.style.height); // 👉️ "100px"

Note that setting the width and height on an inline element, such as a span has no effect.

index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <span id="box" style="background-color: salmon">Box 1</span> <script src="index.js"></script> </body> </html>

And here is our attempt to update the element's height and width.

index.js
const box = document.getElementById('box'); // ✅ Set width to 100px box.style.width = '100px'; // ✅ Set height to 100px box.style.height = '100px';
If you open your browser, you will see that the element's width and height are determined by the content area.

To solve this, we can set the element's display property to inline-block.

index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <span id="box" style="background-color: salmon; display: inline-block" >Box 1</span > <script src="index.js"></script> </body> </html>

And now we are able to set the element's width and height.

index.js
const box = document.getElementById('box'); // ✅ Set width to 100px box.style.width = '100px'; // ✅ Set height to 100px box.style.height = '100px';

Some examples online use the setAttribute method to update the element's height and width, however the setAttribute method overrides the style property completely.

index.js
const box = document.getElementById('box'); box.setAttribute('style', 'width: 100px; height: 100px');

The setAttribute method takes 2 parameters:

  1. The name of the attribute we want to set on the element.
  2. The value that should be assigned to the attribute.
If the attribute already exists, the value is updated, otherwise a new attribute is added with the specified name and value.

So if you use the setAttribute method approach, you are effectively replacing the element's style attribute value.

This can be very confusing and difficult to debug, so it's best to set any CSS properties using the style object on the element.

Set the Width and Height on a Collection of Elements #

If you need to set the width and height on a collection of elements, you have to:

  1. Select the collection of elements.
  2. Use the for...of method to iterate over the collection.
  3. Set the width and height using the style.width and style.height property on each element.
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <div class="box" style="background-color: salmon">Box 1</div> <div class="box" style="background-color: salmon">Box 2</div> <div class="box" style="background-color: salmon">Box 3</div> <script src="index.js"></script> </body> </html>

And here is the related JavaScript code.

index.js
const boxes = document.querySelectorAll('.box'); for (const box of boxes) { box.style.width = '100px'; box.style.height = '100px'; }

We used the document.querySelectorAll method to select all elements with a class of box.

We then used the for...of loop to iterate over the collection and set the width and height properties on each element.

Use the search field on my Home Page to filter through my more than 1,000 articles.