Colour CSS

CSS grid generator

1
2
3
4
5
6

Generated in your browser · nothing is uploaded

Live preview · repeat(), not a repeated list

Builds a display: grid rule with repeat() columns, rows and gaps, previewed as real boxes so you can see what the numbers do.

How to use the css grid generator

1 Set the number of columns and rows you want written into the rule.
2 Set the gaps. Row and column gaps can differ, and the shorthand takes the row one first.
3 Pick a column size, and read the note below on why 1fr is not always the right one.
4 Copy the CSS, which is a fixed track count rather than a responsive pattern.
5 Swap the count for auto-fill or auto-fit by hand if you want the layout to reflow on its own.

The column size is the choice that decides whether a grid behaves, and 1fr is not automatically the right answer. An fr is a share of the leftover space, but the specification defines 1fr as shorthand for minmax(auto, 1fr), which means the track has a minimum of auto and will refuse to shrink below the content inside it. Put one long unbroken string, a wide table or an image with no max-width into such a column and the whole grid pushes past its container. That single detail is the most common grid bug there is. minmax(0, 1fr) sets the floor to zero and the columns behave, at which point the content overflows the column instead, where overflow: hidden or a min-width: 0 on the item catches it. Both of those sizes are in the dropdown here, so the difference is a click apart on real content.

The other two sizes repay a moment of experimenting for the same reason. min-content shrinks a track to the widest thing that cannot be broken, usually the longest word. max-content grows it to the width the content would take if it never wrapped at all. Neither is a layout you would usually ship, but seeing them side by side explains what auto and fr are negotiating between.

What this generator writes is a fixed grid: repeat(3, 1fr) means three columns, at every viewport width, until you change it. The genuinely responsive pattern is a hand edit away and repays memorising: repeat(auto-fill, minmax(220px, 1fr)) fits as many columns of at least 220px as the width allows and shares whatever is left between them, reflowing with no media queries at all. Substitute auto-fit and the only difference appears when you have fewer items than the row could hold: auto-fit collapses the empty tracks to nothing and the items you do have stretch to fill the row, while auto-fill keeps the empty tracks and leaves the gap. Neither is better; a search-results page with one result looks strange under auto-fill and a calendar looks wrong under auto-fit.

One last thing the rule here writes explicitly is the row count. grid-template-rows: repeat(2, auto) sizes exactly two rows. Give the grid more items than two rows of columns can hold and the extras land in implicit rows, which take their size from grid-auto-rows and default to auto regardless of what the explicit rows say. Grids grow along that axis quietly, which is usually what you want and occasionally a surprise.

What people use it for

  • Getting a fixed card grid written out correctly the first time
  • Finding out why a grid is overflowing its container
  • Setting a row gap and a column gap to different values
  • Seeing what min-content and max-content actually do to a column
  • Writing repeat() rather than listing every track by hand
  • Comparing 1fr against minmax(0, 1fr) on real content
  • Working out the starting point for a responsive grid you then edit
  • Explaining the fr unit to somebody who has only used percentages

Questions

Because 1fr is really minmax(auto, 1fr), so the column will not shrink below its content. Switch to minmax(0, 1fr) and the track can finally give way.

MDN, CSS grid layout
Was this tool any good?
Internal signal only · I use it to find the tools worth rebuilding