Sub topic 6
Constraining size with min- and max- utilities
Fixed widths and heights break easily. A w-96 card overflows a 320px phone screen, and a h-40 box clips its text when the text grows. The min-* and max-* utilities set limits and let the browser choose a size inside them.
This chapter assumes you know the CSS box model and the width, min-width, and max-width properties. It also assumes you have read the previous chapter on width and height utilities, including w-full, w-1/2, and size-*. Examples work in Tailwind v3.4 or later. Where v4 differs, the text says so.
How the utilities map to CSS
Each utility sets one CSS property.
| Utility family | CSS property | Example | Generated declaration |
|---|---|---|---|
| `min-w-*` | `min-width` | `min-w-0` | `min-width: 0px` |
| `max-w-*` | `max-width` | `max-w-md` | `max-width: 28rem` |
| `min-h-*` | `min-height` | `min-h-screen` | `min-height: 100vh` |
| `max-h-*` | `max-height` | `max-h-64` | `max-height: 16rem` |
The browser first computes the element's width or height. It then clamps the result to the minimum and maximum. If min-width and max-width conflict, min-width wins. This matters later, in the gotchas.
Max width: the most common constraint
max-w-* is the utility you will use most. Pair it with w-full so the element fills its parent on small screens and stops growing on large ones.
<div class="w-full max-w-md">
Fills the parent up to 28rem, then stops growing.
</div>On a 360px screen this box is 360px wide, because the parent is smaller than max-w-md. On a 1200px screen it is 28rem (448px at the default root font size).
A block-level div already fills its parent, so w-full is often redundant there. It becomes necessary when the element is a flex item, a grid item, or an inline-block, where the default width is based on content.
The max-w scale
max-w-* has its own named scale in addition to the spacing scale.
| Class | Value |
|---|---|
| `max-w-xs` | 20rem |
| `max-w-sm` | 24rem |
| `max-w-md` | 28rem |
| `max-w-lg` | 32rem |
| `max-w-xl` | 36rem |
| `max-w-2xl` | 42rem |
| `max-w-3xl` | 48rem |
| `max-w-4xl` | 56rem |
| `max-w-5xl` | 64rem |
| `max-w-6xl` | 72rem |
| `max-w-7xl` | 80rem |
| `max-w-prose` | 65ch |
The xs to 7xl names are about the size of a container, not the screen breakpoints. max-w-md is 28rem, while the md breakpoint is 768px. Do not assume the two are related.
Other keyword values:
max-w-fullismax-width: 100%.max-w-noneremoves any limit. Use it to cancel amax-w-*set at a smaller breakpoint.max-w-min,max-w-max, andmax-w-fituse intrinsic sizes (min-content,max-content,fit-content).- In v3,
max-w-screen-sm,max-w-screen-md, and so on use your configured breakpoint widths. These are the container-style values that match the screen breakpoints, unlikemax-w-md.
The max-w-prose utility sets a width in ch units, roughly the width of one "0" character in the current font. 65ch keeps most body text at a comfortable 60 to 75 characters per line.
Centering a constrained block
A width-limited block hugs the left edge unless you center it. Add mx-auto:
<main class="mx-auto w-full max-w-3xl px-4">
<h1 class="text-3xl font-bold">Page title</h1>
<p class="mt-4">The content column is centered and never wider than 48rem.</p>
</main>The px-4 keeps text away from the screen edge when the viewport is narrower than max-w-3xl. Without it, the text touches the edges on phones.
Min width
min-w-* stops an element from shrinking below a size. Common values:
min-w-0setsmin-width: 0px.min-w-fullsetsmin-width: 100%.min-w-min,min-w-max, andmin-w-fituse intrinsic sizes.- From Tailwind v3.4, spacing scale values such as
min-w-24also work.
A button that should never collapse below a readable size is a typical case:
<button class="min-w-24 rounded bg-blue-600 px-4 py-2 text-white">
OK
</button>The short label "OK" would make a tiny button. min-w-24 keeps it at least 6rem wide, and a longer label can still make it wider.
The min-w-0 flex fix
min-w-0 is mostly used to fix a flexbox problem. A flex item has min-width: auto by default. That means it will not shrink below the width of its content, so long unbroken text can push a row wider than its container, and truncate appears not to work.
<div class="flex w-64 items-center gap-2">
<img src="/avatar.png" alt="" class="size-10 shrink-0 rounded-full">
<p class="min-w-0 truncate">A very long file name that should be cut off.pdf</p>
</div>Remove min-w-0 from the paragraph and the text stays at its full length, overflowing the 16rem container. With it, the paragraph can shrink and truncate adds the ellipsis. Grid children behave the same way, so min-w-0 is also the fix for grid items that overflow.
Min and max height
Height limits work like width limits, with different typical uses.
min-h-screenismin-height: 100vh. Use it so a page fills the viewport but can still grow with its content.min-h-dvh,min-h-svh, andmin-h-lvh(Tailwind v3.4 or later) use dynamic, small, and large viewport units. On mobile browsers,100vhcan be taller than the visible area because the address bar takes space, andmin-h-dvhtracks the visible area.max-h-*limits height, and is usually combined with scrolling.
<div class="min-h-dvh bg-gray-50">
<div class="mx-auto max-w-sm p-4">
<ul class="max-h-48 overflow-y-auto rounded border">
<li class="px-3 py-2">Item 1</li>
<li class="px-3 py-2">Item 2</li>
<li class="px-3 py-2">Item 3</li>
<li class="px-3 py-2">Item 4</li>
<li class="px-3 py-2">Item 5</li>
<li class="px-3 py-2">Item 6</li>
</ul>
</div>
</div>The list is at most 12rem tall. When its items need more room, the list scrolls instead of growing.
Prefer min-h-* to a fixed h-* for containers that hold text. A fixed height clips content when users enlarge their font size or translate the page into a longer language.
Combining min and max with responsive variants
Because these are ordinary utilities, you can apply them at breakpoints. A common pattern uses a narrow column on phones and a wider one on large screens:
<section class="mx-auto max-w-md md:max-w-2xl lg:max-w-4xl">
<p>The limit grows at each breakpoint.</p>
</section>Utilities without a prefix apply at every width, and md: and lg: override them from those breakpoints up. To remove a limit at a larger breakpoint, use lg:max-w-none.
Common errors
- Setting
w-96where you wantedmax-w-96.w-96is always 24rem and overflows small screens. Usew-full max-w-smor similar. - Using
max-w-mdand expecting themdbreakpoint.md:is a variant prefix (a breakpoint).max-w-mdis a size. They are different features. - Forgetting
mx-auto. The block is limited but sits on the left. - Conflicting limits. If you write
min-w-96 max-w-xs, the minimum wins, so the element is 24rem wide. Themax-w-xsis silently ignored. max-w-proseon a tiny or huge font. Becausechdepends on the font, a differentfont-sizeon the element changes the width. Set the font size on the same element asmax-w-prose, or on an ancestor, to avoid surprises.- Missing
min-w-0in flex or grid. Truncation fails and layouts overflow, as shown above. - Using
min-w-24on Tailwind v3.3 or earlier. The scale values formin-w-*andmin-h-*arrived in v3.4. On older versions the class generates nothing. Check the version, or use an arbitrary value such asmin-w-[6rem].
Choosing between the utilities
- Use
max-w-*for content columns, cards, and form fields that should stop growing. - Use
min-w-*for controls that must not collapse, andmin-w-0to let flex and grid children shrink. - Use
min-h-*for page shells and sections that should be at least a certain height. - Use
max-h-*withoverflow-*for scrollable panels. - Use
w-*andh-*alone only when the size really is fixed, such as an icon.
Exercise
A flex row has an avatar and a paragraph with a very long file name. You add truncate to the paragraph, but the row still overflows its container and no ellipsis appears. What single utility on the paragraph fixes this, and why?
Show answer
Add min-w-0. A flex item has min-width: auto by default, so it will not shrink below its content width. min-w-0 sets min-width: 0px, which lets the paragraph shrink so truncate can cut the text and add an ellipsis.
Exercise
Write the classes for a block that fills its parent on small screens, never exceeds 42rem, is centered, and is at least 10rem tall.
Show answer
mx-auto w-full max-w-2xl min-h-40. The max-w-2xl value is 42rem, mx-auto centers the block, and min-h-40 is 10rem on the spacing scale (40 × 0.25rem).