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
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
noneas 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
widthfor containers. A fixedwidthforces scrolling on narrow screens;max-widthadapts. - Centering needs auto margins.
margin-inline: auto(ormargin: 0 auto) centers a block only when its width is less than its container. - Responsive images.
max-width: 100%; height: autokeeps an image inside its column without distortion. - Use
chfor text. A width of 60 to 75chgives comfortable reading lines in most fonts. min-widthwins overmax-widthwhen 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.
Related
min-widthโ lower limitwidthโ preferred widthmax-heightโ the vertical capmarginโ use auto margins to center- Lesson: Width and Height
- Lesson: Centering