How do fr, minmax() and auto-fit work in CSS Grid?
IntermediateCSS interview questionBy UIRound, updated 17 November 2026
Short answer
In CSS Grid, fr is a share of the space left after fixed and content-sized tracks are placed. minmax(min, max) gives a track a size range. repeat(auto-fit, minmax(200px, 1fr)) creates as many columns of at least 200px as fit, then stretches them to fill the row, giving a responsive grid with no media queries. auto-fill keeps empty columns; auto-fit collapses them.
The fr unit
fr stands for fraction of the free space. The browser first sizes every track that is not flexible (lengths, percentages, auto, min-content), subtracts those and the gaps from the container, then shares what is left by the fr values.
.layout {
display: grid;
width: 820px;
gap: 10px;
grid-template-columns: 200px 1fr 2fr;
}
/* Free space: 820 - 200 - 2 gaps of 10 = 600px
1fr = 600 / 3 = 200px, 2fr = 400px */Two details that interviews like:
- If the
frvalues add up to less than 1, they take only that fraction of the free space.grid-template-columns: 0.5frmakes one column half the free space, not all of it. 1fris reallyminmax(auto, 1fr). Theautominimum means a flexible track never gets narrower than the minimum content size of its items. That causes the most common Grid bug, covered below.
fr vs percentages
% |
fr |
|
|---|---|---|
| Relative to | The grid container's size | The free space left after other tracks and gaps |
Accounts for gap |
No, so repeat(4, 25%) plus a gap overflows |
Yes |
| Can shrink below content | Yes, content may overflow the track | No, by default it stops at the content minimum |
| Works in auto-sized containers | Can resolve to auto if the container size is unknown |
Yes, sized from the content |
Use fr for "share the rest" and fixed lengths for tracks that must not change. Percentages are rarely needed in Grid.
minmax(min, max)
minmax() sets a track's smallest and largest size: minmax(150px, 300px) grows with the container but stays between those bounds. The max can be flexible (minmax(200px, 1fr)), the min cannot be an fr value. If the max is smaller than the min, the max is ignored.
Content keywords are allowed too: minmax(min-content, 20rem) never squashes a word, and minmax(0, 1fr) lets a column shrink to nothing.
The responsive card grid explained
.cards {
display: grid;
gap: 1rem;
grid-template-columns: repeat(auto-fit, minmax(min(200px, 100%), 1fr));
}Read it left to right:
repeat(auto-fit, ...)asks the browser to work out how many columns fit, instead of a fixed number.- The minimum,
200px, is what it uses to count. On an 860px container with 16px gaps, 4 columns need 4 × 200 + 3 × 16 = 848px, so 4 fit and 5 would not. - The maximum,
1fr, then shares the extra 12px, so the 4 columns fill the row exactly. min(200px, 100%)handles containers narrower than 200px. Without it, the one column would stay 200px wide and overflow a 180px space; with it, the minimum shrinks to the container.
As the container shrinks, columns drop from 4 to 3 to 2 to 1, each time at the point where the minimum no longer fits. No breakpoints needed, and because it reacts to the container rather than the viewport, it works in a sidebar or a main column alike.
Auto-repeat needs at least one fixed size to count with. repeat(auto-fit, 1fr) is invalid, because a flexible size alone gives no number of columns.
auto-fit vs auto-fill
Both create as many tracks as fit. They differ only when there are fewer items than tracks:
auto-fill |
auto-fit |
|
|---|---|---|
| Empty tracks | Kept, so they still take up room | Collapsed to 0px, with their gaps |
| Two items in a row that fits five | Two 200px-ish cards, empty space on the right | Two cards stretched to share the full row |
| Good for | Grids where a card should keep its size, like a product list that might have one result | Grids that should always fill the row |
With many items, both give the same result. Pick auto-fill if a single search result stretched across the whole page would look wrong.
The min-content pitfall and minmax(0, 1fr)
.page { display: grid; grid-template-columns: 1fr 1fr; }
/* A long URL, a <pre> block or a wide table in one column
makes that column wider than half, and the grid overflows. */Because 1fr means minmax(auto, 1fr), each column is at least as wide as its widest unbreakable content. Two 1fr columns are therefore only equal when their content allows it. The fix is to set the minimum yourself:
.page { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
/* Or: repeat(2, minmax(0, 1fr)) */Now the columns are always equal and the long content wraps, scrolls or overflows inside its own cell, where you can deal with it with overflow-wrap: anywhere or overflow-x: auto. Setting min-width: 0 on the grid item also works, because the auto minimum comes from the item's automatic minimum size. This is the same problem as flex items refusing to shrink, which Flexbox vs Grid: when to use which? also covers. For the box sizes involved, see What is the CSS box model?.
Practise it
Predict grid track sizes from fr, minmax(), auto-fill and auto-fit drills the arithmetic above, and Build a responsive card grid that fits as many columns as it can without media queries builds the pattern. Predict min-content, max-content and fit-content widths, Build a three-per-row image grid and Build a pricing table with subgrid cover the content-sizing edge cases.
Common follow-up questions
Why are my 1fr 1fr columns not equal?
1fr 1fr columns not equal?One column has content with a larger minimum width, such as a long word, an image with a fixed width or a <pre> block. 1fr cannot go below that minimum. Use minmax(0, 1fr) for truly equal columns.
When should I use auto-fill instead of auto-fit?
auto-fill instead of auto-fit?When items should keep a consistent width even if the row is mostly empty. With auto-fit, a single item stretches across the whole row; with auto-fill, it stays the width of one column.
Does fr work for rows?
fr work for rows?Yes, but rows only share free space when the grid has a definite height. In a container with height: auto there is no free space to share, so the browser sizes fr rows from their content instead. Rows that are all 1fr then come out equal, each as tall as the tallest row, which is a handy trick for equal-height rows (grid-auto-rows: 1fr). Give the container a height (for example min-height: 100dvh for a page shell) and 1fr rows fill it.
What is the difference between minmax(200px, 1fr) and minmax(auto, 1fr)?
minmax(200px, 1fr) and minmax(auto, 1fr)?The first never lets the track go below 200px, whatever the content. The second, which is what plain 1fr means, uses the content's minimum size as the floor, which can be smaller or larger than 200px.
Mistakes to avoid
Using
repeat(4, 25%)with agap, which overflows by the size of the gapsWriting
repeat(auto-fit, 1fr), which is invalidForgetting
min(200px, 100%), so the grid overflows on very narrow screensAssuming
1frcolumns are always equal; useminmax(0, 1fr)when they must beChoosing
auto-fitfor search results and getting one giant card when there is a single match
Sources
Practise it in code
Interviewers rarely stop at the definition. These questions open in an editor with tests.
- Predict grid track sizes from
fr,minmax(),auto-fillandauto-fitEasy20 min - Build a responsive card grid that fits as many columns as it can without media queriesEasy15 min
- Predict
min-content,max-contentandfit-contentwidths in labels, buttons and grid tracksEasy25 min - Build a three-per-row image grid that keeps every cell the same size whatever the image dimensionsEasy20 min
- Build a pricing table whose plan features and buttons line up across cards with subgridEasy20 min