liveBook · Manning

Max-width property

As explained in Math for Web Design

max-width is a CSS constraint that limits the maximum width of an element or container.

A layout can need room to grow without becoming uncomfortably wide. The CSS `max-width` property sets an upper limit on an element’s width. It can cap a growing flex item, keep a liquid page readable on large screens, or limit the length of text lines. section 5.3 section 6.3.4 section 8.1.5

Cap flex growth

Suppose three items in a 600px flex container have `flex-grow` values of 1, 2, and 3. Their proposed widths are 150px, 200px, and 250px. Setting `max-width: 220px` on each item caps the third at 220px, leaving 30px unused. The browser distributes that space between the unconstrained items in a 1:2 ratio. The rendered widths become 160px, 220px, and 220px—exactly 600px in total. The cap is a hard stop for an item, not a reason to leave available space unused. section 5.3.2

Listing 5.9 Using max-width to limit growing
.container {
    display: flex;
    width: 600px;
}
.item {
    max-width: 220px;    #1
}
.item-1 {
    flex-grow: 1;
    flex-basis: 100px;
}
.item-2 {
    flex-grow: 2;
    flex-basis: 100px;
}
.item-3 {
    flex-grow: 3;
    flex-basis: 100px;
}

The CSS listing pairs a 220px cap with three different `flex-grow` values, making the redistribution example easy to check. section 5.3.2

For a liquid layout, first choose the widest outer container that still supports comfortable reading and navigation—for example, `body { max-width: 960px; }`. Then convert fixed child widths to percentages of their parent: a 640px article in a 960px parent becomes `(640 / 960) × 100 = 66.67%`; a 320px aside becomes 33.33%. The children can scale with the available width while the outer container has a ceiling. section 6.3.4

Text width can also be chosen alongside font size and line height. A full-width article can produce lines that are difficult to scan; one worked adjustment pairs a 20px font size and 1.41 line height with a 600px maximum width. For a horizontal flex axis, `max-width` limits growth; for a vertical main axis, the corresponding constraint is `max-height`. section 8.1.5 section 5.3

The constraint matters in CSS Grid too. An `auto` column respects an element’s `max-width` when determining how wide it may grow. A sidebar can therefore use an `auto` track with `min-width` and `max-width` to stay within chosen bounds as the viewport changes. section 4.4.2

How Math for Web Design covers it

How deep it goes
Works through flex-growth redistribution and percentage-based liquid-layout conversion, rather than a general account of width resolution.
Examples
Calculates capped widths for three flex items and pairs a 20px article font with a 600px maximum width.
What it assumes
Assumes familiarity with CSS widths and flex properties while demonstrating the relevant percentage and growth calculations.
What you can do after
Apply it via CSS to bound liquid layouts and keep article lines at a chosen readable width.
How it connects
Connects max-width to flex-grow, CSS Grid auto tracks, and min-width constraints.

Also explained in

Compare how 5 books explain max-width property →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Math for Web Design. The section links point to the parts of the book it draws on.