CSS Grid Generator

Builds a CSS Grid layout visually — columns, rows, gaps and alignment — with a live preview and the CSS to copy. Grid handles two dimensions at once, which is what separates it from flexbox and what makes page-level layout straightforward for the first time.

Live demo · real result from this tool

Press Generate to create a result.

Processed on your device — nothing is uploaded.

How to use CSS Grid Generator

  1. Set the number of columns and rows, or write a template with fr units.
  2. Set the gap — one value for both axes, or separate row and column gaps.
  3. Adjust justify and align to position items within their tracks.
  4. Copy the generated CSS.
  5. gap replaced grid-gap years ago and works in flexbox too; the old name still works but is deprecated.
  6. An fr track will not shrink below its content unless you set minmax(0, 1fr) — the usual cause of a grid overflowing its container.
  7. Grid placement does not change source order for assistive technology or tab order.

fr is the unit that makes grid worth using

A fraction unit distributes whatever space is left after fixed sizes and gaps are subtracted. grid-template-columns: 200px 1fr gives a fixed sidebar and a main column that takes the rest; 1fr 2fr splits the remainder one-third and two-thirds. This is why grid replaced percentage widths: percentages do not know about your gap, so 50% + 50% + a 20px gap overflows, while 1fr 1fr with a gap simply works. Nothing needs recalculating when the gap changes.

A responsive grid with no media queries

grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)) fits as many 240px-minimum columns as the container allows and stretches them to fill it — four across on a desktop, two on a tablet, one on a phone, with no breakpoints written anywhere. It is the single most useful line in CSS layout. auto-fill is the near-identical alternative: it keeps empty tracks where auto-fit collapses them, which matters only when you have fewer items than columns.

Named areas make layouts readable

grid-template-areas lets you draw the layout in the stylesheet as strings — “header header” / “sidebar main” / “footer footer” — and assign each element with grid-area: header. Six months later that is instantly legible in a way that line numbers are not, and rearranging the layout for a breakpoint means rewriting three strings rather than renumbering every child. Note that source order still governs reading order for screen readers and keyboard navigation, so do not use grid placement to reorder anything meaningful.

Grid or flexbox

Grid when the layout is two-dimensional and things must line up across rows and columns: page structure, dashboards, card galleries with consistent columns, forms with aligned labels. Flexbox when it is one-dimensional: a navbar, a button row, centring, a list that wraps. They are complementary rather than competing, and a grid whose cells each use flex internally is the normal arrangement rather than a compromise.

Frequently asked questions

What is CSS Grid?
A two-dimensional layout system that arranges items into rows and columns using grid-template properties.
When should I use Grid instead of Flexbox?
Use Grid for two-dimensional layouts (rows and columns together); Flexbox is better for one-dimensional rows or columns.
What does fr mean in a grid?
The fr unit distributes leftover space; 1fr 2fr splits free space so the second column is twice as wide as the first.
How do I make a responsive grid without media queries?
grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)). The browser fits as many columns of at least 240px as will go and shares the remaining space between them, reflowing at every width with no breakpoints.
What is the difference between auto-fit and auto-fill?
Both create as many tracks as fit. auto-fill keeps empty tracks, so items stay at their minimum width with space left over; auto-fit collapses empty tracks so existing items stretch to fill the row. With more items than columns they behave identically.
Why is my grid overflowing its container?
Almost always a 1fr track holding content wider than the track — a long word, a pre-formatted block, an image. An fr track has an automatic minimum size of its content. Use minmax(0, 1fr) to let it shrink properly.

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close