Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Box Model and Sizing

The max-width property

Reference for the CSS max-width property: cap an element's width, build fluid containers and readable text columns, make images responsive, and center blocks.

The max-width property sets an upper limit for an element's width. If width (or the automatic width) would be larger, the box is narrowed to max-width. If the container is narrower than the limit, the element simply shrinks with it, so no horizontal scrollbar appears.

It is the foundation of fluid layouts: a container that fills small screens but stops growing on large ones.

CSS
.container { max-width: 60rem; margin-inline: auto; }
img { max-width: 100%; height: auto; }

Try it

A container that stops growing
Readable lines and centering

Syntax

CSS
max-width: none
         | <length-percentage [0,โˆž]>
         | min-content | max-content | fit-content | fit-content(<length-percentage>)
         | stretch;
  • Initial value: none
  • Applies to: all elements except non-replaced inline elements, table rows and row groups
  • Inherited: no
  • Animatable: yes, as a length or percentage
  • Computed value: a percentage or none as specified, otherwise an absolute length

Values

Value Description
none No upper limit.
<length> A fixed cap such as 800px, 60rem or 65ch.
<percentage> A percentage of the containing block's width.
min-content, max-content, fit-content Content-based caps.
stretch Fill the available width. Newer, with limited support.

Usage notes

  • Use it instead of width for containers. A fixed width forces scrolling on narrow screens; max-width adapts.
  • Centering needs auto margins. margin-inline: auto (or margin: 0 auto) centers a block only when its width is less than its container.
  • Responsive images. max-width: 100%; height: auto keeps an image inside its column without distortion.
  • Use ch for text. A width of 60 to 75 ch gives comfortable reading lines in most fonts.
  • min-width wins over max-width when both are present and conflict.
  • Percentages in shrink-to-fit parents. Resolving max-width: 100% inside an element with intrinsic width can cause cyclic sizing; images in table cells and floats are the typical cases.

Browser support

Supported in every browser.