Skip to main content

CSS Layout

Intrinsic Sizing

Written by Updated Intrinsic Sizing exercises

Use min-content, max-content, fit-content(), and flexible track sizing to let content participate in layout decisions. It covers min-content, max-content, fit-content(), minmax(), flexible Grid tracks, and content-driven layouts that need fewer breakpoints.

Sometimes you do not want to guess a width. You want the element to be exactly as wide as its content needs, or as narrow as it can get. Intrinsic sizing lets the content decide.

"Intrinsic" means the size comes from the content: a button's label, a line of text, an image's own dimensions. "Extrinsic" means you set it from outside, like width: 300px or width: 50%. Mixing the two well is what makes layouts work with real content. This builds on CSS Width and Height and CSS Grid Columns.

The three keywords

  • min-content is the narrowest the content can get without spilling out. For text, that is usually the width of the longest word.
  • max-content is how wide the content would be if it never wrapped. A sentence stays on one line.
  • fit-content is "as wide as the content, but never wider than the space available". It is usually the one you want.

A toolbar and a popover sized by their content

css

.toolbar {
  display: grid;
  grid-template-columns: max-content minmax(0, 1fr) max-content;
  gap: 0.75rem;
}

.popover {
  width: fit-content;
  max-width: 28rem;
}

In the toolbar, the buttons on each side are exactly as wide as their labels, and the middle column takes the rest. The popover is only as wide as its text, but never wider than 28rem.

The minmax(0, 1fr) trick

minmax(0, 1fr) permits a track to shrink below its content-based automatic minimum. Use it when long content pushes a grid beyond its container.

Example

css

.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr));
}

Normally a 1fr grid column will not shrink below its longest content, so one long URL can push the whole grid wider than the screen. Writing minmax(0, 1fr) allows the column to shrink. In the gallery above, min(100%, 16rem) does the same job for a single narrow column on a phone.

Animating to auto

For years you could not animate height: auto. interpolate-size: allow-keywords finally allows transitions between a length and a keyword like auto:

Example

css

@supports (interpolate-size: allow-keywords) {
  :root { interpolate-size: allow-keywords; }

  .details {
    block-size: 0;
    overflow: clip;
    transition: block-size 250ms ease;
  }

  .details[open] { block-size: auto; }
}

Only Chrome and Edge support it so far. In other browsers the panel just opens without the animation, which is fine.

Intrinsic Sizing Values

ValueMeaningExample
min-contentThe narrowest the content can get without overflowing.width: min-content
max-contentThe width of the content with no wrapping.width: max-content
fit-contentLike max-content, but never wider than the available space.width: fit-content
fit-content()In grid tracks, max-content capped at the size you give.grid-template-columns: fit-content(20rem) 1fr
autoLet the layout decide.width: auto

Complete Example

Complete runnable example

html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Intrinsic Sizing example</title>
  <style>
    * { box-sizing: border-box; }
    body {
      margin: 0;
      padding: 2rem;
      font-family: system-ui, sans-serif;
      line-height: 1.5;
    }
    main { max-width: 70rem; margin-inline: auto; }
    article, section, aside, form, .card, .panel {
      padding: 1rem;
      border: 1px solid #cbd5e1;
      border-radius: 0.6rem;
    }

.toolbar {
  display: grid;
  grid-template-columns: max-content minmax(0, 1fr) max-content;
  gap: 0.75rem;
}

.popover {
  width: fit-content(28rem);
}

.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr));
}

@supports (interpolate-size: allow-keywords) {
  :root { interpolate-size: allow-keywords; }

  .details {
    block-size: 0;
    overflow: clip;
    transition: block-size 250ms ease;
  }

  .details[open] { block-size: auto; }
}
  </style>
</head>
<body>
  <main><div class="toolbar"><strong>Brand</strong><input aria-label="Search"><button>Search</button></div><p class="popover">This box stops growing at a readable width.</p><section class="gallery"><article>Short</article><article>Longer card content</article><article>Third card</article></section></main>
</body>
</html>

Browser Support

Feature
Chrome
Edge
Firefox
Safari
width: min-content46796611

Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8. A partial-support note is included where it changes how the example behaves.

Try it with awkward content

Test with the longest label and the shortest one you can imagine. Intrinsic sizing removes many guessed breakpoints, but only if it has limits: pair it with max-width, minmax() or min() so content can shape the layout without breaking it.

Intrinsic Sizing exercises

As narrow as possible

Make the box exactly as wide as its longest word, with width min-content.

The HTML your code works on
<div class="box">Some words here</div>

Result

Check your answer to see it here.