Grid Track Sizing: repeat(), minmax(), auto-fill, auto-fit and Intrinsic Sizes
The functions and keywords that size grid tracks: repeat() with auto-fill and auto-fit for responsive grids without media queries, minmax() for a range, min-content, max-content, auto and fit-content(), and how tracks are resolved.
kvi5hnuCode Sprout team member8 min readOn this page
Article 66 sized tracks with lengths and fr. Real layouts need more: columns that adapt to the container, as many as fit; tracks that are as wide as their content, but within limits; and short ways to write long track lists. That's what repeat(), minmax() and the content-based keywords are for. Together, they produce the most useful line in CSS grid:
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(15rem, 100%), 1fr)); gap: 1rem; }
By the end of this article, every part of it will make sense.
repeat()
repeat(count, tracks) repeats a track list:
grid-template-columns: repeat(4, 1fr); /* 1fr 1fr 1fr 1fr */
grid-template-columns: repeat(2, 10rem 1fr); /* 10rem 1fr 10rem 1fr */
grid-template-columns: 12rem repeat(3, 1fr) 12rem; /* mixed with other tracks */
The repeated part can include line names (article 68): repeat(3, [col] 1fr).
minmax()
minmax(min, max) makes a track at least min and at most max:
grid-template-columns: minmax(12rem, 20rem) 1fr; /* a sidebar between 12rem and 20rem */
grid-template-rows: minmax(4rem, auto); /* rows at least 4rem, taller if the content needs it */
- The max can be
fr, but the min can't:minmax(1fr, 2fr)is invalid. - If the min is larger than the max, the max is ignored.
minmax(0, 1fr)is the fix for unequal1frcolumns (article 66), since1fralone isminmax(auto, 1fr).
auto-fill and auto-fit: as many columns as fit
Instead of a number, repeat() accepts auto-fill or auto-fit: the browser creates as many tracks as fit in the container. With a minmax() size, that gives a responsive grid with no media queries:
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 20px; }
How it works, tested in Chrome with a 700px container:
- The browser fits as many 150px minimum columns as possible, counting gaps: with a 20px gap, four columns need 4 × 150 + 3 × 20 = 660px, and five would need 830px. So there are 4 columns.
- The
1frmax then shares the remaining width: each column is (700 − 60) / 4 = 160px. - When the container is resized, the number of columns changes at each threshold.
The difference between the two keywords only shows when there are fewer items than columns:

auto-fillkeeps the empty tracks. Tested in Chrome: two items in a 700px grid with no gap get four 175px columns, two of them empty.auto-fitcollapses empty tracks to 0, so the items grow to fill the row: the same two items are 350px each.

The collapsed tracks still exist in auto-fit, which is why lines 4 and 5 can still be used for placement; they just have no width.
Use auto-fill when items should keep a consistent size, like a gallery where a few results shouldn't become huge. Use auto-fit when a handful of items should fill the space, like a row of two or three feature boxes.
Not wider than the container
minmax(150px, 1fr) has a problem on very narrow screens: if the container is narrower than 150px, the one column is still 150px and overflows. Tested in Chrome: in a 100px container, the column is 150px.
min(150px, 100%) as the minimum fixes it: the minimum is 150px, or the container's width if that's smaller. In the 100px container, the column is 100px.

The overflow on the left is the kind that causes a horizontal scrollbar on a phone.
.cards { grid-template-columns: repeat(auto-fill, minmax(min(15rem, 100%), 1fr)); }
That's the line from the start of this article: as many columns as fit, each at least 15rem or the full width, sharing the extra space.
Rules for auto repetition
- Only one
auto-fillorauto-fitrepeat is allowed per track list, and its tracks need a fixed size, or a fixed minimum or maximum inminmax(), because the browser uses it to count how many fit. Tested in Chrome:repeat(auto-fill, 1fr)andrepeat(auto-fill, minmax(min-content, 1fr))are invalid, as is a list with two auto repeats;repeat(auto-fill, minmax(10rem, 1fr))andrepeat(auto-fill, 10rem)are fine. - Fixed tracks can be added around it:
grid-template-columns: 10rem repeat(auto-fill, minmax(8rem, 1fr)). - With a fixed size and no
fr, the tracks don't stretch:repeat(auto-fill, 100px)in a 700px container gives seven 100px columns, tested in Chrome, andjustify-content(article 70) can center them.
body { font: 0.8rem system-ui, sans-serif; margin: 1rem; }
label { margin-inline-end: 0.75rem; }
p { margin: 0.75rem 0 0.25rem; font-weight: 600; color: #8a3b12; }
.grid {
display: grid; gap: 0.4rem;
padding: 0.4rem; border: 2px dashed #8a3b12; border-radius: 8px;
}
.fill { grid-template-columns: repeat(auto-fill, minmax(min(3.5rem, 100%), 1fr)); }
.fit { grid-template-columns: repeat(auto-fit, minmax(min(3.5rem, 100%), 1fr)); }
.grid div { padding: 0.6rem 0.4rem; text-align: center; background: #f4efe6; border: 1px solid #8a3b12; border-radius: 6px; }
With a few items, auto-fill leaves empty columns at the end, while auto-fit stretches the items across the row. Once there are enough items to fill a row, the two are identical.
Content-based sizes
These keywords size a track from the content of its items:
| Size | Track width |
|---|---|
min-content | the narrowest the content can be: its longest word or widest image |
max-content | the content on one line, without wrapping |
auto | between min-content and max-content as a track size, and stretches to fill any space still left after the fr tracks |
fit-content(limit) | like auto, but no wider than limit: min(max-content, max(min-content, limit)) |
Tested in Chrome in a 700px grid with min-content max-content fit-content(120px) fit-content(120px) auto:
| Track | Content | Width |
|---|---|---|
min-content | "one two three" | 32px, the width of "three" |
max-content | "one two three" | 87.1px, on one line |
fit-content(120px) | "short" | 32px, its content |
fit-content(120px) | a longer sentence | 120px, the limit, and the text wraps |
auto | "x" | 429px: it takes all the leftover space |

The two fit-content(120px) columns behave differently because only the second has content wider than the limit.
.form { display: grid; grid-template-columns: max-content 1fr; gap: 0.5rem 1rem; } /* labels as wide as the longest */
.toolbar { display: grid; grid-template-columns: auto 1fr auto; } /* fixed ends, flexible middle */
.sidebar { grid-template-columns: fit-content(18rem) 1fr; } /* sized to content, up to 18rem */
How tracks are resolved
The browser sizes tracks in a fixed order, which explains most surprises:
- Fixed tracks (lengths, percentages) get their size.
- Content-based tracks (
auto,min-content,max-content,fit-content()) are sized from the items in them, including items spanning several tracks. - Tracks with a fixed or content-based maximum, like
minmax(100px, 300px)orfit-content(), grow toward that maximum if there's room. Tested in Chrome:minmax(100px, 300px) 1frin 600px gives 300px and 300px. frtracks share the space that's left, never going below their minimum (auto, unless you setminmax()).- If space is still left,
autotracks stretch to fill it, as long asjustify-contentoralign-contentis the default (normalorstretch). Usuallyfrtracks have taken everything, but not when their values add up to less than 1:auto 0.25frin 600px gives 452px and 148px.
So auto and fr compete differently: in auto 1fr, the auto track only gets its content's size, and 1fr takes the rest; in auto auto, both stretch.
Common patterns
/* responsive cards: as many as fit, at least 16rem */
.cards { grid-template-columns: repeat(auto-fill, minmax(min(16rem, 100%), 1fr)); }
/* a page with a centered, readable content column and full-width breakouts */
.article { grid-template-columns: 1fr min(65ch, 100% - 2rem) 1fr; }
.article > * { grid-column: 2; }
.article > .wide { grid-column: 1 / -1; }
/* a form with labels sized to the longest one */
.fields { grid-template-columns: max-content minmax(0, 1fr); }
The article pattern uses min() to keep the content column readable, with equal 1fr margins on each side, and lets selected elements span all three columns (article 68 covers placement).
What's current
repeat(),minmax(),auto-fill,auto-fit,min-content,max-contentandfit-content()work in all browsers.min(),max()andclamp()inside track sizes work in all browsers.- Container queries (article 78) are an alternative when the number of columns should follow explicit breakpoints.
Common questions
What's the difference between auto-fill and auto-fit?
Both create as many tracks as fit. auto-fill keeps empty tracks; auto-fit collapses them, so the items stretch.
Why is repeat(auto-fill, 1fr) invalid?
The browser needs a minimum size to count how many tracks fit. Use minmax(10rem, 1fr).
Why does my grid overflow on small phones?
The minimum in minmax() is wider than the screen. Use minmax(min(15rem, 100%), 1fr).
When should I use auto instead of 1fr?
When a track should be sized by its content, with any leftover going to fr tracks. In auto 1fr, the first track fits its content and the second takes the rest.
Summary
repeat(n, tracks)repeats tracks;repeat(auto-fill | auto-fit, size)creates as many as fit.minmax(min, max)sets a range; the min can't befr.auto-fillkeeps empty tracks,auto-fitcollapses them.minmax(min(15rem, 100%), 1fr)gives responsive columns that never overflow.min-content,max-content,autoandfit-content()size tracks from their content.- Tracks are resolved in order: fixed, content-based, growth up to a maximum, then
fr;autotracks stretch into whatever space is still left.
Practice questions
In a 1000px grid with gap: 20px, how many columns are there, and how wide is each?
.g { grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); }
Answer
Four. Five would need 5 × 200 + 4 × 20 = 1080px; four need 860px. Each is (1000 − 60) / 4 = 235px.
The same grid holds only two items. How wide are they with auto-fill, and with auto-fit?
Answer
With auto-fill, 235px each, with two empty columns after them. With auto-fit, the empty columns collapse, so the two items share 1000 − 20 = 980px: 490px each.
Write a track list for a toolbar with a logo and an account menu at their natural widths, and a search field taking the remaining space between them.
Answer
.toolbar { display: grid; grid-template-columns: auto 1fr auto; }
What width does each track get, in a 600px container?
.g { grid-template-columns: 100px fit-content(150px) 1fr; }
The second item is a sentence that would be 300px on one line.
Answer
100px, then 150px (the sentence's max-content of 300px is capped by the limit, and it wraps), then 1fr takes the remaining 350px.
Previous: Grid Basics: Grid Containers, Tracks, Lines, Cells and the fr Unit
Next: Placing Grid Items: Line Numbers, Spans, Named Lines and grid-template-areas

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.