CSS Grid Generator
Developer Tools · Added
Grid is the layout system worth learning and the one hardest to hold in your head, because a track list is a small language of its own. Build the layout here instead: add and size tracks, set the gaps and alignment, drop a cell across two columns, and watch a real grid respond. What you copy is the CSS that produced exactly what you are looking at, with every default left out.
Preview
CSS
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: 100px;
gap: 12px;
}HTML
<div class="grid"> <div class="cell">1</div> <div class="cell">2</div> <div class="cell">3</div> <div class="cell">4</div> <div class="cell">5</div> <div class="cell">6</div> </div>
3 explicit columns. Anything that does not fit spills into implicit rows, which take their height from their content unless grid-auto-rows says otherwise.
Only properties that differ from the CSS default are written out, so what you copy is the change rather than a dump of every grid property. display: grid and the column track list are always included, because they are what make the rest apply.
How to use the css grid generator
- 1Choose explicit tracks if you know how many columns you want, or the responsive mode if the column count should follow the container width.
- 2Size each track. Use fr for a share of the free space, px or rem for a fixed width, and auto to let the content decide.
- 3Set the row and column gaps, then the four alignment properties — the hints say which of them positions items inside their cells and which moves the whole grid.
- 4Click a numbered cell to select it, then give it a column or row span, or an explicit start line. Zero means automatic placement.
- 5Copy the CSS, and the matching HTML if you need a skeleton to paste it onto.
Examples
Three equal columns
- Input
- Explicit tracks: 1fr, 1fr, 1fr with a 12px gap
- Result
- grid-template-columns: repeat(3, 1fr); gap: 12px;
Identical tracks collapse into repeat() automatically at three or more, which is what you would have written by hand.
A responsive grid with no media query
- Input
- Responsive mode, minimum column 240px, auto-fit
- Result
- grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
The column count follows the container's width on its own. Three across on a laptop, one on a phone, and not a breakpoint anywhere.
A feature cell spanning two columns
- Input
- Cell 1 with a column span of 2 in a three-column grid
- Result
- .grid > .cell:nth-child(1) { grid-column: span 2; }
Only the cells you actually change get a rule. The other five inherit the container's placement and need no CSS at all.
About the css grid generator
Tracks, lines and the fr unit
A grid is defined by its tracks — the columns and rows — and the lines between them. Declaring three columns creates four vertical lines, numbered from one, and placing an item is a matter of naming the lines it starts and ends at. This is the piece that makes Grid feel different from everything before it: position is expressed against the container's structure rather than against the item's neighbours.
The fr unit is the other new idea, and it is not a percentage. One fr is one share of whatever space remains after fixed sizes and gaps have been taken out, so 200px 1fr 1fr gives a fixed sidebar and two columns that split what is left. Percentages cannot do this, because they are computed against the container before gaps are subtracted, which is why percentage-based grids overflow by exactly the width of their gutters.
minmax() combines the two. minmax(240px, 1fr) means never narrower than 240px, otherwise take a share of the free space — and wrapping it in repeat(auto-fit, …) tells the browser to make as many such columns as fit. That single declaration is a responsive gallery with no media query, and it is the strongest argument for learning Grid rather than reaching for a framework.
Alignment: four properties, two questions
Grid has four alignment properties and they are routinely confused, which is understandable because their names are almost identical. The distinction is what they move. justify-items and align-items position each item inside its own cell — horizontally and vertically respectively. justify-content and align-content move the entire grid inside its container, which only does anything when the tracks are smaller than the space available.
That last condition is the source of most of the confusion. Set justify-content to center on a grid whose columns are all fr, and nothing happens: the fr tracks have already grown to fill the container, so the grid is exactly as wide as its box and there is nothing to centre. Nothing is broken; the property is doing precisely what it says to a grid that has no free space.
The preview on this page is deliberately built from the same options object that produces the CSS, so what you see and what you copy cannot drift apart. When a setting appears to do nothing here, it will also do nothing in your stylesheet — and the note under the preview will usually tell you which of these two situations you are in.
Frequently asked questions
What is the difference between auto-fit and auto-fill?
When should I use Grid rather than Flexbox?
Why does my justify-content setting appear to do nothing?
Why is the generated CSS so short?
Do the spans work at every screen width?
Related tools
CSS Flexbox Generator
Developer Tools
Build a flex layout against a live preview and copy CSS containing only the properties you changed.
CSS Gradient Generator
Developer Tools
Build a linear, radial or conic CSS gradient visually and copy the declaration.
Box Shadow Generator
Developer Tools
Build a layered CSS box-shadow with live preview, including inset and multi-layer depth.