CSS Grid Generator
Build CSS grid layouts visually — copy the generated code instantly
Column Widths
Row Heights
Generated CSS
.grid-container {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr;
gap: 16px;
}
.grid-item {
/* add your item styles here */
}Live Preview (9 cells)
🔒 No upload — runs entirely in your browser. Your data never leaves your device.
Related Tools
CSS Flexbox Generator
Build CSS flexbox layouts visually — copy the generated CSS instantly.
Box Shadow Generator
Build CSS box shadows visually with multiple layers and presets.
CSS Gradient Generator
Build linear, radial and conic CSS gradients visually.
PX to REM Converter
Convert pixels to rem with configurable base font size.
Color Contrast Checker
Check WCAG AA and AAA color contrast ratios.
What is CSS Grid?
CSS Grid is a two-dimensional layout system built directly into CSS that lets you define rows and columns and place elements into them precisely. Before CSS Grid, developers relied on floats, inline-blocks, and table hacks to achieve multi-column layouts — techniques that were brittle, verbose, and required reordering the HTML to match the desired visual output. CSS Grid eliminates all of that by separating the layout concern from the content concern: you define the grid in CSS, then assign elements to grid tracks without touching the HTML structure.
A CSS grid is defined on a container element using display: grid. The grid-template-columns and grid-template-rows properties establish the column and row tracks. Child elements automatically fill these tracks in document order, or you can explicitly place them using grid-column and grid-row.
CSS Grid has over 97% browser support across all modern browsers and is fully supported in Chrome, Firefox, Safari, and Edge. It is the W3C-recommended approach for two-dimensional layout and is used in the design systems of companies like GitHub, Stripe, and Shopify.
How to Use This CSS Grid Generator
- 1.Set the number of columns (1–12) and rows (1–6) using the sliders or plus/minus buttons. The live preview updates immediately.
- 2.Adjust each column width and row height in the size inputs. Type any valid CSS value —
1fr,200px,auto,minmax(100px,1fr)— or pick a preset from the dropdown. - 3.Set the column gap and row gap with the sliders. When both values are equal, the generator outputs the shorthand
gapproperty. - 4.Choose
align-itemsto control vertical alignment within cells, andjustify-itemsto control horizontal alignment. - 5.Click Copy CSS to copy the generated code to your clipboard and paste it directly into your stylesheet.
Understanding grid-template-columns
grid-template-columns is the most important CSS Grid property. It defines how many columns your grid has and how wide each one is. The value is a space-separated list of track sizes — one size per column. You can use virtually any CSS length unit, but the most common values are:
1fr— one fraction of available space.1fr 1fr 1frcreates three equal columns.200px— a fixed pixel width that does not respond to container size.auto— sizes the column to its content width.repeat(3, 1fr)— shorthand for1fr 1fr 1fr; avoids repetition when you have many equal columns.minmax(100px, 1fr)— each column is at least 100px wide and can grow to fill available space.
A common pattern for sidebar layouts is 250px 1fr: a fixed 250px sidebar and a flexible main content area. For a Holy Grail layout with two sidebars, use 200px 1fr 200px. You can mix units freely — 200px repeat(3, 1fr) 200px creates a fixed sidebar on each end with three equal center columns.
The fr Unit: Flexible Layout Made Simple
The fr unit (fractional unit) is unique to CSS Grid and is how the spec solves the problem of proportional space distribution. After all fixed-width columns and gaps are subtracted from the container's total width, the leftover space is divided among the fr columns according to their ratio.
Example: a 900px container with three columns of 1fr 2fr 1fr and a 16px gap between each. The two gaps consume 32px, leaving 868px. The total fr count is 4, so each fr is worth 217px. Column 1 gets 217px, column 2 gets 434px, column 3 gets 217px.
The fr unit is why CSS Grid replaced the percentage-based approaches of the Bootstrap era. With percentages, you had to manually subtract gutters and account for box-sizing. With fr, the browser handles all of that math automatically. The equivalent of a 12-column Bootstrap grid is simply repeat(12, 1fr).
Grid Template Rows and Row Heights
grid-template-rows works identically to grid-template-columns, but for the vertical axis. The most common row value is auto, which means rows size to their content — the row is as tall as the tallest item it contains. This is the default behavior when you omit grid-template-rows entirely.
Set rows to 1fr when you want all rows to be equal height and fill the container. This is useful for full-height layouts such as dashboards. Use minmax(100px, auto) to set a minimum row height while still allowing rows to grow with their content — useful for card-based layouts where some cards may have more text than others but you want a consistent minimum size.
A classic page layout uses three rows: auto 1fr auto. The header (auto) sizes to its content, the main content area (1fr) expands to fill all remaining height, and the footer (auto) sizes to its content — achieving a sticky footer with a single CSS property.
Gap, Column Gap, and Row Gap
The gap property (formerly grid-gap) sets the space between grid tracks. It does not add space at the outer edges of the grid — only between cells. This makes it simpler than adding margins or padding to each cell, which could cause alignment issues at the edges of the container.
gap: 16px sets both column and row gaps to 16px. To set them independently, use column-gap and row-gap. This generator automatically uses the shorthand gap when both values are equal, and outputs separate column-gap and row-gap properties when they differ. Common design gap values are 8px (tight), 16px (standard), 24px (spacious), and 32px (magazine-style).
Aligning Grid Items with align-items and justify-items
CSS Grid has two axes of alignment for items within their cells. align-items controls the vertical (block) axis, and justify-items controls the horizontal (inline) axis. Both accept the same four values:
- stretch (default) — the item stretches to fill the entire cell in that axis.
- start — the item aligns to the top (align) or left (justify) edge of the cell.
- center — the item centers in the cell on that axis.
- end — the item aligns to the bottom (align) or right (justify) edge of the cell.
The shorthand place-items: center center — or simply place-items: center — centers all items in both axes simultaneously. This is the cleanest way to create a centered icon grid. These properties control item alignment within cells; use align-content and justify-content if you want to align the grid tracks themselves within the container.
CSS Grid vs. Flexbox: Choosing the Right Tool
The most common confusion among developers new to modern CSS is when to reach for CSS Grid versus Flexbox. The simple rule: use Flexbox for one-dimensional layouts (a single row of buttons, a horizontal navigation bar, a column of stacked form fields) and use CSS Grid for two-dimensional layouts (a dashboard with rows and columns, a photo gallery, a page template with header/sidebar/main/footer regions).
The key structural difference: in Flexbox, items flow along one axis and can wrap to a new line, but each line is independent — items in two different lines have no relationship to each other. In CSS Grid, items in different rows and columns can align with each other because the grid tracks are shared. This is why consistent card heights in a product listing require CSS Grid: the tracks enforce that all cards in the same row reach the same height, regardless of their content.
The two systems are complementary, not competing. Use CSS Grid to create the macro layout of a page or component, then use Flexbox inside individual grid cells to arrange content within them. A navigation header, for example, might use CSS Grid for the three-column layout (logo | nav links | CTAs) but Flexbox inside each cell to vertically center and space the individual items.
Responsive Grids Without Media Queries
One of CSS Grid's most powerful features is intrinsic responsiveness — creating layouts that adapt to the container width without any @media queries. The technique uses repeat(auto-fit, minmax(MIN, 1fr)):
auto-fit— creates as many column tracks as fit, then collapses any empty tracks.minmax(200px, 1fr)— each column is at least 200px wide and stretches to fill available space.
The result: on a 1200px screen, a minmax(200px, 1fr) grid shows 5–6 columns. On a 600px screen, it shows 2–3 columns. On a 320px mobile screen, it shows 1 column. Zero media queries, zero breakpoint math. This is the right starting point for most product card grids, team photo galleries, and feature highlight sections.
If you want items to maintain equal-width columns without the last row stretching to fill empty space, use auto-fill instead of auto-fit. With auto-fill, empty column tracks are preserved at the original minimum width, keeping the columns consistent across all rows.
Common CSS Grid Layout Patterns
Sidebar layout
grid-template-columns: 260px 1fr;Fixed left sidebar, flexible main content. Swap to 1fr 260px for a right sidebar.
Holy Grail (header, two sidebars, main, footer)
grid-template-columns: 200px 1fr 200px;
grid-template-rows: auto 1fr auto;Three-column page template with header and footer spanning all columns via grid-column: 1 / -1.
Card grid (responsive)
grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));Adapts from 1 column on mobile to 4+ columns on wide screens. No media queries.
Dashboard grid
grid-template-columns: repeat(12, 1fr);
grid-template-rows: repeat(6, 100px);12-column, 6-row dashboard base. Widgets span multiple columns and rows using grid-column: span 4.