Skip to content
Tutorials Center
My learning

Topics

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.

CSS
.card { width: 20rem; }
.half { width: 50%; }

Try it

Different kinds of width

Syntax

CSS
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 auto do not interpolate by default)
  • Computed value: a percentage or auto as 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: auto already does this and respects margins. width: 100% plus padding and borders overflows the parent unless box-sizing: border-box is set.
  • Prefer max-width for fluid layouts. width: 100%; max-width: 60rem shrinks on small screens and stops on large ones; a fixed width: 60rem forces horizontal scrolling on narrow screens.
  • Inline elements ignore it. A <span> or <a> needs display: inline-block or another type before width applies.
  • Replaced elements such as images use their intrinsic ratio: set only one of width or height and 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 ch and rem for text-based widths so a box scales with the font size, for example max-width: 65ch for 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.