Last updated: Mar 5, 2024
Reading time·3 min
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.
<!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>
And here is the related JavaScript code.
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.
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.
<!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>
And here is our attempt to update the element's height and width.
const box = document.getElementById('box'); // ❌ Set width to 100px box.style.width = '100px'; // ❌ Set height to 100px box.style.height = '100px';
width
and height
are determined by the content area.To solve this, we can set the element's display
property to inline-block
.
<!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>
And now we can set the element's width and height.
const box = document.getElementById('box'); // ✅ Set width to 100px box.style.width = '100px'; // ✅ Set height to 100px box.style.height = '100px';
Some examples use the setAttribute()
method to update the element's height and width, however, the setAttribute
method overrides the style
property completely.
const box = document.getElementById('box'); box.setAttribute('style', 'width: 100px; height: 100px');
The setAttribute
method takes 2 parameters:
name
of the attribute we want to set on the element.value
that should be assigned to the attribute.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.
If you need to set the width
and height
on a collection of elements, you
have to:
for...of
method to iterate over the collection.width
and height
using the style.width
and style.height
property on each element.<!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>
And here is the related JavaScript code.
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.
You can learn more about the related topics by checking out the following tutorials: