Drag across empty cells to add an item. Drag an item to move it, or its corner to resize.
Items
Line numbers, dashed cells and badges only show here, they're not in the code.
Paint with
grid-template-columns the columns
Columns
All:
px
Makes as many columns as fit, each at least this wide, then shares the rest with 1fr. Resize the
page and the column count changes, no media queries needed.
grid-template-rows 0 = the grid adds rows as needed
Rows
All:
grid-auto-rows size of the rows the grid adds by itself
gap
px
px
justify-items items in their cells, across
align-items items in their cells, up and down
justify-content the columns, when there’s spare width
align-content the rows, when there’s spare height
These two move the whole grid. They only do something when the tracks add up to less than the container,
so fr tracks leave them nothing to do.
grid-auto-flow how items without a spot fill in
Container size untick for auto
px
px
Pick an item or click one in the preview
Letters, digits and hyphens. CSS treats Main and main as two names. Paint this item's cells in the preview, or set them below.
Values left at their default (stretch, normal, row, no gap) aren't written.
Presets Common layouts, one click to load
CSS Grid Generator: Build Grid Layouts Visually
What is CSS grid?
CSS grid lets you lay things out in rows and columns at the same time. You put display: grid on
a parent element (the container), say how wide the columns are and how tall the rows are, and its children
(the items) fill the cells. Then you can make any item cover several cells, give areas names like
header and sidebar, and line everything up without a single float or margin hack.
Page layouts, dashboards, photo galleries and card lists are all grid jobs. This generator lets you build one on real
boxes: drag across cells to add an item, drag it to move it, paint named areas, and copy the CSS and HTML.
123456
3 equal columns
HeaderSideMain
Page layout
Big2345
Mosaic with spans
Centered
place-items: center
How to Use It, Step by Step
Five quick steps. Everything happens in your browser, and your last grid is remembered for next time.
Step 1 Start From a Preset
Pick one under Presets: Holy grail, 12-column layout, Card gallery, Dashboard, Photo mosaic,
Sidebar + content or Centered item. Already have grid CSS, maybe from another generator? Click
Paste CSS, paste it and press Load.
Step 2 Set the Columns and Rows
On the Container tab, use + and − for 0 to 12 columns and rows,
and give each one a size: fr, px, %, auto,
min-content, max-content or minmax(). The All buttons set
every track at once. Pick auto-fit for a responsive grid.
Step 3 Draw and Move Items
In the preview, drag across empty cells to add an item there. Drag an item to move it, and drag the small square at
its corner to resize it. The numbers around the grid are the line numbers, so you can see why an item says
grid-column: 2 / 4. Prefer the keyboard? The Item tab has start line, end line and
span fields.
Step 4 Or Name Areas
Switch to Named areas, pick an item under the preview and drag across cells to paint its area.
The tool writes the grid-template-areas strings and a grid-area for each item, and warns
you when an area isn’t a rectangle.
Step 5 Copy the Code
Copy all gives you the CSS in a <style> tag plus the HTML. The preview runs
exactly that code. Read the notes under it: they tell you when something won’t do what you expect, like
justify-content with fr columns. Switch to Tailwind for classes like
grid grid-cols-12 gap-4 col-span-full.
Lines, Tracks, Cells and Areas
Four words explain all of grid. Here’s a grid with three columns and two rows:
1234123
Lines are the borders between the columns and rows. Three columns have four column lines, numbered 1 to 4 (and -4 to -1 from the other end).
Tracks are the columns and rows themselves. The middle column (light green) is one track.
Cells are where one row meets one column. The dark one, top right, is a single cell.
Areas are rectangles of cells. The two lime cells at the bottom left could be an area called footer.
gap puts space between the tracks, never at the outer edges. One value sets both directions;
gap: 20px 10px is 20px between rows and 10px between columns.
The fr Unit
fr means “a fraction of the free space”. The grid takes away the fixed sizes and the gaps first, then
shares what’s left between the fr tracks.
1fr1fr1fr
grid-template-columns: 1fr 1fr 1fr;
Three equal columns.
2fr1fr
grid-template-columns: 2fr 1fr;
The first column gets twice the share.
100px1fr
grid-template-columns: 100px 1fr;
Fixed first, then 1fr takes whatever is left.
auto1frauto
grid-template-columns: auto 1fr auto;
auto fits the content. Great for a label, a field and a button.
💡 fr or %? Four columns of 25% plus a gap are wider than 100%, so the grid overflows.
Four columns of 1fr share the space after the gaps and always fit.
repeat() and minmax()
repeat(4, 1fr) is short for 1fr 1fr 1fr 1fr. It can repeat a pattern too:
repeat(2, 100px 1fr) is 100px 1fr 100px 1fr. The generator writes repeat()
when all tracks are the same size (untick Write equal sizes as repeat() to list them).
minmax(min, max) gives a track a range. minmax(100px, auto) is at least 100px and grows with
its content, which is a good size for rows. minmax(0, 1fr) is a 1fr column that’s allowed to
get narrower than its content, so a long word can’t make one column wider than the others.
https://example.com/a-very-long-link2
grid-template-columns: 1fr 1fr;
The long link makes the first column wider than half.
Both columns stay exactly half, and the link wraps.
auto-fit vs auto-fill
Put auto-fit or auto-fill in repeat() instead of a number and the browser makes
as many columns as fit. They only differ when there aren’t enough items to fill a row. Both boxes below have room for
more 60px columns than there are items (there are only two):
12
repeat(auto-fill, minmax(60px, 1fr))
auto-fill keeps the empty columns, so the items stay small.
12
repeat(auto-fit, minmax(60px, 1fr))
auto-fit collapses the empty columns, so the items stretch.
Placing Items by Line Numbers and Spans
Items without a position fill the next free cell, left to right, top to bottom. To put one somewhere, give it a start
and an end line with grid-column and grid-row, or just say how many tracks to
span.
grid-column: 1 / 323
.item-1 { grid-column: 1 / 3; }
From line 1 to line 3: two columns wide.
1 / -123
.item-1 { grid-column: 1 / -1; }
From the first line to the last, whatever the column count.
1span 234
.item-2 { grid-column: span 2; }
Two columns wide, wherever the next free spot is.
row: 1 / 32345
.item-1 { grid-row: 1 / 3; }
Two rows tall. The other items flow around it.
If an item goes past the lines you defined, or you didn’t define any rows, the grid adds tracks by itself. These are
implicit tracks, and grid-auto-rows sets their size. In the generator they have amber outlines.
Items can also overlap: the one later in the HTML sits on top, and z-index changes that.
💡 grid-auto-flow: dense lets later small items fill holes left by big ones. Try the Photo mosaic preset
and switch between row and row dense.
Each string is one row and each word is one cell, so the layout is drawn right in the CSS. A name repeated across
a row makes the area wider, repeated down the rows makes it taller. Use a dot (.) for an empty cell.
Then give each item grid-area: name.
The one rule: every area must be a single filled rectangle. An L shape breaks it, and the browser then ignores the
whole grid-template-areas line. The generator tells you when that happens.
Alignment: justify vs align, items vs content
Grid has two directions and two levels, so four properties. justify works across (left to right),
align works up and down. -items moves each item inside its own cell,
-content moves the whole set of tracks inside the container.
Across (inline)
Up and down (block)
Each item in its cell
justify-items
align-items
One item only
justify-self
align-self
All the tracks in the container
justify-content
align-content
Both at once
place-items, place-self, place-content
12
justify-items: start;
Items shrink to their content and sit at the left of each cell.
12
justify-items: center;
Centered across each cell.
12
align-items: end;
Items sit at the bottom of each cell.
12
place-items: center;
Both at once: dead center in every cell.
justify-content and align-content only do something when the tracks add up to less than the
container. fr tracks always fill it, so with 1fr columns justify-content has
nothing to move. With fixed columns it does:
12
justify-content: center;
The 60px columns move to the middle as a group.
12
justify-content: space-between;
First and last columns touch the edges.
12
justify-content: end;
All columns move to the right.
The shortest way to center anything is display: grid; place-items: center; on the parent. More ways are in
our how to center text in HTML tutorial.
One line does the work: as many columns of at least 200px as fit, sharing the rest. A phone shows one column, a
wide screen five or six. If the container can get narrower than 200px, write
minmax(min(200px, 100%), 1fr) so a single column can shrink too. Pick auto-fit in the
generator, or load the Card gallery preset, and resize your browser to watch it.
Grid or Flexbox?
Use grid when rows and columns should line up with each other: a page layout, a dashboard, a gallery,
a form with labels and fields. Use flexbox for one row or one column where the content decides the
sizes: a navbar, a row of buttons, a list of tags. They work well together, a grid for the page and flexbox inside each
part. Build the one-direction parts with the CSS Flexbox Generator.
Common Mistakes
Putting display: grid on the items
display: grid, the columns and the gap go on the parent. Only its direct children
become grid items, so a wrapper div in between turns everything inside it into one item.
Using % columns with a gap
25% 25% 25% 25% plus a gap is more than 100%, so the grid sticks out of its container. Use
repeat(4, 1fr).
Areas that aren’t rectangles
"a a" "a b" makes a an L shape, and the browser throws away the whole
grid-template-areas value. Split it into two areas or change the shape.
Counting cells instead of lines
grid-column: 1 / 3 covers two columns, not three: it runs from line 1 to line 3. When in doubt, use
span 3, or look at the line numbers around the grid in the generator.
fr rows in a container with no height
1fr rows share the container’s height. If the container has no height, there’s nothing to share and
the rows just fit their content. Set a height or min-height: 100vh.
What This Generator Can’t Do
Fixed sizes are in px (and % for tracks). It doesn’t write rem, vh, calc(), fit-content() or var(); pasted rem values are turned into px.
It doesn’t write named lines like [content-start], grid-auto-columns or the grid-template and grid shorthands. Pasted line names are dropped.
Responsive mode always uses minmax(Npx, 1fr), and named areas need a fixed number of columns.
Up to 12 columns, 12 rows and 12 items, and items placed by line numbers end at line 13 at most.
It works on one container. For a grid inside a grid item (or subgrid), build each level on its own and nest the HTML.
The Tailwind output covers the layout only, for Tailwind 3.3 and newer. Areas use arbitrary properties like [grid-area:header].
fr is a fraction of the free space. The grid first takes away fixed sizes and gaps, then shares what’s left between the fr tracks. So grid-template-columns: 200px 1fr 2fr gives the first column 200px, and the other two split the rest 1 : 2. Unlike %, fr never pushes the grid wider than its container because of the gaps.
What is the difference between auto-fit and auto-fill?
Both make as many columns as fit, for example with repeat(auto-fill, minmax(200px, 1fr)). The difference shows when there aren’t enough items to fill a row. auto-fill keeps the empty columns, so the items stay their normal width. auto-fit collapses the empty columns to zero, so the items stretch across the whole row.
How do I make a responsive grid without media queries?
Use grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)). The browser fits as many columns of at least 200px as it can and shares the rest of the width between them. On a phone you get one column, on a laptop maybe four. Pick auto-fit in the generator and drag the slider to change the smallest width.
How do grid line numbers work?
Lines sit between and around the tracks. Three columns have four column lines, numbered 1 to 4 from the left, and also -1 to -4 from the right. grid-column: 1 / 3 means start at line 1 and end at line 3, so the item covers two columns. grid-column: 1 / -1 always runs from the first line to the last one of the grid you defined, however many columns there are.
What is the difference between grid-column: 1 / 3 and span 2?
Both make an item two columns wide. 1 / 3 also says where: from line 1 to line 3. span 2 only says how wide, and lets the grid find a free spot by itself. Use line numbers for a fixed layout and span for galleries and cards that should flow.
Why is my grid-template-areas not working?
Usually one of three things. Every area must be one filled rectangle, so an L shape or a name used in two separate places breaks it. Every row string must have the same number of cells. And the items need grid-area: name with exactly the same name, without quotes. If any of this is wrong, the browser ignores the whole grid-template-areas line. Use a dot (.) for an empty cell.
What are implicit rows and grid-auto-rows?
When an item lands outside the rows you defined, or you don’t define any rows at all, the grid adds rows by itself. Those are implicit rows. Their height comes from grid-auto-rows, which is auto (as tall as the content) unless you set it. grid-auto-rows: minmax(100px, auto) is a good default: at least 100px, taller when the content needs it.
Why does my 1fr column get wider than the others?
1fr has a minimum of auto, so a column can’t get narrower than its widest content, like a long word, a URL or a big image. Use minmax(0, 1fr) instead and the columns stay equal (then let the text wrap with overflow-wrap: anywhere). That’s also what Tailwind’s grid-cols-3 uses.
Should I use CSS grid or flexbox?
Use grid when you want rows and columns that line up with each other: page layouts, dashboards, galleries, forms. Use flexbox for a single row or column where the content sets the sizes, like a navbar or a row of buttons. Many pages use both, a grid for the page and flexbox inside each card.