/

HTML / CSS / JavaScript Tutorial

CSS Property: width

[this page | pdf | back links]

The CSS (CSS1) width property indicates the width of an element (excluding padding, borders and margins). It is overridden by the min-width or max-width properties, if either of them are present.

 

Valid property values (other than inherit and initial) are:

 

Value

Description

length

Width as a CSS length

%

Width of element defined as a percentage of width of its containing block

auto

(default value). Browser determines width

 

Default Value:

auto

JavaScript syntax:

e.g. object.style.width="300px"

Inherited:

No

Animatable:

Yes

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
p {border: thin solid green;}
p.x1 {width: 200px;}
</style>
</head>
<body>
1. Element with default property<br>
<p>1</p><br>
2. Element set using in-file HTML style
<p class="x1">2</p><br>
3. Element set using JavaScript
<p id="x2">3</p>

<script>
document.getElementById("x2").style.width = "200px";
</script>

</body>
</html>

FUNCTION THAT MAY ASSIST IN TESTING WHETHER FEATURE IS SUPPORTED:
function isSupportedCSSPropertyWidth() {
  var x = document.createElement("DIV"); x.style.width = "200px"; return (window.getComputedStyle(x, null).width == "200px");
}


NAVIGATION LINKS
Contents | Prev | Next | CSS Properties


Desktop view | Switch to Mobile