Skip to content

Grid Generator

Drag across empty cells to add an item. Drag an item to move it, or its corner to resize.
Items
3
Line numbers, dashed cells and badges only show here, they're not in the code.
grid-template-columns the columns
Columns
3
All:
grid-template-rows 0 = the grid adds rows as needed
Rows
2
All:
grid-auto-rows size of the rows the grid adds by itself
gap
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

CSS

.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(2, 100px);
  gap: 12px;
}

.item {
  /* Demo look: swap for your own styles */
  padding: 12px 16px;
  border-radius: 8px;
  background: #a3e635;
  color: #0b0f19;
  font-weight: 600;
}

.item-1 {
  grid-column: 1 / 3;
  grid-row: 1;
}

HTML

<div class="container">
  <div class="item item-1">1</div>
  <div class="item item-2">2</div>
  <div class="item item-3">3</div>
</div>

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.

    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:

    • 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.

    grid-template-columns: 1fr 1fr 1fr;

    Three equal columns.

    grid-template-columns: 2fr 1fr;

    The first column gets twice the share.

    grid-template-columns: 100px 1fr;

    Fixed first, then 1fr takes whatever is left.

    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.

    grid-template-columns: 1fr 1fr;

    The long link makes the first column wider than half.

    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);

    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):

    repeat(auto-fill, minmax(60px, 1fr))

    auto-fill keeps the empty columns, so the items stay small.

    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.

    .item-1 { grid-column: 1 / 3; }

    From line 1 to line 3: two columns wide.

    .item-1 { grid-column: 1 / -1; }

    From the first line to the last, whatever the column count.

    .item-2 { grid-column: span 2; }

    Two columns wide, wherever the next free spot is.

    .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.

    Named Areas: the Holy Grail Layout

    .container {
      display: grid;
      grid-template-columns: 150px 1fr 150px;
      grid-template-rows: auto 1fr auto;
      grid-template-areas:
        "header header header"
        "nav    main   aside"
        "footer footer footer";
    }
    header { grid-area: header; }

    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 celljustify-itemsalign-items
    One item onlyjustify-selfalign-self
    All the tracks in the containerjustify-contentalign-content
    Both at onceplace-items, place-self, place-content
    justify-items: start;

    Items shrink to their content and sit at the left of each cell.

    justify-items: center;

    Centered across each cell.

    align-items: end;

    Items sit at the bottom of each cell.

    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:

    justify-content: center;

    The 60px columns move to the middle as a group.

    justify-content: space-between;

    First and last columns touch the edges.

    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.

    A Responsive Grid Without Media Queries

    .cards {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
      gap: 16px;
    }

    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

    Next steps: round the corners of your cards with the CSS Border Radius Generator, add depth with the CSS Box Shadow Generator, or line up the inside of each card with the CSS Flexbox Generator.

    Frequently Asked Questions

    What does 1fr mean in CSS grid?

    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.