English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

CSS Reference Manual

CSS @rules (RULES)

Comprehensive CSS Properties

CSS width attribute usage and examples

The width CSS attribute specifies the width of the content area of the element. This content does not include padding, border, or margin. Please refer toCSS box model.

The following table explains the usage and version history of this attribute, as well as the syntax of its use in JavaScript scripts.

Default value:auto
Applies to:All elements except replaced embedded elements, table rows, and row groups
Inheritance:No
Animatable:Yes.Please refer to Animation attribute.
Version:CSS 1,2,3
JavaScript syntax:object.style.width="280px"

Syntax of width usage

The syntax of this attribute is as follows:

width: length | percentage | auto | initial | inherit

The following examples demonstrate how to use the width attribute.

  p {
   width: 300px;
   background-color: #f0e68c;
  }
Test to see‹/›

Attribute value

The following table describes the values of this attribute.

ValueDescription
lengthSpecify the width as a length value, in units such as px, pt, cm, em, etc. Negative length values are not allowed.
percentageSpecify the width as a percentage. The percentage is calculated relative to the width of the element's containing block. Negative percentage values are not allowed.
autoThe browser will calculate the width of the specified element based on the values of other attributes. This is the default value.
initialSet this attribute to its default value.
inheritIf specified, the associated elements adopt the width attribute value of their parent element.

Browser compatibility

Browser compatibility, the numbers in the table below represent the minimum version number of browsers that support this attribute; all mainstream browsers support this attribute.

  • Firefox 1+

  • Google Chrome1+

  • Internet Explorer 4+

  • Apple Safari 1+

  • Opera 4+

Read more

Please refer to the tutorials related to the following content:CSS Box Model,CSS dimensions.

Related attributes:max-width,min-width.