Grids that hold their shape

You will see why a 1fr column grows past its share when the content is long, how minmax(0, 1fr) stops it, and how auto-fit and auto-fill decide how many columns you get, with every track measured live in your own browser.

GuideLayout
01

Why 1fr grows

A 1fr column is shorthand for minmax(auto, 1fr). That auto minimum stops the column from getting narrower than the widest thing inside it that cannot break, such as a long word, a URL or a line of code. If you write minmax(0, 1fr) instead, the minimum becomes zero and the columns stay equal whatever they hold.

Both grids below hold the same three cards. The dashed line marks the edge of each grid. Type into the box or press Long word. The readout shows the column widths your browser actually used, read live from getComputedStyle. When content overflows too far to fit the panel, the demo zooms that preview out so you can see all of it. The pixel values in the readout are not scaled.

One long word, two grids Interactive
repeat(3, 1fr)
repeat(3, minmax(0, 1fr))
Measuring the tracks…

A short summary appears here after you change a control.

With the long German compound word in the first card, the 1fr grid gives most of its width to column one. In a narrow panel it also pushes past its own edge. The minmax(0, 1fr) grid keeps three equal columns, but the word now runs over its neighbor. Section 3 fixes that. Delete letters one at a time and watch the 1fr grid. Its columns become equal at the moment the first card's minimum width fits within one equal share.

So when minmax seems broken, check what you wrote. If the track says 1fr, it is not minmax(0, 1fr). It is minmax(auto, 1fr), and the auto minimum is doing what it is supposed to do.

</> Two ways to take the content minimum away.Show code
/* Before: a column can grow to fit its content */
.grid { grid-template-columns: repeat(3, 1fr); }

/* After: the minimum is zero, so every column gets an equal share */
.grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }

/* With 1fr tracks, you can also zero the minimum on the items */
.grid > * { min-width: 0; }
02

Watch the tracks resolve

The browser does not guess the column widths. It runs a short loop that the spec calls "find the size of an fr". The demo below runs the same loop in JavaScript. It uses the min-content width your browser measured for each card, which is the narrowest the card can get by wrapping at every allowed point. Then it checks its prediction against the widths your browser actually produced.

The fr loop, predicted and measured Interactive
minmax(0, 1fr)
Measuring…

A short summary appears here after you change a control.

Drag the width down slowly. At full width on a desktop, usually only the first track freezes; the readout tells you which ones froze in your browser. On a narrow screen, more than one may be frozen before you touch the slider. As the box shrinks, the share per fr gets smaller. When it drops below a card's minimum, that track freezes too. If every track freezes and their minimums no longer fit, the grid overflows its box. Now turn on minmax(0, 1fr). The loop finishes in one step at every width, because a minimum of zero is never larger than a share.

One level deeper. Track sizing starts by giving every track a base size. When a track has an auto minimum, its base size comes from each item's minimum contribution. In this demo, each card has min-width: auto and visible overflow, so that minimum is based on its min-content width. Only after that does the browser expand the fr tracks. It divides the available space by the total of the flex factors. If a track's base size is larger than its share, the browser treats that track as fixed and divides again without it. An fr value can never shrink a track below its base size, so a plain 1fr track cannot ignore a large content minimum.

03

Stop the spill, not just the growth

minmax(0, 1fr) stops the column from growing, but the long content is still there. In the first demo, the word in the minmax(0, 1fr) grid ran straight over its neighbor. A complete fix has two parts: keep the track at its share, then give the content a way to fit inside it.

Hold the track, then fit the content Interactive
minmax(0, 1fr)
break-word
anywhere
code scrolls
Measuring…

A short summary appears here after you change a control.

Start with every switch off and turn them on one at a time. With only minmax(0, 1fr) on, the tracks are equal. The readout shows how far the word and the code spill out of their cards. Add anywhere and the word wraps inside its card. Add code scrolls and any code that does not fit scrolls sideways inside its own card. You can scroll it by touch or trackpad, or focus it and use the arrow keys.

Now try the part that is easy to miss. Turn minmax(0, 1fr) off and leave anywhere and code scrolls on. The tracks are equal again with plain 1fr, as long as the remaining content minimums fit. Two separate rules explain why.

overflow-wrap: anywhere adds break points that the browser counts when it measures min-content width. overflow-wrap: break-word can break the word on screen, but those breaks do not count in that measurement. The min-content line in the readout is proof from your own browser. It measures a hidden copy of the word card under each value. By the spec, break-word reports room for the whole word, while anywhere drops to about one character plus the card's padding and border. Swap anywhere for break-word with plain 1fr and watch the first track take its large minimum back. The second rule is that a grid item which is a scroll container, such as one with overflow: auto or hidden, has an automatic minimum width of zero. That is why the scrolling <pre> stops pushing its track wider. (overflow: clip does not create a scroll container.)

</> Hold the track, then let the content fit.Show code
.grid {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}
.grid > * {
  overflow-wrap: anywhere; /* long words and URLs wrap */
}
.grid pre {
  overflow-x: auto;        /* code scrolls inside its card */
}
.grid img {
  max-width: 100%;         /* overflow-wrap does not resize images */
}

Use both. minmax(0, 1fr) protects the track from content you did not plan for, and the content rules deal with the spill. Each one covers a case the other misses. Images ignore overflow-wrap, a table with many columns can stay too wide however its words break, and a nested grid brings its own track minimums. A track with a zero minimum does nothing to stop any of these from overflowing it.

04

auto-fill or auto-fit

Both keywords tell the browser to repeat a track as many times as fits. In this pattern, you see a difference when there are fewer items than tracks. auto-fill keeps the empty tracks, so each card stays the width of one track. auto-fit collapses the empty tracks to zero, so the 1fr tracks that hold cards share the freed space and the cards stretch.

Same cards, two keywords Interactive
Measuring…

A short summary appears here after you change a control.

Start with three cards at full width. If your panel has room for more than three tracks, as a desktop panel usually does, the top grid shows dashed empty tracks after the last card. The bottom grid has none. Its cards are wider, and the readout lists the collapsed tracks as 0px. A phone may fit only two or three tracks. If you see no empty track, set the cards slider to 1. Then add cards until the first row is full, and the two grids become identical. This is why the difference can show up only on wide screens, or on a results page with just a few items.

One level deeper. The browser counts the repetitions before it places any items. When the maximum is 1fr, it uses the minimum for that count, which here is 96px, and includes the gaps. With auto-fit, every repeated track that ends up with no item is collapsed. Its size becomes 0px, and the gaps on both sides of it merge. getComputedStyle still lists each collapsed track as 0px. So the number of entries in the readout is the number of tracks the browser created, not the number you can see.

</> One keyword decides what happens to empty tracks.Show code
/* Keeps empty tracks: cards stay one track wide */
.cards { grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); }

/* Collapses empty tracks: cards stretch to fill the row */
.cards { grid-template-columns: repeat(auto-fit, minmax(96px, 1fr)); }

The column count follows the width of the grid itself, not the viewport. That lets an auto-fit grid work inside a sidebar or a card without any media query. When the cards also need to change their inner layout, not just their number per row, use queries that read the box.

05

When minmax seems ignored

For this kind of grid, with a pixel or rem minimum and a 1fr maximum, the repeat count follows a simple formula. For a box of width W, a gap g and a minimum m, the browser makes floor((W + g) ÷ (m + g)) tracks. It never makes fewer than one. That last rule causes a common overflow: when the box is narrower than the minimum, you still get one track at the full minimum width.

The formula against your browser Interactive
min(100%, …)
Measuring…

A short summary appears here after you change a control.

With the switch off, drag until the box in the readout is narrower than 14rem, which is 224px at a 16px root font size. The formula rounds down to zero, the browser raises that to one track, and the track sticks out of the box. On a narrow phone, this can cause a horizontal scrollbar that is hard to trace. Turn on min(100%, …). The minimum can now never be wider than the box, so the single track fits.

</> Cap the minimum at the width of the box.Show code
/* Overflows any box narrower than 14rem */
grid-template-columns: repeat(auto-fit, minmax(14rem, 1fr));

/* The minimum is never wider than the box itself */
grid-template-columns: repeat(auto-fit, minmax(min(100%, 14rem), 1fr));

If minmax() still looks ignored, check these causes in order:

  • A fixed count. repeat(3, minmax(200px, 1fr)) never wraps. Three tracks of at least 200px overflow any box narrower than 600px plus the gaps. Wrapping needs auto-fit or auto-fill.
  • A max smaller than the min. In minmax(300px, 200px) the browser ignores the smaller max, so the track is a fixed 300px.
  • An fr as the minimum. minmax(1fr, 300px) is invalid, so the browser drops the whole declaration. DevTools marks it as invalid.
  • A parent that grows. A flex item or grid item around your grid can have its own automatic minimum, so the blowout moves up a level. In a flex row or a 1fr column, give that parent min-width: 0. The same kind of rule works vertically: it is why a grid-based accordion needs min-height: 0 on its inner element to close fully.
06

Build yours, then copy it

You can tune the card grid in one place. Change the minimum and the gap, drag the preview width, and copy the result. The readout under the controls shows the tracks your browser resolved for the values you picked.

Tune it, then copy it Interactive
auto-fit
min(100%, …)
Measuring….cards { … }

A short summary appears here after you change a control.

Shrink the preview as far as it goes with min(100%, …) on, and the cards stack in one column that fits. Turn the switch off, and the track overflows whenever your chosen minimum is wider than the preview. Next, switch to auto-fill and lower the minimum. Once the row has room for more than three tracks, the empty ones stay on the right. A narrow phone may never have room for a fourth. The copied CSS includes overflow-wrap: anywhere. A fixed minimum does not grow for a long word, so the word could otherwise spill out of its card.

For accessibility, keep the reading order intact. Without order, dense packing or manual placement, these cards appear in source order, so keyboard focus and screen readers follow the same order your eyes do. Avoid grid-auto-flow: dense and manual reordering for cards that contain links or buttons. The CSS here animates nothing. If you later animate the cards, put the animation inside @media (prefers-reduced-motion: no-preference). Live text needs care too. The demos on this page send screen readers one short summary after updates pause for 300ms, not the full readout on every slider tick. That is a trailing debounce.

When to use it. Use auto-fit with min(100%, …) for card lists, galleries, product tiles and dashboard widgets: any set of similar items whose column count should follow the available space. Use minmax(0, 1fr) for fixed fr columns that hold user content, translations or URLs.

When not to. Do not use auto-fit for layouts that need a set number of columns, like a form with a label column and a field column. Write those tracks out explicitly. For items with very different natural widths, such as a toolbar or a tag list, flexbox with flex-wrap is usually the better fit. It can size each item from its content without lining items up in shared columns. And do not use minmax(0, 1fr) to hide an image or table that really is too wide. Give the image max-width: 100%, and put the table in a wrapper that scrolls.

Quick answers

Why is CSS grid minmax() not working?
Check the track you actually wrote. Plain 1fr means minmax(auto, 1fr), so wide content can enlarge the column. Other causes include a fixed repeat count that never wraps, a minimum wider than the grid, a maximum smaller than the minimum, an invalid fr minimum such as minmax(1fr, 300px), or a parent flex or grid item that grows.

Why are my 1fr columns not equal width?
A 1fr track has an automatic minimum, so content that cannot shrink, such as a long word, URL, or unwrapped code line, can make one column wider than its share. Use repeat(3, minmax(0, 1fr)) for three columns that stay equal. Then wrap or scroll the long content, because it can still spill out of its card.

Why does content still overflow with minmax(0, 1fr)?
minmax(0, 1fr) stops content from setting the column's minimum width, but it does not resize or wrap that content. For long words and URLs, add overflow-wrap: anywhere to the grid items. Give code blocks overflow-x: auto, give images max-width: 100%, and put wide tables inside a wrapper that scrolls.

What is the difference between auto-fit and auto-fill in CSS Grid?
Both create as many repeated tracks as fit. When there are too few items to occupy them, auto-fill keeps the empty tracks. auto-fit collapses empty repeated tracks to 0px. With a 1fr maximum, the occupied tracks then stretch into the freed space. When no repeated tracks are empty, the two layouts look the same.

How do I stop a grid with auto-fit and minmax() overflowing on mobile?
Use repeat(auto-fit, minmax(min(100%, 14rem), 1fr)). With minmax(14rem, 1fr), a grid narrower than 14rem still creates one 14rem track, which sticks out. Adding min(100%, 14rem) caps the track minimum at the grid's width. Long words or other wide content inside cards may still need their own wrapping, resizing, or scrolling rules.

New deep dives, in your inbox.