Skip to main content
ZeroUtil

CSS Grid Generator

Build CSS Grid layouts visually with live preview. Works as a grid system generator and HTML grid generator.

Maintained by

1
2
3
4
5
6
7
8
9
3
3
10px
10px
Content Areas

No content areas defined. Add an area to create spanning cells.

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

Laying Out a Grid

  1. Set column and row counts with the sliders, or type a custom template like 1fr 2fr 1fr or repeat(3, minmax(200px, 1fr)) directly into the template fields. The text inputs accept any valid grid-template-columns / grid-template-rows value.
  2. Configure column and row gaps. These map to the column-gap and row-gap properties - pixel-precise whitespace between tracks without adding margin to individual cells.
  3. Click "+ Add Area" to define a named region using the grid-template-areas system. Each area spans from its column/row start to its column/row end line.
  4. Drag area bounds on the preview to adjust which lines each region touches, or type line numbers directly.
  5. Copy CSS. Output contains the full grid container declaration plus per-area grid-area rules ready for your stylesheet.

How the grid layout algorithm runs

CSS Grid comes from CSS Grid Layout Module Level 1 and is the only two-dimensional layout system built into CSS. When you set display: grid, the browser runs a track-sizing algorithm that converts your template values into concrete pixel sizes for every column and row. The algorithm passes through three phases: first it resolves fixed-length tracks (pixels, ems), then it distributes remaining space proportionally across fr-unit tracks, and finally it auto-sizes any auto or min-content/max-content tracks based on their contents.

The fr unit is what makes grid feel natural: one fr equals one fractional share of the leftover space after fixed tracks are subtracted. grid-template-columns: 200px 1fr 2fr on a 1000px container becomes "200px, 267px, 533px" at runtime. minmax(200px, 1fr) gives a track a floor of 200px that grows to take its fraction only when space allows. All of this runs during layout, not paint, so the browser caches the computed track sizes and only re-runs the algorithm when the container size, template, or content dimensions change. That\'s why resizing a grid container is cheap, but re-sorting grid items via JavaScript triggers a full layout pass.

Typical real uses for grid

  • Page-level layouts with header, sidebar, main content, and footer as named areas.
  • Responsive card galleries using grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)) - cards flow and resize without media queries.
  • Form layouts where labels and inputs align across rows with grid-template-columns: auto 1fr.
  • Dashboard panels with widgets that span multiple cells, each positioned with grid-column / grid-row shorthand.
  • Photo mosaics mixing portrait and landscape cells using grid-auto-flow: dense to back-fill gaps.
  • Magazine-style editorial grids where named areas drive a deliberate, non-uniform reading flow.

Edge cases and pitfalls

  • Implicit vs explicit tracks. If you place an item at grid-row: 10 but only defined 3 explicit rows, the browser creates 7 implicit rows of grid-auto-rows size - auto by default. That can produce zero-height tracks that look like layout bugs. Always set grid-auto-rows: minmax(50px, auto) as a safety floor.
  • Line-number counting starts at 1, not 0. A 3-column grid has lines 1, 2, 3, 4. grid-column: 1 / 4 spans all three columns. Off-by-one errors are the #1 source of grid frustration.
  • Named areas must form rectangles. grid-template-areas strings are parsed into a 2D matrix, and any L-shaped or disconnected area name is silently ignored. Validate your ASCII-art carefully.
  • Gaps don\'t count as lines. The gap is rendered between tracks but doesn\'t add line numbers, so item positioning math stays clean.
  • Percentages in gaps resolve against the container size, not the content. gap: 5% on a 1000px container gives 50px gaps - useful for proportional spacing, sometimes surprising when the container is fluid.
  • Grid items can overlap by explicit placement - two items at the same grid-area stack, and z-index controls the order. This is a feature (for layered layouts), not a bug, but it catches people expecting flexbox-like collision avoidance.
  • subgrid has limited support. grid-template-columns: subgrid lets nested grids inherit tracks from a parent. Firefox shipped it in 71 (2019); Chrome took until version 117 (2023); Safari caught up in 16. Check caniuse.com/css-subgrid before relying on it.

Background on the spec (grid system generator and HTML grid generator)

CSS Grid Layout Module Level 1 was published as a W3C Candidate Recommendation in 2017 after years of spec work driven by Microsoft\'s Bo Cupp and Rachel Andrew. Level 2 added subgrid; Level 3 adds masonry-style auto-placement, currently shipping behind flags. Grid was designed to complement flexbox: flex handles one-dimensional layouts, grid handles two-dimensional ones. The fr unit, named areas, and the auto-fit/auto-fill keywords are all grid-specific. Browser compatibility per caniuse.com/css-grid sits above 98% globally - Chrome 57+, Firefox 52+, Safari 10.1+, Edge 16+ - so grid is production-safe everywhere except ancient embedded browsers.

Grid vs flexbox vs tables

Grid, flex, and HTML tables all arrange content in rows and columns, but they answer different questions. Grid is content-out: you define a track skeleton and place items into specific cells, giving maximum control over alignment across both axes. Flexbox is content-in: a row or column of items negotiates its sizes based on content. Flex is perfect for navbars, button groups, and toolbars; grid is perfect for page layouts and multi-axis alignments. HTML tables remain the right tool for tabular data - sortable columns, cell relationships that screen readers announce. Compared to Bootstrap\'s 12-column grid or Tailwind\'s grid-cols-* utilities, writing raw CSS Grid gives you arbitrary templates and named areas that utility classes struggle to express. For simple card layouts, a framework is fine; for anything bespoke, native grid CSS is more maintainable.

Frequently Asked Questions

What does the fr unit actually mean?

Fraction unit. One <code>fr</code> represents one equal share of the space left over in the grid container after all fixed-length tracks, gaps, and intrinsic-sized tracks are subtracted. In <code>grid-template-columns: 100px 1fr 2fr</code> with a 700px container, the 100px track takes its share, and the remaining 600px is split 1:2 between the two <code>fr</code> tracks (200px and 400px). The <code>fr</code> unit is grid-exclusive - it has no meaning outside a grid container.

When should I use grid-template-areas vs line-based placement?

Named areas are best for top-level page layouts where the structure is easy to visualize as an ASCII grid: header, sidebar, main, footer. Line-based placement (<code>grid-column: 2 / 5</code>) is better for fine-grained component layouts where you need precise spans, overlapping items, or dynamic positioning. Named areas make intent obvious to future readers; line-based placement gives you mathematical control. Use both in the same stylesheet - they don't conflict.

How do grid and flexbox differ in practice?

Flexbox arranges items along a single axis (row or column) and lets them negotiate sizes based on content. Grid arranges items in a two-dimensional track structure where you define the skeleton first. Flex is content-driven (the items shape the container); grid is container-driven (the container shapes the items). For a navigation bar, flex is right. For an entire page layout with header, sidebar, and main, grid is right. They compose cleanly: a grid container with flex children inside each cell is a very common pattern.

What is auto-fit vs auto-fill for responsive grids?

Both create as many tracks as fit in the container. <code>auto-fill</code> keeps empty tracks at the end (items stay their minimum size even if there's extra space). <code>auto-fit</code> collapses empty tracks and stretches the filled ones to use the full space. For a card grid with <code>minmax(240px, 1fr)</code>, <code>auto-fit</code> produces full-width cards when only a few items are present, while <code>auto-fill</code> leaves them at 240px with the extra space distributed as gap. Choose based on whether items should always fill the container or always stay their natural size.

Does this generator communicate with any server?

No. Column and row templates, gap values, area definitions, and every other configuration live in component state locally. The CSS output is built with template literals inside the Preact component, and the preview renders with inline styles applied to DOM nodes. There is no API call tied to your grid configuration.

What is subgrid and when should I use it?

Subgrid lets a nested grid inherit the track definitions from its parent, so child grids align their columns or rows to the parent's lines. Declare <code>grid-template-columns: subgrid</code> on the nested grid. Browser support arrived late (Firefox 71, Safari 16, Chrome 117) so check caniuse.com/css-subgrid before using it in production.

How do I make a responsive grid without media queries?

Combine <code>repeat()</code>, <code>auto-fit</code> (or <code>auto-fill</code>), and <code>minmax()</code>: <code>grid-template-columns: repeat(auto-fit, minmax(240px, 1fr))</code>. Items will be at least 240px wide and stretch to share available space; the browser automatically adjusts the column count as the container resizes. This one declaration replaces a handful of media queries for card-gallery layouts.

What causes weird empty rows in my grid?

Usually implicit tracks. When you place an item with <code>grid-row: 5</code> but only defined 3 explicit rows, the browser generates 2 implicit rows to reach row 5, each sized by <code>grid-auto-rows</code> (default <code>auto</code>, meaning they take the height of their content - often zero for empty rows). Set <code>grid-auto-rows: minmax(auto, max-content)</code> or use named-area templates.

Is this the same as an HTML grid generator or a grid system generator?

Yes. "HTML grid generator", "grid system generator", "grid generator css", "generator grid" and "generate grid" are all common ways people search for a tool that writes <code>display: grid</code> CSS for an HTML container. This page fits every variant: it outputs a standards-based CSS Grid declaration plus optional <code>grid-template-areas</code>. If you want a pre-built 12-column system like Bootstrap or a utility-first framework like Tailwind, those are layered on top of native CSS Grid - start here if you want direct control without the framework overhead.

More CSS & Design