Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages

Free tool · Developer tools

CSS grid generator

Set the columns, rows and gaps, tap cells to draw named areas, and watch the layout appear. Copy the finished CSS and HTML when it looks right.

  • Free
  • No sign-up
  • Runs in your browser

By Maaz Zindani · Updated

One size for each column or row, with spaces between. 1fr is one share of the free space, 200px is a fixed size, auto fits the content. You can also write repeat(3, 1fr). Up to 12 each.

A whole number from 0 to 200. The gap is the space between cells.

Named areas

Type a name, then tap cells to give them that name. Tap a named cell again to clear it. An area must be one solid rectangle.

Live preview
  • 3 columns by 3 rows
  • 4 named areas
CSS
.container { display: grid; grid-template-columns: 200px 1fr 1fr; grid-template-rows: 80px 1fr 60px; gap: 10px; grid-template-areas: "header header header" "sidebar main main" "footer footer footer"; } .header { grid-area: header; } .sidebar { grid-area: sidebar; } .main { grid-area: main; } .footer { grid-area: footer; }
HTML
<div class="container"> <div class="header">header</div> <div class="sidebar">sidebar</div> <div class="main">main</div> <div class="footer">footer</div> </div>

How to use the CSS Grid Generator

  1. Type the Column sizes and Row sizes, one size for each, such as 200px 1fr 1fr. The Columns and Rows counts follow.
  2. Set the Column gap and Row gap in pixels.
  3. Under Named areas, type a name and tap cells to paint it. Tap a painted cell again to clear it.
  4. Check the Live preview, then press Copy CSS, Copy HTML or Copy both.

How do I make a CSS grid?

A grid has three parts. You turn an element into a grid with display: grid. You list the sizes of its columns with grid-template-columns and of its rows with grid-template-rows. The elements inside it then fall into the cells, left to right and top to bottom. This page writes those lines for you and shows the result as you type.

The space between cells is the gap, written gap. It sits only between cells, never before the first or after the last. If the row gap and column gap are equal, the page writes one value. If they differ, it writes the row gap first and the column gap second, as CSS expects.

The page starts with a common page layout: a header across the top, a sidebar, a main area and a footer. Its columns are 200px 1fr 1fr, its rows are 80px 1fr 60px and its gap is 10px. The full CSS has 18 lines, and you can read it in the result card.

What do fr, px and auto mean in a grid?

Each size you type is one track, a column or a row. A size in px is fixed. A size in % is a share of the whole grid. The fr unit is a share of what is left. According to the W3C CSS Grid specification, a track in fr gets a part of the free space in proportion to its number, so 2fr gets twice as much as 1fr. The free space is what remains after the fixed sizes and the gaps are taken out. The specification advises using values of at least 1.

auto sizes the track to fit its content. When all columns are equal, the page writes repeat(3, 1fr) instead of repeating the size three times. You can type repeat() yourself too.

The table shows the widths for a grid 900 px wide with a 10 px gap. The widths come from the same code the page uses. A track in auto cannot be worked out without content, so it has no number.

Column sizes in a 900 px wide grid with a 10 px gap
You typeCSS writtenWidths in px
1fr 1fr 1frrepeat(3, 1fr)293.3, 293.3, 293.3
200px 1fr 2fr200px 1fr 2fr200, 226.7, 453.3
25% 1fr25% 1fr225, 665
100px auto 1fr100px auto 1frdepends on the content
repeat(2, 150px)repeat(2, 150px)150, 150

In the first row each column gets the same share of the free space. In the second row, the 200 px column is taken first, then the gaps, and what remains is divided in the ratio 1 to 2. Percent works against the full width, not against what is left, so 25% of 900 px is always 225 px.

A bare number, like 100, is read as pixels. The page asks for a size above 0 in fr and refuses words it does not know. At most 12 columns and 12 rows are allowed.

How do named grid areas work?

Named areas let you describe a layout as a picture. Each row of the grid becomes a line of text in quotes, and each word in it is the name of the cell. A dot means an empty cell. Then each element picks its area with grid-area: name. Tap cells in the page to paint the picture, and the page writes the text for you.

The W3C specification sets the rules. Every row must have the same number of cells, and an area made of several cells must fill one solid rectangle. If a name does not form a rectangle, the whole declaration is invalid. Take a 2 by 2 grid where "a" fills the first column and "b" and "c" share the second. Its areas are written as "a b" "a c".

That works because "a" covers two cells in a straight line. If you painted "a" in the top left and the bottom right, the page warns you and leaves the areas out of the code, because CSS would ignore them anyway.

Area names use letters, numbers, hyphens and underscores, and cannot begin with a digit. The page cleans a name as you type it, so "Main Content" becomes main-content.

How do I use the generated code?

Paste the CSS into your stylesheet and the HTML where the layout should appear. With named areas, the HTML has one element for each area and the class matches the name. Without areas, it has one numbered element for each cell, which you can rename.

The preview shows the grid with the same properties the code uses, so what you see is what the browser draws. The preview has no content, so a track in auto or fr shrinks to a small height. A real page fills it with content.

This page writes the basic parts of a grid. It does not add minmax(), auto-fit or media queries. You can type them in your own stylesheet once the main structure is right. Everything runs in your browser and nothing you set is sent anywhere.

Use it with

Questions

What does 1fr mean?

One share of the free space. A grid with 1fr 2fr gives the second column twice the width of the first, after fixed sizes and gaps are taken out.

What is the difference between gap and padding?

Gap is the space between cells. Padding is the space inside the grid box, around the edge. The gap is never added before the first or after the last track.

How do I leave a cell empty in grid-template-areas?

Write a dot for it. The page does this for every cell you do not paint.

Why did the page leave my areas out of the CSS?

One of the names does not form a solid rectangle. CSS treats the whole areas list as invalid in that case. Paint the cells of that name as one block.

Can I use percentages and auto?

Yes. Type sizes such as 25% or auto. Percent is a share of the whole grid, and auto fits the content.

Is my layout sent anywhere?

No. The preview and the code are made in your browser.

The tool's results are computed in your browser by the code on this page, using the sources listed above; the explanations are checked against the tool's own output.