Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1050632 of width

  • Revision slug: Web/CSS/width
  • Revision title: width
  • Revision id: 1050632
  • Created:
  • Creator: HTMLValidator
  • Is current revision? No
  • Comment Fixed some invalid CSS

Revision Content

{{CSSRef}}

Summary

The width CSS property specifies the width of the content area of an element. The content area is inside the padding, border, and margin of the element.

The {{cssxref("min-width")}} and {{cssxref("max-width")}} properties override {{cssxref("width")}}.

{{cssinfo}}

Syntax

/* <length> values */
width: 300px;
width: 25em;

/* <percentage> value */
width: 75%;

/* Keyword values */
width: 25em border-box;
width: 75% content-box;
width: max-content;
width: min-content;
width: available;
width: fit-content;
width: auto;

/* Global values */
width: inherit;
width: initial;
width: unset;

Values

<length>
See {{cssxref("<length>")}} for possible units.
<percentage>
Specified as a {{cssxref("<percentage>")}} of containing block's width. If the width of the containing block depends on the width of the element, the resulting layout is undefined.
border-box {{experimental_inline}}
If present, the preceding {{cssxref("<length>")}} or {{cssxref("<percentage>")}} is applied to the element's border box.
content-box {{experimental_inline}}
If present, the preceding {{cssxref("<length>")}} or {{cssxref("<percentage>")}} is applied to the element's content box.
auto
The browser will calculate and select a width for the specified element.
max-content {{experimental_inline}}
The intrinsic preferred width.
min-content {{experimental_inline}}
The intrinsic minimum width.
available {{experimental_inline}}
The containing block width minus horizontal margin, border and padding.
fit-content {{experimental_inline}}
The larger of:
  • the intrinsic minimum width
  • the smaller of the intrinsic preferred width and the available width

Formal syntax

{{csssyntax}}

Examples

Default width

p.goldie {
  background: gold;
}
<p class="goldie">The Mozilla community produces a lot of great software.</p>

{{EmbedLiveSample('Default_width', '500px', '64px')}}

Pixels and ems

.px_length {
  width: 200px;
  background-color: red;
  color: white;
  border: 1px solid black;
}

.em_length {
  width: 20em;
  background-color: white;
  color: red;
  border: 1px solid black;
}
<div class="px_length">Width measured in px</div>
<div class="em_length">Width measured in em</div>

{{EmbedLiveSample('Pixels_and_ems', '500px', '64px')}}

Percentage

.percent {
  width: 20%;
  background-color: silver;
  border: 1px solid red;
}
<div class="percent">Width in percentage</div>

{{EmbedLiveSample('Percentage', '500px', '64px')}}

max-content

p.maxgreen {
  background: lightgreen;
  width: intrinsic;           /* Safari/WebKit uses a non-standard name */
  width: -moz-max-content;    /* Firefox/Gecko */
  width: -webkit-max-content; /* Chrome */
}
<p class="maxgreen">The Mozilla community produces a lot of great software.</p>

{{EmbedLiveSample('max-content', '500px', '64px')}}

min-content

p.minblue {
  background: lightblue;
  width: -moz-min-content;    /* Firefox */
  width: -webkit-min-content; /* Chrome */
}
<p class="minblue">The Mozilla community produces a lot of great software.</p>

{{EmbedLiveSample('min-content', '500px', '155px')}}

Specifications

Specification Status Comment
{{SpecName('CSS3 Box', '#the-width-and-height-properties', 'width')}} {{Spec2('CSS3 Box')}} Added the max-content, min-content, available, fit-content, border-box, content-box keywords.
{{SpecName('CSS3 Transitions', '#animatable-css', 'width')}} {{Spec2('CSS3 Transitions')}} Lists width as animatable.
{{SpecName('CSS2.1', 'visudet.html#the-width-property', 'width')}} {{Spec2('CSS2.1')}} Precises on which element it applies to.
{{SpecName('CSS1', '#width', 'width')}} {{Spec2('CSS1')}} Initial definition

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support {{CompatVersionUnknown}} {{CompatGeckoDesktop("1.0")}} 4 3.5 1.0 (85)
Animatability{{experimental_inline}} {{CompatUnknown}} {{CompatGeckoDesktop("16.0")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
max-content{{experimental_inline}} {{CompatChrome(22.0)}} {{property_prefix("-webkit")}}
{{CompatChrome(46.0)}} [1]
{{CompatGeckoDesktop("1.9")}}{{property_prefix("-moz")}} {{CompatUnknown}} 15 {{property_prefix("-webkit")}} 2.0 (421) (intrinsic value)
6.1 {{property_prefix("-webkit")}}
min-content{{experimental_inline}}

{{CompatChrome(22.0)}} [4] {{property_prefix("-webkit")}}
{{CompatChrome(46.0)}} [1]

{{CompatGeckoDesktop("1.9")}} {{property_prefix("-moz")}} {{CompatUnknown}} 15 {{property_prefix("-webkit")}} 2.0 (421) (min-intrinsic value)
6.1 {{property_prefix("-webkit")}}
available{{experimental_inline}} {{CompatNo}} {{CompatGeckoDesktop("1.9")}} {{property_prefix("-moz")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}[1]
fill-available{{experimental_inline}} {{CompatChrome(22.0)}} {{property_prefix("-webkit")}} {{CompatNo}} {{CompatUnknown}} {{CompatUnknown}} 6.1 {{property_prefix("-webkit")}}
fit-content{{experimental_inline}} {{CompatChrome(22.0)}} [4] {{property_prefix("-webkit")}}
{{CompatChrome(46.0)}} [1]
{{CompatGeckoDesktop("1.9")}} {{property_prefix("-moz")}} {{CompatUnknown}} 15 {{property_prefix("-webkit")}} 6.1 {{property_prefix("-webkit")}}[2]
border-box and content-box{{experimental_inline}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
Feature Android Android Webview Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile Chrome for Android
Basic support {{CompatUnknown}} {{CompatVersionUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatVersionUnknown}}
Animatability{{experimental_inline}} {{CompatUnknown}} {{CompatUnknown}} {{CompatGeckoDesktop("16.0")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
max-content{{experimental_inline}} {{CompatUnknown}} {{CompatChrome(46.0)}} [1] {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatChrome(46.0)}} [1]
min-content{{experimental_inline}} {{CompatUnknown}} {{CompatChrome(46.0)}} [1] {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatChrome(46.0)}} [1]
available{{experimental_inline}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
fill-available{{experimental_inline}} {{CompatUnknown}} {{CompatChrome(46.0)}} [1] {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatChrome(46.0)}} [1]
fit-content{{experimental_inline}} {{CompatUnknown}} {{CompatChrome(46.0)}} [1] {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatChrome(46.0)}} [1]
border-box and content-box{{experimental_inline}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

[1] WebKit implements a variation of this value under the name fill-available as of December 2013.

[2] Earlier versions of WebKit implemented an earlier version of this value under the name intrinsic, but implements fit-content since version 6.1 as well.

[3] Unprefixed.

[4] Old WebKit support intrinsic and min-intrinsic keywords, but removed in Chrome 48.

See also

  • box model, {{cssxref("height")}}, {{cssxref("box-sizing")}}, {{cssxref("min-width")}}, {{cssxref("max-width")}}

Revision Source

<div>{{CSSRef}}</div>

<h2 id="Summary">Summary</h2>

<p>The <strong><code>width</code></strong> <a href="/en-US/docs/Web/CSS">CSS</a> property specifies the width of the content area of an element. The <a href="/en-US/docs/Web/CSS/CSS_Box_Model/Introduction_to_the_CSS_box_model#content-area">content area</a> is <em>inside</em> the padding, border, and margin of the element.</p>

<p>The {{cssxref("min-width")}} and {{cssxref("max-width")}} properties override {{cssxref("width")}}.</p>

<p>{{cssinfo}}</p>

<h2 id="Syntax">Syntax</h2>

<pre class="brush:css">
/* &lt;length&gt; values */
width: 300px;
width: 25em;

/* &lt;percentage&gt; value */
width: 75%;

/* Keyword values */
width: 25em border-box;
width: 75% content-box;
width: max-content;
width: min-content;
width: available;
width: fit-content;
width: auto;

/* Global values */
width: inherit;
width: initial;
width: unset;
</pre>

<h3 id="Values">Values</h3>

<dl>
 <dt><code>&lt;length&gt;</code></dt>
 <dd>See {{cssxref("&lt;length&gt;")}} for possible units.</dd>
 <dt><code>&lt;percentage&gt;</code></dt>
 <dd>Specified as a {{cssxref("&lt;percentage&gt;")}} of containing block's width. If the width of the containing block depends on the width of the element, the resulting layout is undefined.</dd>
 <dt><code>border-box </code>{{experimental_inline}}</dt>
 <dd>If present, the preceding {{cssxref("&lt;length&gt;")}} or {{cssxref("&lt;percentage&gt;")}} is applied to the element's border box.</dd>
 <dt><code>content-box</code> {{experimental_inline}}</dt>
 <dd>If present, the preceding {{cssxref("&lt;length&gt;")}} or {{cssxref("&lt;percentage&gt;")}} is applied to the element's content box.</dd>
 <dt><code>auto</code></dt>
 <dd>The browser will calculate and select a width for the specified element.</dd>
 <dt><code>max-content</code> {{experimental_inline}}</dt>
 <dd>The intrinsic preferred width.</dd>
 <dt><code>min-content</code> {{experimental_inline}}</dt>
 <dd>The intrinsic minimum width.</dd>
 <dt><code>available</code> {{experimental_inline}}</dt>
 <dd>The containing block width minus horizontal margin, border and padding.</dd>
 <dt><code>fit-content</code> {{experimental_inline}}</dt>
 <dd>The larger of:
 <ul>
  <li>the intrinsic minimum width</li>
  <li>the smaller of the intrinsic preferred width and the available width</li>
 </ul>
 </dd>
</dl>

<h3 id="Formal_syntax">Formal syntax</h3>

<pre class="syntaxbox">
{{csssyntax}}</pre>

<h2 id="Examples">Examples</h2>

<h3 id="Default_width">Default width</h3>

<pre class="brush:css">
p.goldie {
  background: gold;
}</pre>

<pre class="brush:html">
&lt;p class="goldie"&gt;The Mozilla community produces a lot of great software.&lt;/p&gt;</pre>

<p>{{EmbedLiveSample('Default_width', '500px', '64px')}}</p>

<h3 id="Pixels_and_ems">Pixels and ems</h3>

<pre class="brush: css">
.px_length {
  width: 200px;
  background-color: red;
  color: white;
  border: 1px solid black;
}

.em_length {
  width: 20em;
  background-color: white;
  color: red;
  border: 1px solid black;
}
</pre>

<pre class="brush: html">
&lt;div class="px_length"&gt;Width measured in px&lt;/div&gt;
&lt;div class="em_length"&gt;Width measured in em&lt;/div&gt;</pre>

<p>{{EmbedLiveSample('Pixels_and_ems', '500px', '64px')}}</p>

<h3 id="Percentage">Percentage</h3>

<pre class="brush: css">
.percent {
  width: 20%;
  background-color: silver;
  border: 1px solid red;
}</pre>

<pre class="brush: html">
&lt;div class="percent"&gt;Width in percentage&lt;/div&gt;</pre>

<p>{{EmbedLiveSample('Percentage', '500px', '64px')}}</p>

<h3 id="max-content">max-content</h3>

<pre class="brush:css;">
p.maxgreen {
  background: lightgreen;
  width: intrinsic;           /* Safari/WebKit uses a non-standard name */
  width: -moz-max-content;    /* Firefox/Gecko */
  width: -webkit-max-content; /* Chrome */
}</pre>

<pre class="brush:html">
&lt;p class="maxgreen"&gt;The Mozilla community produces a lot of great software.&lt;/p&gt;</pre>

<p>{{EmbedLiveSample('max-content', '500px', '64px')}}</p>

<h3 id="min-content">min-content</h3>

<pre class="brush:css">
p.minblue {
  background: lightblue;
  width: -moz-min-content;    /* Firefox */
  width: -webkit-min-content; /* Chrome */
}</pre>

<pre class="brush:html">
&lt;p class="minblue"&gt;The Mozilla community produces a lot of great software.&lt;/p&gt;</pre>

<p>{{EmbedLiveSample('min-content', '500px', '155px')}}</p>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('CSS3 Box', '#the-width-and-height-properties', 'width')}}</td>
   <td>{{Spec2('CSS3 Box')}}</td>
   <td>Added the <code>max-content</code>, <code>min-content</code>, <code>available</code>, <code>fit-content</code>, <code>border-box</code>, <code>content-box</code> keywords.</td>
  </tr>
  <tr>
   <td>{{SpecName('CSS3 Transitions', '#animatable-css', 'width')}}</td>
   <td>{{Spec2('CSS3 Transitions')}}</td>
   <td>Lists <code>width</code> as animatable.</td>
  </tr>
  <tr>
   <td>{{SpecName('CSS2.1', 'visudet.html#the-width-property', 'width')}}</td>
   <td>{{Spec2('CSS2.1')}}</td>
   <td>Precises on which element it applies to.</td>
  </tr>
  <tr>
   <td>{{SpecName('CSS1', '#width', 'width')}}</td>
   <td>{{Spec2('CSS1')}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari (WebKit)</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoDesktop("1.0")}}</td>
   <td>4</td>
   <td>3.5</td>
   <td>1.0 (85)</td>
  </tr>
  <tr>
   <td>Animatability{{experimental_inline}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoDesktop("16.0")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>max-content</code>{{experimental_inline}}</td>
   <td>{{CompatChrome(22.0)}} {{property_prefix("-webkit")}}<br />
    {{CompatChrome(46.0)}} [1]</td>
   <td>{{CompatGeckoDesktop("1.9")}}{{property_prefix("-moz")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>15 {{property_prefix("-webkit")}}</td>
   <td>2.0 (421) (<code>intrinsic</code> value)<br />
    6.1 {{property_prefix("-webkit")}}</td>
  </tr>
  <tr>
   <td><code>min-content</code>{{experimental_inline}}</td>
   <td>
    <p>{{CompatChrome(22.0)}} [4] {{property_prefix("-webkit")}}<br />
     {{CompatChrome(46.0)}} [1]</p>
   </td>
   <td>{{CompatGeckoDesktop("1.9")}} {{property_prefix("-moz")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>15 {{property_prefix("-webkit")}}</td>
   <td>2.0 (421) (<code>min-intrinsic</code> value)<br />
    6.1 {{property_prefix("-webkit")}}</td>
  </tr>
  <tr>
   <td><code>available</code>{{experimental_inline}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatGeckoDesktop("1.9")}} {{property_prefix("-moz")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}<sup>[1]</sup></td>
  </tr>
  <tr>
   <td><code>fill-available</code>{{experimental_inline}}</td>
   <td>{{CompatChrome(22.0)}} {{property_prefix("-webkit")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>6.1 {{property_prefix("-webkit")}}</td>
  </tr>
  <tr>
   <td><code>fit-content</code>{{experimental_inline}}</td>
   <td>{{CompatChrome(22.0)}} [4] {{property_prefix("-webkit")}}<br />
    {{CompatChrome(46.0)}} [1]</td>
   <td>{{CompatGeckoDesktop("1.9")}} {{property_prefix("-moz")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>15 {{property_prefix("-webkit")}}</td>
   <td>6.1 {{property_prefix("-webkit")}}<sup>[2]</sup></td>
  </tr>
  <tr>
   <td><code>border-box</code> and <code>content-box</code>{{experimental_inline}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Android Webview</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Phone</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
   <th>Chrome for Android</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td>Animatability{{experimental_inline}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoDesktop("16.0")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>max-content</code>{{experimental_inline}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatChrome(46.0)}} [1]</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatChrome(46.0)}} [1]</td>
  </tr>
  <tr>
   <td><code>min-content</code>{{experimental_inline}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatChrome(46.0)}} [1]</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatChrome(46.0)}} [1]</td>
  </tr>
  <tr>
   <td><code>available</code>{{experimental_inline}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>fill-available</code>{{experimental_inline}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatChrome(46.0)}} [1]</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatChrome(46.0)}} [1]</td>
  </tr>
  <tr>
   <td><code>fit-content</code>{{experimental_inline}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatChrome(46.0)}} [1]</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatChrome(46.0)}} [1]</td>
  </tr>
  <tr>
   <td><code>border-box</code> and <code>content-box</code>{{experimental_inline}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] WebKit implements a variation of this value under the name <code>fill-available</code> as of December 2013.</p>

<p>[2] Earlier versions of WebKit implemented an earlier version of this value under the name <code>intrinsic</code>, but implements <code>fit-content</code> since version 6.1 as well.</p>

<p>[3] Unprefixed.</p>

<p>[4] Old WebKit support&nbsp;<code>intrinsic</code> and <code>min-intrinsic</code> keywords, but removed in <a href="https://www.chromestatus.com/feature/5758434351775744">Chrome 48</a>.</p>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/CSS/CSS_Box_Model/Introduction_to_the_CSS_box_model">box model</a>, {{cssxref("height")}}, {{cssxref("box-sizing")}}, {{cssxref("min-width")}}, {{cssxref("max-width")}}</li>
</ul>
Revert to this revision