CSS Grid Generator

Visualize and generate CSS Grid layouts with real-time preview. Experiment with container and item properties to master grid layouts.

Settings

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
CSS Code

What is CSS Grid?

CSS Grid is a two-dimensional layout system—handles rows and columns at the same time. Set display: grid on a container and you can define explicit grid tracks, place items anywhere, overlap elements.

Unlike Flexbox which is one-dimensional (either row or column), Grid excels at full page layouts. Think dashboards, image galleries, magazine-style designs.

The syntax takes a bit to learn, but once it clicks, you'll build layouts in minutes that used to take hours.

Perfect complement to Flexbox—use Grid for overall page structure, Flex for component internals.

How do grid-template-columns and fr units work?

Grid-template-columns defines your column tracks. Could be fixed: grid-template-columns: 200px 200px 200px for three 200px columns. Or flexible: grid-template-columns: 1fr 2fr 1fr for proportional sizing. The fr unit means "fraction of available space."

Mix units freely: 200px 1fr 1fr gives you a fixed sidebar and two equal flexible columns. Use repeat() for patterns: repeat(3, 1fr) instead of 1fr 1fr 1fr. Way cleaner.

What's the difference between Grid and Flexbox?

Grid is two-dimensional—rows and columns together. Flexbox is one-dimensional—either rows or columns. That's the core difference.

Use Grid for:

  • Page layouts, complex arrangements
  • Calendar layouts, dashboards, photo galleries
  • Anything that needs precise placement in both directions

Use Flexbox for:

  • Components, navigation bars, centering
  • Dynamic content where you don't know the size
  • One-directional layouts

They work great together. Not an either/or situation. I use both in every project—Grid for macro layout, Flex for micro adjustments.

How do I create responsive grids?

The auto-fit and auto-fill keywords with repeat() are magic: grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)). This creates as many columns as fit, each at least 250px, growing to fill space. No media queries needed.

Auto-fit collapses empty tracks and stretches filled ones. Auto-fill keeps empty tracks around. Usually auto-fit is what you want—items expand to use available space.

Add media queries for bigger changes: switch from three columns on desktop to one on mobile.

Use gap for spacing between items instead of margins—way cleaner. The minmax() function is your friend for constraining track sizes while staying flexible.

How does the gap property work in Grid?

Gap adds space between grid items—both rows and columns. Set it once on the container: gap: 1rem for equal spacing both ways. Or separately: row-gap: 1rem; column-gap: 2rem. Unlike margins, gap only appears between items, not on the outer edges.

Gap also works with Flexbox now—super handy for spacing flex items without margins. Makes layouts cleaner and easier to adjust. Change one value, all spacing updates.

Can I overlap items with Grid?

Absolutely. Grid items can occupy the same cells—just position them there and they'll stack. Use grid-column and grid-row to place items: grid-column: 1 / 3; grid-row: 1 / 2;.

Multiple items targeting the same area? They overlap. Control stacking with z-index. Great for text over images, decorative elements, complex designs. No absolute positioning hacks needed.

Named grid areas make this even easier: define areas with grid-template-areas, then assign items with grid-area: header.