Visual CSS grid builder for rows, columns, gaps, and alignment. Customize grid-template and copy CSS instantly.
.grid-container {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr;
gap: 16px;
justify-items: stretch;
align-items: stretch;
justify-content: stretch;
align-content: stretch;
}CSS Grid is a two-dimensional layout system built into the browser. Unlike Flexbox (one-dimensional), Grid lets you define rows and columns at once with grid-template-columns and grid-template-rows. You place items into cells, control spacing with gap, and align content with justify-items, align-items, justify-content, and align-content. Grid is ideal for dashboard layouts, card grids, and page structures where you need explicit control over both axes.
Apply display: grid to a container. Define the track structure: grid-template-columns: 1fr 1fr 1fr creates three equal columns; 1fr is one fraction of the available space. Use gap (or row-gap, column-gap) for spacing. Child elements flow into the grid automatically by row, unless you assign grid-column and grid-row for explicit placement.
Grid is two-dimensional (rows + columns); Flexbox is one-dimensional (row or column). Use Grid when you need to align items in both directions, such as dashboard panels or card galleries. Use Flexbox for navbars, button groups, or single-row/column layouts. Both can be nested: a Grid cell can contain a Flex container. Pair Grid with tools like the Spacing Scale Assistant for consistent gap values, or the Border Radius Generator and Box Shadow Generator for styling grid cards.
| Aspect | CSS Grid | Flexbox |
|---|---|---|
| Dimensions | 2D (rows + columns) | 1D (row or column) |
| Best for | Page layout, dashboards, galleries | Navbars, button groups, alignment |
| Content-driven | No, structure is explicit | Yes, wraps based on content |
| Overlap / placement | Easy with grid-column/row | Harder, requires tricks |
Common patterns include: equal columns (1fr 1fr 1fr or repeat(3, 1fr)), sidebar layouts (250px 1fr), and responsive galleries using repeat(auto-fit, minmax(200px, 1fr)). For cards or dashboard widgets, combine Grid with consistent gaps and alignment options. Use grid-template-areas when you need named regions for cleaner markup.
Use media queries to change grid-template-columns at breakpoints: e.g. 1fr on mobile, 1fr 1fr at 768px, 1fr 1fr 1fr at 1024px. Alternatively, repeat(auto-fit, minmax(200px, 1fr)) creates a fluid grid that adds or removes columns based on container width, without media queries.
repeat(n, 1fr) creates n equal tracks. minmax(min, max) sets a minimum and maximum size for a track: minmax(200px, 1fr) means at least 200px, up to one fraction of remaining space. auto-fit fills the row with as many tracks as fit; empty tracks collapse. auto-fill behaves similarly but reserves space for empty tracks. Use auto-fit when you want items to grow into empty space; use auto-fill when you want consistent track sizing.
Defining only grid-template-columns without rows can create implicit rows that behave unexpectedly. Using fr with fixed tracks (200px 1fr 1fr) is fine, but mixing fr and % can cause overflow. Forgetting gap leaves items flush; use consistent spacing. Overusing nested grids when a single grid with grid-column spans would suffice increases complexity.
CSS Grid is a layout system that uses rows and columns. You define grid-template-columns and grid-template-rows (e.g. 1fr 1fr 1fr for three equal columns), then place items in the grid. gap controls spacing between cells.
1fr is one fraction of the available space. So 1fr 1fr 1fr divides the space into three equal columns. 2fr 1fr would make the first column twice as wide as the second.
Use media queries to change grid-template-columns and grid-template-rows at different breakpoints. For example: at 768px switch from 1fr 1fr 1fr to 1fr 1fr, or use repeat(auto-fill, minmax(200px, 1fr)) for auto-flow.
Explore these related free tools to enhance your productivity and workflow.
Visual tool for CSS clip-path shapes with presets and custom points
Create custom border radius values for all corners with individual or locked controls
Generate spacing systems (4, 8, 12, 16px, etc) and export CSS variables
Generate CSS box shadows with custom offsets, blur, spread, color, and opacity