CSS Intrinsic Sizing: min-content, max-content, fit-content
CSS intrinsic sizing keywords size a box from its content instead of its container. How min-content, max-content and fit-content work, with practical layouts.
Intrinsic sizing in CSS means letting a box’s size come from its content rather than from its container. The three keywords that expose it are min-content (the narrowest the content can get without overflowing), max-content (the widest the content wants to be with no wrapping), and fit-content (max-content, but never wider than the space available). Once you know what each one measures, a lot of layouts that used to need JavaScript or magic numbers become one line of CSS.
Extrinsic vs intrinsic sizing
Most CSS you write is extrinsic: the size is imposed from outside. width: 300px, width: 50%, and the default width: auto on a block element (fill the container) all ignore what is inside the box.
Intrinsic sizing asks the content. The browser already computes these measurements internally — it needs them for table columns, floats, and flex and grid items — and the keywords simply let you use them directly on width, height, min-width, max-width, flex-basis, and grid track definitions.
What does min-content measure?
min-content is the smallest size the content can take without overflowing. For text, that means wrapping at every opportunity, so the box becomes as wide as the longest unbreakable word. For an image, it is the image’s width.
.caption {
width: min-content;
}
A classic use is a figure whose caption should never be wider than its image:
figure {
width: min-content;
}
figure img {
max-width: none;
}
The figure shrinks to the image’s width (the widest unbreakable thing inside it), and the caption text wraps to fit underneath. No fixed widths, no measuring in script.
The danger is long unbroken strings — URLs, hashes, long compound words. They define the min-content width, so a single long token can push the box wider than you expected. Pair it with overflow-wrap: anywhere if the content is user-generated; our guide to CSS overflow covers the related wrapping and clipping properties.
What does max-content measure?
max-content is the size the content would take if it never wrapped. For a paragraph, that is the width of the entire text on one line.
.tag {
width: max-content;
}
This is useful for labels, badges, tooltips, and buttons that should hug their text exactly. It is risky for anything with long content: a sentence at max-content will happily overflow a narrow screen, because the keyword ignores the container entirely. If you need a single line that truncates instead, combine white-space: nowrap with the techniques in text-overflow and ellipsis.
How fit-content combines the two
fit-content is the one you will use most. It behaves like max-content until the content would be wider than the available space, at which point it behaves like auto and wraps. Formally it resolves to min(max-content, max(min-content, available space)).
.callout {
width: fit-content;
margin-inline: auto;
}
That snippet produces a box that hugs short content, wraps long content at the container edge, and centers itself — something that once required display: inline-block on the child plus text-align: center on the parent.
There is also a function form, fit-content(<length>), which works in grid track sizing. grid-template-columns: fit-content(20rem) 1fr gives the first column its max-content width but caps it at 20rem.
The three keywords side by side
| Keyword | What it measures | Wraps text? | Respects container? | Typical use |
|---|---|---|---|---|
min-content | Narrowest width without overflow | At every opportunity | No | Figures, tight captions |
max-content | Width with no wrapping at all | Never | No | Badges, tags, labels |
fit-content | max-content, capped at available space | Only when needed | Yes | Callouts, buttons, centered boxes |
auto (block) | Fill the container | When it hits the edge | Yes | Normal flow content |
Intrinsic sizing in grid and flexbox
Grid is where these keywords earn their keep. Track sizes accept them directly, and they combine with minmax():
.layout {
display: grid;
grid-template-columns: max-content 1fr;
}
That two-column form layout gives the label column exactly the width of its longest label and hands the rest to the inputs. The pattern minmax(min-content, 1fr) is another common one: a flexible track that refuses to shrink below its content. If minmax() and repeat() are new to you, see CSS grid repeat() and minmax().
Flexbox uses intrinsic sizes implicitly. A flex item’s automatic minimum size is its min-content size, which is why a flex child containing a long word or a wide table refuses to shrink and overflows the row. The usual fix is min-width: 0 on the item, which removes that floor. Understanding this interaction makes flex-grow, flex-shrink and flex-basis behave far more predictably. You can also set flex-basis: max-content to start an item at its natural width before growing or shrinking.
Heights, and where intrinsic sizing gets weird
The keywords work on height too, but block heights are already intrinsic by default — height: auto on a block is effectively its content height — so you rarely need them there. One place they matter is animation: you cannot transition from height: 0 to height: auto in older CSS, and intrinsic keywords historically could not be interpolated either. Newer CSS adds interpolate-size: allow-keywords and the calc-size() function to make that possible; support varies, so treat it as progressive enhancement.
Another subtlety: percentages inside an intrinsically sized box can be circular. A child at width: 50% inside a parent at width: max-content creates a dependency loop. Browsers resolve this with defined rules, but the results are rarely what you intended. Keep children of intrinsically sized boxes in intrinsic or fixed units.
Practical patterns
A few layouts that become trivial:
- Shrink-wrapped centered card:
width: fit-content; margin-inline: auto; - Label/value lists:
grid-template-columns: max-content 1fr; - Image with matching caption:
figure { width: min-content; } - Sidebar that sizes to its nav but caps out:
grid-template-columns: fit-content(16rem) 1fr; - Buttons that never squash their text:
min-width: max-content;on the button
For truly fluid sizing based on the viewport rather than content, these keywords combine well with clamp() for fluid typography, and for form controls specifically, field-sizing lets inputs and textareas grow with what the user types.
The takeaway
Intrinsic sizing lets content decide how big a box should be. min-content is the tightest fit, max-content is the no-wrap width, and fit-content is max-content that politely stops at the container edge. Reach for fit-content by default when you want a box to hug its content, use max-content in grid tracks for label columns, and remember that flexbox’s hidden min-content floor is the reason min-width: 0 fixes so many overflow bugs.
Tagged
Keep reading
Takina · · 5 min read CSS Sibling Combinators: + and ~ Selectors Explained
CSS sibling combinators select elements by their neighbors: + targets the next sibling, ~ targets all later siblings. Syntax, examples, and limits.
Takina · · 5 min read CSS text-overflow Ellipsis: How to Truncate Text
CSS text-overflow: ellipsis truncates overflowing text with "…", but only with overflow and white-space set. The recipe, flexbox fixes, and accessibility.
Takina · · 4 min read HTML details and summary Elements Explained
The HTML details and summary elements build native disclosure widgets and accordions with no JavaScript. How they work, how to style them, and pitfalls.