Width - Property CSS

This page is also about physical property(ies):
- height
Sets the height of the element.
And the logical properties:
- inline-size
Sets the size of the element in the inline direction. For Latin-based languages, this property is equivalent to width.
- block-size
Sets the size of an element in the block direction. For Latin-based languages, this logical property is equivalent to height.
width
height
inline-size
block-size

Summary of characteristics of the width property

Quick description
Sets the width of the element.
Status
Standard
Usable for
HTML, SVG
Predefined values
auto | min-content | max-content | fit-content
Percentages
Calculated relative to the width of the parent element.
Initial value
auto
Inherited by default
No.
Computed value: during an animation, the width property gradually changes from one value to another.
W3C Module
CSS Box Sizing Module
 🡇  
 🡅  
Document status: WD (Working Draft)

Document status: WD (Working Draft)

Document status: WD (Working Draft)

Document status: REC (Recommendation)

Document status: DEPR (Old specification, declared obsolete or superseded)

Syntax diagram of width.

width - Syntax DiagramSyntax diagram of the width CSS property. See stylescss.free.fr for details. auto auto length length % % min-content min-content max-content max-content fit-content() fit-content()width:;width:;
Syntax diagram of the width property.
The links in the diagram provide more details about the values.

In the diagram, the bold terms are the predefined words of CSS, the other terms are described below:

  • length is a numerical value followed by one of CSS dimension units.
  • % is a percentage calculated relative to the width of the parent element.

Description of the width property.

The property width sets the width of the element.

This property is called "physical" because it affects the width of the element regardless of the writing mode (for example, Asian languages are written from top to bottom).

Method of calculating dimensions.

Warning! The box-sizing property changes the way an element's dimensions are calculated:

Interaction between box-sizing and width
box-sizing:content-box
The width property sets the width of the element's content, excluding padding and border thickness.
Interaction between box-sizing and width
box-sizing:border-box
The property width sets the width of the element, including padding and border thickness.

It is also necessary to take into account the min-width and max-width properties, which can impose constraints on the width of the element and take precedence.

Language support.

To facilitate the management of non-Latin languages, two properties allow you to define the dimensions of an element relative to the writing mode used. These two properties are:

  • inline-size: size of the element in the line-writing direction. For Latin languages, this property defines the width.
  • block-size: Dimension of the element in the block direction. For Latin-based languages, this property defines the height of the element.

⮜───── width ─────⮞
The direction and writing mode of this text adapt to the chosen language.
⮜──── height ────⮞

If you want more explanations on handling non-Latin languages, refer to the tutorial on writing modes.

Values for width.

The values described below apply both to the property width and to the corresponding logical property: inline-size or block-size.

  • width: auto;

    The browser determines the width of the element. In general, the width is equal to width of the parent minus the parent's padding, minus the element's margins. In the example below, a thin line separates the parent's padding from the element's margins.

    width:auto;
  • width: 150px;

    The width of the element is set to the specified dimension. It is a positive number or 0, followed by a unit of measurement (see CSS dimension units). Depending on the value specified, the element may overflow its parent.

    width:150px;
    width:220px;

    If the value is expressed as a percentage, it is calculated relative to the width of the parent minus the parent's padding.

    width:50%;
  • width: min-content; width: max-content; width: fit-content; width: fit-content(d);

    With one of these three values, the width of the element is determined based on its content (intrinsic size):

    • min-content: content width after adding as many line breaks as possible. Refer to the value min-content for a more precise description.
    • max-content: content width without line breaks, except those explicitly written in the source code (HTML). See the page on max-content for a full description.
    • The fit-content value or the function of the same name should be accepted, but it still has some compatibility issues with the function. With fit-content, the width of the element is determined based on its content, but without exceeding the available space or the d dimension specified as a parameter of the function. Line breaks can be inserted if the length of the text exceeds this limit.
      Refer to the description of the value fit-content or the function fit-content() for a full description.
  • width: initial; (auto) width: inherit; width: revert; width: revertLayer; width: unset;

    These values ​​are described in more detail on their respective page: initial, inherit, revert, revert-layer, unset.

Animation of the width property.

The property width is among those most often animated for various visual effects.

This text gradually reveals itself

Programmatic width manipulation.

The code examples below are given with the width property but will be easily transposed to the equivalent logical properties inline-size and block-size.

Changing the value of width with JavaScript.

Two syntaxes are possible.

Javascript
let el = document.getElementById('id'); el.style['width'] = '50%'; // or let el = document.getElementById('id'); el.style.width = '50%';

Retrieving the value assigned to width with JavaScript.

The code below works if the property has been applied directly to the element itself via its style attribute, and not through a CSS selector. The values assigned by the code above can also be retrieved in this way.

Javascript
let el = document.getElementById('id'); let value = el.style['width']; // or let el = document.getElementById('id'); let value = el.style.width;

Retrieving the computed value of width.

The computed value is the one resulting from the evaluation of the relative units (in particular the percentage) and the possible consideration of inherited values. If not, the computed value will be the initial value of the property, that is auto in the case of width.

Javascript
let el = document.getElementById('id'); let value = window.getComputedStyle(el).getPropertyValue('width');

Changing the value of width with JQuery.

JQuery

$('#id').css('width', '50%');

Retrieving the computed value of width with JQuery.

JQuery
let value = $('#id').css('width');

Other code examples.

Other examples of JavaScript code interacting with CSS styles are provided on the page Javascript and CSS.

Try it yourself.

The buttons below apply the entered value to the width property and then display either the value as it was applied, or the computed value. This second option allows you to see how the value of width is serialized. In particular, you can see that all units are converted to pixels.

Simulator.

The simulator below allows you to distinguish between physical properties, such as width, and logical properties like inline-size, which adapt to the writing mode of the language used.

It can also be useful for getting familiar with the less common dimension units, such as vw, vh, vmin, etc. See CSS dimension units.


Writing mode:

width :

inline-size :

To fit this text within the width of the page, the browser may or may not insert line breaks, depending on the value of the width property.

Browsers compatibility with width.

All these properties are properly supported by all current browsers. However, Internet Explorer does not handle logical properties (which take writing direction into account), but IE is no longer used.

Column 1
Proper handling by browsers of the physical property width (which does not take into account the writing mode based on the language) to define the width of the element.
Column 2
Support for the logical property (which takes into account the writing mode of the language) inline-size to define the dimension of an element in the inline direction.
Column 3
Support for the logical property (which takes into account the writing mode of the language) block-size to define the dimension of an element in the block direction.
1
width
property
2
inline-size
property
3
block-size
property
Estimated overall support.
96%
95%
95%

Browsers on computers :

Mobile browsers :

Outdated or marginal browsers :

Internet Explorer

UC Browser pour Androïd

Opéra Mobile

QQ Browser

Baidu Browser

Samsung Internet

Safari

Safari sur IOS

Opéra

Firefox pour Androïd

Chrome

Edge

Firefox

Androïd Brower

Chrome pour Androïd

KaiOS Browser

Opéra mini

Historic of the width property.

  • Cascading Style Sheets specification - Version 1

    This first level of the CSS specification describes the mechanism of cascading style sheets. Authors and readers can add styles to documents. Mainly intended for formatting HTML documents, CSS allows defining colors, fonts, text formatting, spacing (margins), etc. The CSS language is easy to understand, readable, and easy to write; it uses common terms from desktop publishing.

    Regarding width. Initial definition of the width property.
    The width of an element is defined as the width of the content, excluding border thickness, and padding.
    WD
    November 17, 1995
    Working Draft.
    PR
    November 12, 1996
    Proposed for recommendation.
    REC
    December 17, 1996
    Recommendation .
    DEPR
    September 13, 2018
    Old specification, declared obsolete.
  • Cascading Style Sheets Level 2 - Revision 1 Specification

    This level 2 of the specification describes the mechanism of cascading styles in CSS. This language is used to format HTML or XML documents. It supports media-specific style sheets so that authors can adapt the presentation of their documents for visual browsers, auditory devices, printers, braille devices, portable devices, etc.

    Regarding width. Adding the inherit value for the width property to force inheritance of the parent's value.
    Introduced the box-sizing property which modifies the method of calculating the dimensions of an element.
    WD
    November 04, 1997
    Working Draft.
    PR
    March 24, 1998
    Proposed for recommendation.
    REC
    May 11, 1998
    Recommendation .
  • CSS Logical Properties and Values Level 1

    Regarding width. Introduction of two logical properties to define the dimensions of an element contextually in relation to the writing mode of the language used: inline-size and block-size.
    WD
    May 18, 2017
    Working Draft.
    CR
    PR
    REC
  • CSS Box Sizing Module Level 3

    Regarding width. The width property supports values that define a dimension relative to content: min-content, max-content, etc.
    WD
    September 27, 2012
    Working Draft.
    CR
    PR
    REC
  • CSS Box Sizing Module Level 4

    Regarding width. No change to the width property.
    WD
    May 26, 2020
    Working Draft.
    CR
    PR
    REC

See also, regarding the size of the elements.

The properties width, inline-size, and block-size are described in the CSS Box Sizing Module module. Many other definitions contribute to the dimensions of an element.

Properties:

aspect-ratio
Langue française
Sets the ratio of the width to the height of the element.
block-size
Sets the size of an element in the block direction. For Latin-based languages, this logical property is equivalent to height.
box-sizing
Langue française
Defines how block dimensions are calculated: whether or not to include borders and padding.
contain-intrinsic-block-size
Langue française
Sets the dimension of the element in the direction of the blocks, when the element is confined.
contain-intrinsic-height
Langue française
Sets the intrinsic height of the element when it is confined.
contain-intrinsic-inline-size
Langue française
Sets the dimension of the element in the direction of the lines, when the element is confined.
contain-intrinsic-size
Langue française
Sets the dimensions of the element (blocks and rows) when the element is confined.
contain-intrinsic-width
Langue française
Sets the intrinsic width of the element when it is confined.
height
Sets the height of the element.
inline-size
Langue française
Sets the size of the element in the inline direction. For Latin-based languages, this property is equivalent to width.
max-height
Sets a limit for the maximum height of the element.
max-width
Sets the maximum width of the element.
min-height
Sets the minimum height of the element.
min-width
Sets the minimum width of the element.
Width
Sets the width of the element.

Functions:

fit-content()
Calculates a dimension based on content and boundaries.

Values:

fit-content
Calculates the size of an element based on its contents and available space.
max-content
Gives the dimension of a text without inserting line breaks.
min-content
Donne la dimension d'un texte après avoir inséré le plus possible de retours à la ligne.