Build a CSS grid by drawing it
This CSS grid generator writes the code for a grid layout for you: you set the columns and rows, draw the areas with your mouse on the preview, and the tool produces the matching CSS and HTML. The preview uses exactly the same values as the code, so what you see is what you paste into your stylesheet. It is handy for a page with a header, sidebar and footer, or for an image gallery.
Grid places blocks in two dimensions, rows and columns at once. To line items up along a single row, such as a navigation bar, the flexbox generator is often the simpler choice.
How to use it
- Start from a preset: “Classic layout”, “Sidebar”, “3 × 3 gallery”, “Responsive cards” or “12 columns”.
- In “Columns” and “Rows”, type the sizes separated by spaces, or use the + and − buttons.
- Set “Gap between rows (px)” and “Gap between columns (px)”, then the grid height.
- On the preview, drag across empty cells: a new area appears. Drag an area to move it. Rename it in the “Areas” list, which also lets you adjust everything from the keyboard.
- Pick how areas are placed in the code: “Named areas” or “Line numbers”.
- Click “Copy CSS”, then “Copy HTML”.
Drag the bottom right corner of the preview to check the grid on a narrow or a wide screen.
Key concepts
The fr unit
fr stands for “fraction of the free space”. With three columns and a 16 px gap, the code is:
grid-template-columns: 1fr 2fr 1fr;gap: 16px;
On a grid 832 px wide, the two gaps take 32 px. That leaves 800 px, split into 4 shares of 200 px: the middle column is 400 px wide and the other two are 200 px each. You can mix px, % and auto freely with fr: 240px 1fr gives a fixed sidebar and a content column that takes the rest.
repeat() and minmax()
repeat(4, 1fr) is the same as 1fr 1fr 1fr 1fr; tick “Write repeat() for identical tracks” to get the short form. minmax(100px, 1fr) sets a minimum and a maximum size. Put them together and you get a card grid that adapts to the screen on its own, with no media query:
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
At 640 px wide with a 16 px gap, you get 3 columns; at 400 px, only one is left. If you prefer rem to px, the px to rem converter gives you the matching values.
Named areas
Each area has a name, and grid-template-areas draws the page as text, one string per row. The “Classic layout” preset gives:
grid-template-areas: "header header header" "sidebar main main" "footer footer footer";
Each block then gets its name: .sidebar { grid-area: sidebar; }. A dot marks an empty cell. An area must stay a rectangle, which is why the tool will not let two areas overlap. In “Line numbers” mode, the same area becomes grid-column: 1 / 2; grid-row: 2 / 3;: you count the lines between cells, not the cells themselves.
Alignment
justify-items and align-items position each block inside its cell; justify-content and align-content move the whole grid when it is smaller than its container. The tool only writes the values you change. Out of the box, the container class is .grid.
Browser support
CSS Grid works in all current browsers, on desktop and mobile. Before going live, you can minify the CSS you copied.
FAQ
Why can't I see my fr rows?
An fr row shares out the height of the grid. If the grid has no height, there is nothing to share. Enter a “Grid height” or use auto for the rows.
Should I use named areas or line numbers?
Named areas read like a floor plan and are easy to rearrange inside a media query. Line numbers work better when blocks need to overlap or when the grid has many columns.
Does Grid replace flexbox?
No, they work together: Grid for the page structure, flexbox to align the content of a bar or a button. You will often find a flex container inside a grid cell.
Are my settings sent anywhere?
No. The code is calculated in your browser and your last settings stay on this device.