Set the width of a <button> element:
document.getElementById("myBtn").style.width = "300px";
Try it Yourself »
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.
Property | |||||
---|---|---|---|---|---|
width | Yes | Yes | Yes | Yes | Yes |
Return the width property:
object.style.width
Set the width property:
object.style.width = "auto|
length|%|initial|inherit"
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 |
Default Value: | auto |
---|---|
Return Value: | A String, representing the width of an element |
CSS Version | CSS1 |
Change the width of a <div> element:
document.getElementById("myDIV").style.width = "500px";
Try it Yourself »
Change the height and width of an <img> element:
document.getElementById("myImg").style.height = "300px";
document.getElementById("myImg").style.width = "300px";
Try it Yourself »
Return the width of an <img> element:
alert(document.getElementById("myImg").style.width);
Try it Yourself »
CSS tutorial: CSS Dimension
CSS tutorial: CSS Box model
CSS reference: width property
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!