CSS Reference ยท Box Model and Sizing
The width property
Reference for the CSS width property: lengths, percentages, auto, min-content, max-content and fit-content, with live examples and the box-sizing effect.
The width property sets the preferred width of an element's box. By default it applies to the content box, so padding and borders are added on top, unless you change box-sizing. The value auto, which is the default, lets the layout mode choose: a block fills its container, a floated or absolutely positioned box shrinks to fit its content, and flex or grid items follow their own sizing rules.
.card { width: 20rem; } .half { width: 50%; }
Try it
Syntax
width: auto | <length-percentage [0,โ]> | min-content | max-content | fit-content | fit-content(<length-percentage>) | stretch;
- Initial value:
auto - Applies to: all elements except non-replaced inline elements, table rows and row groups
- Inherited: no
- Animatable: yes, as a length, percentage or calc() (keywords such as
autodo not interpolate by default) - Computed value: a percentage or
autoas specified, otherwise an absolute length
Values
| Value | Description |
|---|---|
auto |
The browser computes the width from the layout mode. Blocks fill their container. |
<length> |
A fixed size such as 300px, 20rem or 40ch. |
<percentage> |
A percentage of the containing block's width. |
min-content |
The narrowest the box can be without overflowing, such as the longest word. |
max-content |
The width needed to fit the content on one line without wrapping. |
fit-content |
The content size, but never more than the available space. |
fit-content(<length>) |
Like fit-content, capped at the given length. |
stretch |
Fill the container's available width, including margins. Newer, with growing support. |
Usage notes
- Don't set a width on blocks that should fill their container.
width: autoalready does this and respects margins.width: 100%plus padding and borders overflows the parent unlessbox-sizing: border-boxis set. - Prefer
max-widthfor fluid layouts.width: 100%; max-width: 60remshrinks on small screens and stops on large ones; a fixedwidth: 60remforces horizontal scrolling on narrow screens. - Inline elements ignore it. A
<span>or<a>needsdisplay: inline-blockor another type beforewidthapplies. - Replaced elements such as images use their intrinsic ratio: set only one of
widthorheightand the other follows, unless both are set. - Percentages need a reference. The containing block must have a width; percentage widths inside shrink-to-fit parents can behave in cyclic ways.
- Use
chandremfor text-based widths so a box scales with the font size, for examplemax-width: 65chfor readable lines.
Browser support
Supported in every browser. stretch is newer and may need a vendor-prefixed spelling in older versions, so test before using it.
Related
heightโ the vertical counterpartmin-widthandmax-widthโ lower and upper limitsbox-sizingโ what width includesaspect-ratioโ derive one dimension from the other- Lesson: Width and Height
- Lesson: The Box Model