Coderspace Log In

CSS Grid Generator

Set up your columns and rows, adjust the gaps and place items by clicking cells in the preview. See the grid immediately and copy ready-made CSS and HTML with one click. Everything runs in your browser and no value is ever sent to a server.

Grid settings

Columns
Rows
Gaps
Alignment

As long as a setting stays on its default value it is left out of the output, so the code never fills up with lines that do nothing.

Rows sized in fr only become visible once the container has a height. This slider affects the preview only and never reaches the output.

Preview

Placed items

No items yet. In the preview, click the start cell first and then the end cell.

Veri Analitiği

What CSS Grid actually does

Setting display: grid on an element creates a grid formatting context. Its direct children stop stacking in normal flow and are placed instead on a two dimensional network of lines formed by the column and row tracks you define. That is the real difference from Flexbox: Flexbox distributes along one axis, Grid defines both axes at once.

The skeleton comes from grid-template-columns and grid-template-rows. Both take a space separated list of track sizes, one value per column or row. Numbered lines appear between the tracks, so a three column grid has four vertical lines numbered 1 to 4. The numbers you write when placing an item refer to those lines, not to the columns, which is why grid-column: 1 / 3 spans two columns rather than three.

Track units and what fr really distributes

A track can be a fixed length (px, rem), a percentage of the container, auto, which grows with its content, min-content, the narrowest the content can get, or max-content, the widest it wants to be without wrapping. All of those are measurable sizes.

fr behaves differently and is the unit people misread most often. It is not a length but a share, and it divides up the free space only, meaning whatever is left after the inflexible tracks and all the gaps have been subtracted. So in 200px 1fr 1fr each flexible track gets half of the remainder, not half of the container.

minmax() and the responsive grid idiom

minmax(min, max) puts a floor and a ceiling on a track. It never shrinks below the floor, never grows past the ceiling, and moves freely in between according to content and available space. It is useful on its own, but it earns its reputation in combination with repeat().

Anyone who wants a responsive card grid without media queries reaches for repeat(auto-fit, minmax(240px, 1fr)). The browser works out how many tracks of at least 240px fit in the container, creates them, and hands the leftover width to those tracks through 1fr. As the viewport narrows, the column count drops on its own.

The difference between auto-fit and auto-fill only shows up when you have fewer items than tracks. auto-fill keeps the empty tracks in the grid, so three cards still sit in an eight column layout and huddle on the left. auto-fit collapses the empty tracks to zero, so the remaining cards stretch to fill the row. Pick auto-fit when you want cards to grow, auto-fill when you want the column rhythm to stay put.

Line based placement or named areas

There are two ways to place an item. The first is by line number: grid-column: 2 / 4 runs the item from vertical line 2 to vertical line 4. The span keyword lets you say "cover two columns wherever you land" instead of naming both lines.

The second is grid-template-areas, where you draw the layout on the container as a picture made of names and then give each child a name with grid-area. For page skeletons built from a handful of meaningful regions this makes the CSS readable, and rearranging the layout is a matter of redrawing the picture. For repeating content of unknown length, such as a card list, named areas get unwieldy fast and line numbers are the better tool.

Implicit tracks, grid-auto-rows and dense packing

When you place more items than you declared tracks for, the browser generates extra ones. These are implicit tracks and they are sized auto by default. Use grid-auto-rows to control their height, or grid-auto-flow: column if new items should open columns instead of rows.

grid-auto-flow: dense backfills the holes that differently sized items leave behind by pulling later, smaller items forward. It closes the visual gaps, but it also detaches the visual order from the DOM order, which is disorienting for keyboard and screen reader users. Fine for a gallery where order carries no meaning, best avoided for content that is meant to be read in sequence.

justify versus align, items versus content

Two axes: justify-* controls the inline axis, which is horizontal in a left to right document, and align-* controls the block axis, which is vertical. Two scopes: *-items positions every item inside its own cell, while *-content positions the whole set of tracks inside the container. The second one does nothing unless the tracks are smaller than the container, so justify-content has no visible effect when your columns are sized in fr and already consume every pixel. That is also why this generator leaves out any alignment declaration still sitting at its initial value: a line that does nothing only misleads the next reader.

Grid or Flexbox?

Aspect CSS Grid Flexbox
Axes Rows and columns together One axis at a time
Who defines the layout The container, up front The items, from their content
Typical job Page skeleton, card grid, table like layout Toolbar, button group, tag list
Alignment Both axes at once Distribution on the main axis, alignment on the cross axis
Unknown item count auto-fit adjusts the track count wrap pushes items onto the next line

The practical rule: build the outer frame with Grid and lay out the small, single axis clusters inside it with Flexbox. They are not rivals, they are two layers you nest.

Three mistakes worth knowing

If the layout sits on a colour transition, the value produced by the CSS gradient generator drops straight onto the container you build here.

How do you create a CSS Grid?

  1. 1

    Set up your columns and rows

    Enter how many columns and rows you want, then pick a unit for each track: fr, px, percent, auto, min-content, max-content or minmax. When tracks repeat, the tool shortens the output with repeat() by itself.

  2. 2

    Adjust the gaps and the alignment

    Drag the row-gap and column-gap sliders; while the two values match, the output uses the single line gap shorthand. Any justify or align choice left on its default is never written to the code, so the CSS does not fill up with useless lines.

  3. 3

    Place items by clicking cells

    In the preview, click the start cell first and then the end cell; the rectangle between them becomes a grid item. You can name each item, give it a colour, and remove it from the list with one click.

  4. 4

    Copy the CSS and the HTML

    The container rule and a separate rule for every item are written as you go, and the HTML block beside them uses the same class names. Copy the two panels separately and paste them into your project as they are.

FAQ

An fr is a share of the free space, not of the container's total width. The browser first subtracts the inflexible tracks (px, %, auto, min-content and so on) along with every gap, then splits what remains according to the fr coefficients. So in 200px 1fr 1fr each flexible column gets half of the remainder, not half of the container.

Both work out how many tracks fit in the container. The difference appears when you have fewer items than tracks: auto-fill keeps the empty tracks, so items bunch to the left and space is left over on the right. auto-fit collapses the empty tracks to zero, so the existing items stretch and fill the row. Choose auto-fit when cards should grow, auto-fill when the column rhythm should stay fixed.

Almost always for the same reason: 1fr means minmax(auto, 1fr), and that auto floor keeps the track from shrinking below its content. An unbreakable string, a wide table or a box with overflow: auto inside will push the column past the container. Change the track to minmax(0, 1fr), or set min-width: 0 on the child.

Both produce the same result, so the choice is about readability. For page skeletons made of a few nameable regions, grid-template-areas draws a picture of the layout in the CSS and rearranging it means redrawing that picture. For card lists of unknown length, named areas get unwieldy and line numbers with span are the more practical tool.

justify-items aligns each item horizontally inside its own cell. justify-content takes the whole set of tracks as one block and aligns that block inside the container. The second only does something when the tracks are narrower than the container, so it has no visible effect once your columns are sized in fr and consume all the space. align-items and align-content are the vertical counterparts.

No. gap only opens space between tracks: it adds nothing at the outer edge of the grid and nothing before the first or after the last item. A margin belongs to the item itself, can collapse against a neighbour, and leaves a stray gap after the last one. When you want space around the grid, put padding on the container.

No, they solve different problems. Grid defines a two dimensional layout up front on the container, which suits page skeletons, card grids and table like arrangements. Flexbox works on one axis and lets the items' own content drive the result, which suits toolbars, button groups and tag lists. The common practice is to build the outer frame with Grid and lay out the small clusters inside it with Flexbox.

Coderspace is always with you in your tech career!

Learn, code, compete, get hired!

Sign up now