This CSS grid generator builds the code for a grid layout while you watch it take shape. Set the number of columns and rows, choose how wide and tall they should be, and add the gaps between them. The preview updates as you type, and the CSS and HTML are ready to copy into your project. It's a quick way to start a page layout, a card gallery or a dashboard without having to remember the exact syntax.
What is CSS grid?
CSS grid layout introduces a two-dimensional grid system to CSS. A grid is a set of intersecting horizontal and vertical lines that define rows and columns, and elements can be placed onto the grid within these lines. Grids can be used to lay out major page areas or small user interface elements. You create a grid container by declaring display: grid on an element, and as soon as you do, all direct children of that element become grid items.
How to use the CSS grid generator
- Enter the columns and rows. You can have up to 12 of each.
- Choose the column width and row height: equal shares, fit the content, or a fixed size in pixels.
- Set the gaps between columns and between rows.
- Name the class if you don't want to use grid.
- Copy the CSS and the HTML into your files.
Worked example
Leave the settings at 3 columns and 2 rows, with equal-share columns, content-height rows and 16px gaps. The generator gives this CSS:
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(2, auto);
gap: 16px;
}
The HTML is a div with the class grid that holds six child divs, numbered 1 to 6. Paste both into a page and you'll see three equal columns in two rows, exactly like the preview.
The three track sizes
The rows and columns of a grid are called tracks. A grid track is the space between any two adjacent lines on the grid, and you define tracks with the grid-template-columns and grid-template-rows properties.
| Choice | Code | What it does |
|---|---|---|
| Equal shares | 1fr | Splits the free space evenly between the tracks |
| Fit the content | auto | Makes each track as large as its content needs |
| Fixed size | 200px | Gives every track the same set size |
You can create a grid with fixed track sizes, using pixels for example, or with flexible sizes using percentages or the fr unit, which was designed for this purpose. Equal shares suit most page layouts because the columns stretch and shrink with the screen.
What repeat() means
When several tracks are the same size, the code uses repeat() notation instead of writing the size again and again. So repeat(3, 1fr) means three columns of one share each, and it's the same as writing 1fr 1fr 1fr. With a single column or row, the generator writes the size on its own.
Gaps between cells
Gutters, or alleys, between grid cells are created with the column-gap and row-gap properties, or with the shorthand gap. When both gaps are equal, the generator writes one value, such as gap: 16px. When they differ, it writes the row gap first and then the column gap, as in gap: 10px 20px. Set both gaps to 0 and the gap line is left out altogether.
Adding more items than cells
The HTML includes one child for each cell, so a 3 by 2 grid has six items. If you add a seventh, the browser doesn't drop it. Tracks are also created in the implicit grid when an item is positioned outside the tracks you defined, so the extra item starts a new row. That's handy for galleries and lists where you don't know how many items there'll be.
Tips for real projects
- Use equal shares for responsive pages. Fixed pixel columns can overflow on a phone.
- Change the class name to something that describes the content, such as cards or gallery.
- Replace the numbered divs with your own content once the layout looks right.
- Nest grids when you need to. A grid item can be a grid container too.
Going further by hand
The generator covers the most common layouts, and the code is easy to extend once it's in your stylesheet. A useful next step is the minmax() function. When you set up a grid, you may want to give tracks a minimum size but also let them expand to fit any content that's added. For example, you may want rows that never collapse smaller than 100 pixels, but that stretch if the content is taller. Writing minmax(100px, auto) as the row size does exactly that. You can also mix sizes in one line, such as a fixed sidebar beside a flexible main column, by writing the sizes one after another instead of using repeat(). And if the grid should sit in a line of text, declare display: inline-grid instead of display: grid.
Common mistakes
- Putting display: grid on the wrong element. It goes on the parent, not on the items.
- Wrapping items in an extra div. Only direct children become grid items.
- Using fixed pixel widths everywhere. The grid can't adapt to small screens.
- Forgetting the units. A gap needs a unit such as px.
Related tools
Add depth with the CSS box shadow generator, shrink your stylesheet with the CSS and JavaScript minifier, and tidy your markup with the HTML formatter.
What next
Use this often? Add CSS Grid to your Home screen.
Frequently asked questions
How does the CSS grid generator work?
It turns your choices for columns, rows, sizes and gaps into grid-template-columns, grid-template-rows and gap rules, and shows a live preview.
What does 1fr mean in CSS grid?
It's a flexible unit that stands for one share of the free space in the grid, so three 1fr columns are equal in width.
What does repeat(3, 1fr) do?
It creates three tracks of 1fr each. It's a shorter way of writing 1fr 1fr 1fr.
How do I add space between grid items?
Use the gap property, or column-gap and row-gap. The generator adds it for you.
What happens if I add more items than cells?
The browser adds new rows automatically for the extra items.
Was this tool helpful?
What went wrong?
We only receive the reason you pick, never what you typed into the tool.
Thanks for your feedback.