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")}} |
{{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")}}