Set the Width and Height of an Element using JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 5, 2024
3 min

banner

# 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.

index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>bobbyhadz.com</title> </head> <body> <div id="box" style="background-color: salmon">Box 1</div> <script src="index.js"></script> </body> </html>
The code for this article is available on GitHub

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';

set width and height of element using javascript

The code for this article is available on GitHub

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"

# Setting width and height has no effect on inline elements

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" /> <title>bobbyhadz.com</title> </head> <body> <span id="box" style="background-color: salmon">Box 1</span> <script src="index.js"></script> </body> </html>
The code for this article is available on GitHub

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';
The code for this article is available on GitHub
If you open your browser, you will see that the element's width and height are determined by the content area.

# Setting the element's display to inline-block

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

index.html
<!DOCTYPE html> <html lang="en"> <head> <title>bobbyhadz.com</title> <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>
The code for this article is available on GitHub

And now we can 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';

setting element display to inline block

The code for this article is available on GitHub

Some examples 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> <title>bobbyhadz.com</title> <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>
The code for this article is available on GitHub

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'; }

set width and height on collection of elements

The code for this article is available on GitHub

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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.