spacing

width

Sets the horizontal size of an element's content box (or border box if box-sizing: border-box). Modern intrinsic keywords give sizing that adapts to content.

width: auto | <length> | <percentage> | min-content | max-content | fit-content

Common values

Value Effect
auto Default; browser calculates from content and context
100% Fills the containing block
min-content Shrinks to the smallest possible content size
max-content Grows to the largest content size without wrapping
fit-content Shrinks to content but capped by container

Examples

.container { width: 100%; max-width: 1200px; }

Fluid container with a cap

.pill { width: fit-content; padding: .25rem .75rem; }

Chip sized to its label

img { width: 100%; height: auto; }

Fluid responsive images

Gotcha

Percentage widths resolve against the containing block's width; that block must itself have a defined width for the percentage to work.

Related properties

← All CSS properties · CSS selectors · Grid vs Flexbox