Free CSS Grid Generator
The CSS Grid Generator builds a grid layout visually. Set the number of columns and rows, adjust equal or auto sizing and set gaps, then copy the generated CSS and a live preview.
The one-dimensional alternative is the CSS Flexbox Generator, and the spacing units are handled by the PX to REM / EM Converter and PX to VW / VH Converter. For the breakpoints between layouts, the Screen Resolution Checker reports the viewport sizes to target.
How to use the Grid Generator
- 1 Set the number of columns and rows.
- 2 Choose fixed, fractional or auto sizing for each track.
- 3 Set the gap and decide how items should place themselves.
- 4 Copy the generated declarations.
Building a responsive grid
The most useful grid pattern uses no media queries at all: a repeated column definition with a minimum track size and a fractional maximum. The browser fits as many columns of at least that width as the container allows, and redistributes the remainder. The layout reflows purely from the container width.
The minimum size is the important parameter. Set it to the smallest width a card can be without its content breaking, and the grid handles every viewport from there. Getting it wrong produces either cramped content on small screens or an unnecessary wrap on large ones.
- `repeat(auto-fill, minmax(20rem, 1fr))` is the responsive default.
- The `fr` unit divides remaining space after fixed tracks.
- Gaps are not counted as tracks and do not consume fractional space.
- Grid rows align across all columns, which flexbox cannot do.
Frequently asked questions
What does a fractional unit mean?
The `fr` unit represents a share of the free space in the grid after fixed tracks and gaps are accounted for. Two columns sized one fractional unit each will be equal, and a column of two fractional units will be twice as wide as one of them. It is what makes grids respond to available space without media queries.
What is the difference between `auto-fill` and `auto-fit`?
Both create as many tracks as will fit. `auto-fill` keeps empty tracks, so items stay their intended size and the row has empty space at the end. `auto-fit` collapses empty tracks to zero, so the remaining items stretch to fill the row. Which you want depends on whether you would rather items be larger or stay consistent.
When should I use a grid rather than flexbox?
When the layout has to align in two dimensions. A row of cards where the cards must be the same height is a grid problem, because grid rows align across columns by construction. Alignment in both axes is what grid is for, and flexbox is not a substitute for it.
Related Developer Tools
You might also need
Tools from other categories that people pair with the Grid Generator: