Example
Set the width of a <button> element:
document.getElementById("myBtn").style.width = "300px";
Try it Yourself »
Definition and Usage
The width property sets or returns the width an element.
The width property has effect only on block-level elements or on elements with absolute or fixed position. The overflowing content can be manipulated with the overflow property.
Tip: Use the height property to set or return the height of an element.
Browser Support
The width property is supported in all major browsers.
Syntax
Return the width property:
object.style.width
Set the width property:
object.style.width="auto|length|%|initial|inherit"
Property Values
Value | Description |
---|---|
auto | The browser sets the width. This is default |
length | Defines the width in length units |
% | Defines the width in % of the parent element |
initial | Sets this property to its default value. Read about initial |
inherit | Inherits this property from its parent element. Read about inherit |
Technical Details
Default Value: | auto |
---|---|
Return Value: | A String, representing the width of an element |
CSS Version | CSS1 |
More Examples
Example
Change the width of a <div> element:
document.getElementById("myDIV").style.width = "500px";
Try it Yourself »
Example
Change the height and width of an <img> element:
document.getElementById("myImg").style.height = "300px";
document.getElementById("myImg").style.width = "300px";
Try it Yourself »
Example
Return the width of an <img> element:
alert(document.getElementById("myImg").style.width);
Try it Yourself »
Related Pages
CSS tutorial: CSS Dimension
CSS tutorial: CSS Box model
CSS reference: width property
< Style Object