CSS width attribute

Definition and Usage

Sets the width of the element.

Description

This property defines the width of the element's content area, where padding, borders, and margins can be added outside the content area.

Inline non-replaced elements will ignore this property.

See Also:

CSS Tutorial:CSS Dimensions

CSS Tutorial:Overview of CSS Box Model

CSS Reference Manual:height property

HTML DOM Reference Manual:width property

Example

Set the height and width of paragraphs:

p
  {
  height:100px;
  width:100px;
  }

Try It Yourself

CSS Syntax

width: auto|value|initial|inherit;

Property Value

Value Description
auto Default Value. The browser can calculate the actual width.
length Define the width using units such as px, cm, etc.
% Defines a percentage width based on the width of the containing block (parent element).
inherit Specifies that the width property should be inherited from the parent element.

Technical Details

Default Value: auto
Inheritance: no
Version: CSS1
JavaScript Syntax: object.style.width="50px"

Try It Yourself Example

Set the width of an element using pixel values
This example demonstrates how to set the width of an element using pixel values.
Set the width of an element using percentages
This example demonstrates how to set the width of an element using percentage values.

Browser support

The numbers in the table indicate the first browser version that fully supports this attribute.

Chrome IE / Edge Firefox Safari Opera
1.0 4.0 1.0 1.0 3.5