Style widthProperty
Style Object
Definition and Usage
The width property sets or returns the width of an element.
The width property only takes effect on block-level elements or elements with absolute/fixed positioning. Overflow content can be constrained by the overflow property.
Syntax
Setting the width property:
Object.style.width="auto|length|%|inherit"
Returning the width property:
Object.style.width
| Value | Description |
|---|---|
| auto | Default. The width set by the browser. |
| length | Defines the width using units such as px, cm, etc. |
| % | Defines the width as a percentage of the parent element. |
| inherit | The value of the width property is inherited from the parent element. |
Browser Support
![]()
![]()
![]()
![]()
![]()
All major browsers support the width property.
Note:IE7 and earlier versions do not support the "inherit" value. IE8 only supports "inherit" if a !DOCTYPE is specified. IE9 supports "inherit".
Example
Example
Set the width of a button:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Example (example.com)</title>
<script>
function displayResult(){
document.getElementById("b1").style.width="300px";
}
</script>
</head>
<body>
<input type="button" id="b1" onclick="displayResult()" value="Modify the button's width">
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>Example (example.com)</title>
<script>
function displayResult(){
document.getElementById("b1").style.width="300px";
}
</script>
</head>
<body>
<input type="button" id="b1" onclick="displayResult()" value="Modify the button's width">
</body>
</html>
Try it Yourself »
Style Object Other Extensions