Skip to content
tezvyn:

How do min-content, max-content, and fit-content work in CSS Grid?

Source: developer.mozilla.orgHardHow cards are made

How do min-content, max-content, and fit-content work in CSS Grid?

It tests intrinsic track sizing. A strong answer defines min-content as the smallest no-overflow width, max-content as widest no-wrap width, and fit-content as clamped max-content; then shows labels hugging longest word.

What's really being asked

This question probes your understanding of intrinsic sizing in CSS Grid and how the browser resolves track sizes based on content rather than explicit lengths. It reveals whether you know the difference between minimum and maximum intrinsic contributions, how the grid container distributes leftover space, and when to reach for content-based keywords instead of fixed or fractional units.

The full answer

First, define min-content as the smallest size a track can shrink to before its content overflows, which for text is usually the width of the longest word or unbreakable element. Second, define max-content as the largest size a track would need if its content could lay out on a single line without wrapping, representing the upper bound of the content's natural width. Third, explain fit-content as a value that behaves like max-content but is clamped by the available space in the container, effectively capping expansion so the track does not push the grid beyond its bounds; if space is tight it can still compress down to min-content. Fourth, apply these definitions to the specific declaration grid-template-columns: auto 1fr min-content by describing a layout where the first column sizes to its own content with auto, the middle column absorbs all free space with 1fr, and the final column shrinks to the intrinsic minimum of its content, such as a set of action buttons or labels that should stay compact without wrapping.

The mistakes people make

A red flag is treating auto and fit-content as interchangeable; auto distributes leftover space after content sizing and can grow well beyond max-content, whereas fit-content hard-caps growth at the container limit. Another mistake is claiming these keywords are exclusive to Grid when they also apply to Flexbox and other layout contexts, even though the question focuses on Grid. Some candidates also describe max-content as the largest possible viewport width rather than the content's own unwrapped width, or they suggest min-content equals zero, confusing it with min-width zero.

What usually comes next

An interviewer might ask how fit-content differs from the fit-content function with an argument, such as fit-content(200px), which lets you supply your own cap instead of using the container size. They might also ask what happens when multiple tracks use min-content and max-content simultaneously and the grid is constrained, which tests your understanding of the grid sizing algorithm and how the browser balances intrinsic contributions against fractional units.

A concrete example

Imagine a settings panel with three columns: a category label on the left, a fluid input field in the middle, and a Delete button on the right. Using grid-template-columns: auto 1fr min-content, the left column hugs the label text, the input stretches to fill remaining space, and the right column collapses to the exact width of the Delete button text plus padding because min-content prevents the button from wrapping or expanding. If the button label were longer, the column would grow only enough to hold that longest word, keeping the layout crisp and predictable.

Interview question

When a CSS Grid container lacks sufficient horizontal space, how does a fit-content track behave compared to a max-content track?

  • a.fit-content is clamped by the container's available space and can compress to min-content, while max-content demands the full unwrapped width even if it overflowsCorrect
  • b.fit-content distributes leftover free space like auto, while max-content only considers the longest unbreakable element
  • c.fit-content resolves to zero when space is tight, while max-content compresses to the width of the longest word
  • d.fit-content and max-content are identical unless the track contains fractional units
Why?

fit-content caps expansion at the container's available space but can still compress to min-content, whereas max-content always takes the content's full unwrapped width regardless of overflow. Option B is tempting because it confuses fit-content with auto, which distributes leftover space and can grow beyond max-content, while fit-content hard-caps growth.

Just read this? Test yourself on what you have been reading.

Read the original → developer.mozilla.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on css — each one lists the topics its interview covers.

See open roles