Sub topic 6
Constraining size with min- and max- utilities
This chapter assumes you know the w-* and h-* utilities, the spacing scale, and the CSS properties min-width, max-width, min-height and max-height. Setting a size says what you want. A min or max utility sets a limit that the browser enforces when the container or content would push the element past it.
The utilities and what they map to
| Class | CSS |
|---|---|
| `max-w-md` | `max-width: 28rem` |
| `max-w-prose` | `max-width: 65ch` |
| `max-w-full` | `max-width: 100%` |
| `max-w-none` | `max-width: none` |
| `min-w-0` | `min-width: 0px` |
| `min-w-full` | `min-width: 100%` |
| `min-h-screen` | `min-height: 100vh` |
| `max-h-64` | `max-height: 16rem` |
The max-w-* named sizes run from max-w-xs up through max-w-7xl. The min-* and max-* height and width utilities also accept spacing-scale numbers, such as max-h-64, and fractions.
Fluid up to a limit
A fixed width is rigid on small screens. A percentage width keeps growing on large ones. Combine the two:
<div class="w-1/2 max-w-md">Half of the parent, but never wider than md</div>The element is 50% of its parent on narrow screens. Once 50% would exceed 28rem, it stops growing. The same pattern works with w-full max-w-md, which fills a phone screen and stays at 28rem on a desktop.
Add mx-auto to center a width-limited block:
<main class="mx-auto w-full max-w-3xl px-4">
Page content
</main>Which property wins
The browser resolves width in a fixed order. It starts with the requested value, applies max-width, then applies min-width. Because min-width is applied last, it wins when the two conflict.
Common uses
min-h-screenon a page wrapper makes it at least as tall as the viewport but lets it grow with content.h-screenwould fix the height and cause overflow.max-h-64 overflow-y-automakes a scrollable panel that only scrolls when its content is taller than 16rem.max-w-fullon an image keeps it from overflowing its container.
The flex truncation fix
A flex item has an automatic minimum width equal to its content. A long unbroken string therefore refuses to shrink, and truncate appears to do nothing. Setting min-w-0 removes that minimum:
<div class="flex gap-2">
<p class="min-w-0 truncate">quarterly-report-final-v2-revised-really-final.pdf</p>
<button class="shrink-0">Open</button>
</div>The same applies to grid items, and to min-h-0 when a flex column must scroll internally.
Common errors
- Using
max-w-*on an inline element.<span>ignores width limits. Addinline-blockorblock. - Expecting
max-w-mdto set the width. It only caps the width. A block element already fills its parent, but a flex item or floated element may shrink to its content. Addw-fullif you need it to fill. - Confusing
max-w-mdwith a breakpoint. In this utility,mdmeans 28rem. The responsivemd:prefix is a separate feature. - Fighting
min-wandmax-w. If both apply and conflict,min-widthwins. Check for a straymin-w-*when a cap seems to be ignored.
Exercise
A flex row holds a long file name and a button. The name overflows instead of showing an ellipsis, even though it has the truncate class. Which class do you add to the name element, and why?
Show answer
Add min-w-0. A flex item's default minimum width is its content width, so it cannot shrink below the text. min-w-0 sets min-width: 0px, which lets the item shrink so truncate can clip the text and add the ellipsis.
Exercise
Write classes for a wrapper that fills the width of its parent on small screens, never exceeds 48rem, is centered, and is at least as tall as the viewport.
Show answer
w-full max-w-3xl mx-auto min-h-screen. max-w-3xl is 48rem.