The width
property in CSS specifies the width of the element’s content1 area. This “content” area is the portion inside the padding, border, and margin of an element (the box model).
.wrap { width: 80%; }
In the example above, elements that have a class name of .wrap
will be 80% as wide as their parent element. The accepted values are any of the length values, in addition to some keywords we’ll cover later.
Width can be overridden by the closely correleated properties min-width and max-width.
.wrapperA { width: 100%; max-width: 320px; /* Will be AT MOST 320px wide */ } .wrapperB { width: 100%; min-width: 20em; /* Will be AT LEAST 20em wide */ }
<!-- Entire Viewport --> <h1>Entire Viewport</h1> <div class="viewport"> <p>Width <code>%</code></p> <div class="box-width percentage">100%</div> <p>Width <code>px</code></p> <div class="box-width pixel">200px</div> <p>Width <code>em</code> <small>(based on 16px computed font-size)</small></p> <div class="box-width em">20em</div> <p>Width <code>rem</code> <small>(based on 16px computed font-size of root element)</small></p> <div class="box-width em">20rem</div> </div> <!-- Container 480px --> <h1>Container 480px</h1> <div class="container"> <p>Width <code>%</code></p> <div class="box-width percentage-half">50%</div> <div class="box-width percentage-full">100%</div> <p>Width <code>px</code></p> <div class="box-width pixel">200px</div> <p>Width <code>em</code> <small>(based on 16px computed font-size)</small></p> <div class="box-width em">20em</div> <p>Width <code>rem</code> <small>(based on 16px computed font-size of root element)</small></p> <div class="box-width em">20rem</div> </div> <!-- Absolute Position --> <h1>Absolute Position</h1> <div class="absolute-position"> <div class="box-width no-width">Based on content if width is not defined</div> <div class="box-width rem"></div> <div class="box-width em"></div> </div> <!-- Table Rows --> <h1>Table Rows :(</h1> <table> <tr class="box-width row-percent"> <td>%</td> </tr> <tr class="box-width row-em"> <td>em</td> </tr> </table>
// ======================================== // Globals // ======================================== html { background: #292a2b; color: #FFF; } .box-width { height: 50px; background: #e78629; } // ======================================== // Demo // ======================================== .viewport { outline: 1px solid #ed862a; .box-width.percentage { width: 100%; // % } .box-width.pixel { width: 200px; // px } .box-width.em { width: 20em; // em } .box-width.em { width: 20rem; // rem } } // ======================================== // Container 480px // ======================================== .container { outline: 1px solid #ed862a; width: 500px; .box-width.percentage-half { width: 50%; // half % } .box-width.percentage-full { width: 100%; // full % } .box-width.pixel { width: 200px; // px } .box-width.em { width: 20em; // em } .box-width.em { width: 20rem; // rem } } // ======================================== // Table Rows and Row Groups // ======================================== .absolute-position { position: relative; .box-width.no-width { // width is calculated based on content outline: 1px solid black; position: absolute; top: 0; right: 0; } } // ======================================== // Table Rows and Row Groups // ======================================== .box-width.row-percent { width: 50%; } .box-width.row-em { width: 20em; }
Digging Deeper
When using percentage (%) for width, authors must be aware that the percentage is based on the element’s parent, or in other words, the width of the containing block. If your parent is set at 480px – as demonstrated by our demo – then the percentage is based on that value. So in our case 50% of 480px leaves us with 240px as a computed pixel value.
Note that width
applies to all elements except non-replaced or inline elements, table rows and row groups (i.e. thead
, tfoot
and tbody
).
For absolutely positioned elements whose containing block is based on a block container element, the percentage is calculated with respect to the width of the padding box of that element.
Width is usable today as an animatable property.
<div class="animating-width">hover to animate width using CSS Transitions</div>
html { background: #292a2b; color: #FFF; } // ========================================= // Width Animation Demo // ========================================= .animating-width { background: #e78629; color: white; height: 100px; width: 50%; @include transition(.3s width ease-in-out); // the transition effect for hover &:hover { cursor: pointer; width: 100%; // animating to full width on hover } }
Keyword Values
With some special keyword values, it is possible to define width (and/or height) according to the content of the element.
min-content
The min-content
value is the smallest measure that would fit around its content if all soft wrap opportunities within the box were taken.
The best example for this kind of value is a properly written figure
element:
<figure> <img src="images/kitten.jpg" alt="" /> <figcaption>What a lovely kitten we got there in this image which is encapsulted in a figure element. How dear, look how long this caption is!</figcaption> </figure>
Once we have applied some basic styles to this markup, we get:
<figure> <img src="http://placekitten.com/400/200" alt="" /> <figcaption>What a lovely kitten we got there in this image which is encapsulted in a figure element. How dear, look how long this caption is!</figcaption> </figure>
figure { margin: 1em auto; background: #EFEFEF; border: 1px solid rgba(0,0,0,.1); padding: 1em; } img { display: block; } figcaption { padding: 1em 0; }
If we wanted that figure element to essentially be the size of that image, so the text wraps at the edges of the image. We could float it left or right, because float will exhibit that same kind of shrink-to-fit behavior, but what if we wanted to center it? min-content
allows us to center it:
<figure> <img src="http://placekitten.com/400/200" alt="" /> <figcaption>What a lovely kitten we got there in this image which is encapsulted in a figure element. How dear, look how long this caption is!</figcaption> </figure>
figure { width: -webkit-min-content; width: -moz-min-content; width: min-content; margin: 1em auto; background: #EFEFEF; border: 1px solid rgba(0,0,0,.1); padding: 1em; } img { display: block; } figcaption { padding: 1em 0; }
Because we’ve assigned min-content
to the figure
element, it takes the minimum width it can have when taking all soft wrap opportunities (like spaces between words) so that the content still fits in the box.
max-content
The max-content
property refers to the narrowest measure a box could take while fitting around its content – if no soft wrap opportunities within the element were taken.
Check out what happens if we apply this to our simple kitten/figure demo:
<figure> <img src="http://placekitten.com/400/200" alt="" /> <figcaption>What a lovely kitten we got there in this image which is encapsulted in a figure.</figcaption> </figure>
figure { width: -webkit-max-content; width: -moz-max-content; width: max-content; margin: 1em auto; background: #EFEFEF; border: 1px solid rgba(0,0,0,.1); padding: 1em; } img { display: block; } figcaption { padding: 1em 0; }
Because the caption is very longer than the image is wide (it doesn’t take any soft wrap opportunity, like the spaces between words), it means it has to display the caption on a single line, thus the figure
is as wide as that line.
fill-available
???. One of life’s great mysteries.
fit-content
The fit-content
value is roughly equivalent to margin-left: auto
and margin-right: auto
in behaviour, except it works for unknown widths.
For instance, let’s say we need to center an inline navigation across the page. Your best bet would be to apply text-align: center
to the ul
, and display: inline-block
to the li
. This would give you something like this:
<ul> <li>Home</li> <li>News</li> <li>About</li> <li>Contact</li> </ul>
ul { background: deepskyblue; padding: 1em; text-align: center; } li { display: inline-block; background: tomato; padding: .5em; }
However, the blue background (from the ul
element) spreads across the entire document because the ul
is a block-level element, which means its width is restricted only by its containing element. What if we want to have the blue background collapsing around the list items? fit-content
to the rescue!
<ul> <li>Home</li> <li>News</li> <li>About</li> <li>Contact</li> </ul>
ul { background: deepskyblue; padding: 1em; width: -webkit-fit-content; width: -moz-fit-content; width: fit-content; margin: 1em auto; } li { display: inline-block; background: tomato; padding: .5em; }
With fit-content
and margin: 1em auto
, this works like a charm and only the navigation has a colored background, not the whole document width.
If you’re into this sort of thing, you’ll be happy to know the formula to define the size of a fit-content length is:
fit-content = min(max-content, max(min-content, fill-available))
This is a pretty unused value, so if you come up with a great use-case, let use know!
Related Properties
Browser Support
For normal length values (e.g. px)
Chrome | Safari | Firefox | Opera | IE | Android | iOS |
---|---|---|---|---|---|---|
Yep | Yep | Yep | Yep | Yep | Yep | Yep |
If authors choose rem
units for width, be mindful that this unit of measurement is not supported in versions of IE 8 and below.
For min-width
and max-width
Chrome | Safari | Firefox | Opera | IE | Android | iOS |
---|---|---|---|---|---|---|
24+ | 5.1+ | 18+ | 12.1+ | 8+ | 2.1+ | 3.2+ |
For the keyword values
Chrome | Firefox | Internet Explorer | Opera | Safari | iOS | Android | |
---|---|---|---|---|---|---|---|
min-content | Yep | Yep | Nope | 15+ | 6.1 | 7 | 4.4 |
max-content | Yep | Yep | Nope | 15+ | 6.1 | 7 | 4.4 |
fill-available | Yep | Kinda | Nope | 15+ | 6.1 | 7 | 4.4 |
fit-content | Yep | Yep | Nope | 15+ | 6.1 | 7 | 4.4 |
- All values need to be prefixed by either
-webkit-
or-moz-
(depending on the vendor) - Kinda for Firefox means it supports
available
rather thanfill-available
Prefixing with Sass
The prefixing for these is a bit tricky, since only a few are required and they differ from browser to browser. Here is a Sass @mixin
to help:
@mixin intrinsic($property, $value) { @if $value == fit-content or $value == min-content or $value == max-content { #{$property}: -webkit-#{$value}; #{$property}: -moz-#{$value}; #{$property}: $value; } @else if $value == fill-available or $value == available { #{$property}: -webkit-fill-available; #{$property}: -moz-available; #{$property}: fill-available; } @else { #{$property}: $value; } }
And you can use it like this:
.element { @include intrinsic(width, min-content); }
Which generates this CSS:
.element { width: -webkit-min-content; width: -moz-min-content; width: min-content; }
Leave a Reply