Style width property
- Previous Page whiteSpace
- Next Page wordBreak
- Go Back to Previous Level Object na Style ng HTML DOM
Definition and Usage
width
property sets or returns the width of an element.
width
The property is only valid for block-level elements or elements with absolute or fixed positioning. Overflowing content can be handled using overflow property to handle.
Tip:Use height property Sets or returns the height of an element.
See also:
CSS Tutorial:CSS Dimension
CSS Tutorial:CSS Frame Model
CSS Reference Manual:width property
Example
Example 1
Set the width of the <button> element:
document.getElementById("myBtn").style.width = "300px";
Example 2
Change the width of the <div> element:
document.getElementById("myDIV").style.width = "500px";
Example 3
Change the height and width of the <img> element:
document.getElementById("myImg").style.height = "300px"; document.getElementById("myImg").style.width = "300px";
Example 4
Return the width of the <img> element:
alert(document.getElementById("myImg").style.width);
Syntax
Return width property:
object.style.width
Set width property:
object.style.width = "auto|length|%|initial|inherit"
Property Value
Value | Description |
---|---|
auto | Browser sets the width. Default. |
length | Defines the width in length units. |
% | Defines the width of the parent element in percentages. |
initial | Sets this property to its default value. See initial. |
inherit | Inherits this property from its parent element. See inherit. |
Technical Details
Default value: | auto |
---|---|
Return value: | A string that represents the width of an element. |
CSS Version: | CSS1 |
Browser Support
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
Support | Support | Support | Support | Support |
- Previous Page whiteSpace
- Next Page wordBreak
- Go Back to Previous Level Object na Style ng HTML DOM