UseToolSuite UseToolSuite

CSS Grid Generator

Free online CSS Grid generator with live visual preview. Set columns, rows, gaps, and alignment — get clean, copy-ready CSS and HTML instantly.

Grid Structure

3
2
16px
16px

Track Sizing

Alignment

Grid vs Flexbox: the one-line rule

Both lay out elements, but they’re built for different jobs:

  • Grid is two-dimensional — you control rows and columns at once. Reach for it for page layouts, dashboards, card galleries, and anything with structure in both axes.
  • Flexbox is one-dimensional — items flow in a single row or column. Reach for it for toolbars, button groups, and aligning a component’s contents.

A useful heuristic: Grid for the macro layout of the page, Flexbox for the micro alignment inside components. They compose well — a grid cell is often a flex container.

The fr unit and responsive grids without media queries

The fr (fraction) unit splits leftover space proportionally: grid-template-columns: 1fr 2fr 1fr gives the middle column twice the width of the outer two. Combined with repeat(), minmax(), and auto-fit, you get responsiveness for free:

grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));

This fits as many ≥250px columns as the container allows and wraps to fewer on narrow screens — no breakpoints required. (auto-fit collapses empty tracks; auto-fill keeps them.)

Common pitfalls

SymptomCause & fix
Items overflow the containerFixed px columns exceed width — use minmax(0, 1fr)
fr rows have no heightContainer has no height — give it one (100vh, etc.)
Item won’t span as expectedspan exceeds track count, or implicit tracks appear

The minmax(0, 1fr) trick deserves a highlight: a bare 1fr track has a minimum size of its content, so a wide child (a long word, a <pre>) can blow out the layout. The 0 minimum lets the track shrink and prevents the overflow.

Build it visually

Set columns, rows, gaps, and alignment with the live preview here to get clean copy-ready CSS, then layer on the techniques above — name your areas for readable page layouts, use auto-fit + minmax for responsive card grids, and reach for subgrid when nested components need to align. To fine-tune track sizes in rem vs px, the CSS Units Converter pairs well.

Last updated Built and maintained by Necmeddin Cunedioglu How tools are tested

How helpful was this tool?

Click to rate

Key Concepts

Grid Container

The element with display: grid applied. All direct children of a grid container become grid items. The grid container establishes a new grid formatting context and defines the grid's column and row structure via grid-template-columns and grid-template-rows.

Grid Track

A single row or column in a CSS Grid. Grid tracks are defined by grid-template-columns (for column tracks) and grid-template-rows (for row tracks). The space between grid lines is a track. The fr unit distributes available space proportionally among tracks.

Grid Line

The dividing lines that make up the structure of the grid. Lines run horizontally (row lines) and vertically (column lines). They are numbered starting from 1. You reference grid lines in grid-column and grid-row properties to position or span items across the grid.

Implicit Grid

Grid tracks automatically created when items are placed outside the explicitly defined grid. If you have a 3×2 grid but place an item in row 4, the browser creates implicit row tracks to accommodate it. Use grid-auto-rows and grid-auto-columns to control the size of these implicitly created tracks.

fr Unit

A flexible length unit unique to CSS Grid that represents a fraction of the available space in the grid container. After accounting for fixed-size and auto-size tracks, the remaining space is divided among fr-unit tracks in proportion to their fr values. 1fr 2fr 1fr divides space into 25%, 50%, and 25% respectively.

Frequently Asked Questions

What is CSS Grid and when should I use it?

CSS Grid is a two-dimensional layout system that lets you place elements in rows and columns simultaneously. Use Grid when your layout has a defined structure in both axes — like page layouts, card grids, dashboards, and image galleries. Use Flexbox when you're laying out items in a single row or column with flexible sizing. Grid is better for macro layout; Flexbox is better for component-level alignment.

What does the "1fr" unit mean in CSS Grid?

The "fr" (fraction) unit represents a fraction of the available space in the grid container. With grid-template-columns: repeat(3, 1fr), each column gets exactly one-third of the container width. With 1fr 2fr 1fr, the middle column gets twice as much space as each of the outer columns. The fr unit distributes remaining space after fixed-size and auto-size columns are accounted for.

What is the difference between gap, row-gap, and column-gap?

The gap shorthand sets spacing between grid tracks. gap: 16px sets both row and column gaps to 16px equally. gap: 16px 24px sets row-gap to 16px and column-gap to 24px. Use row-gap and column-gap for independent control. Note: gap only applies between tracks — it does not add space on the outer edges of the grid container. Use padding on the container for outer spacing.

How do justify-items and align-items work in CSS Grid?

justify-items controls how grid items are aligned horizontally within their cells (along the inline/row axis). align-items controls vertical alignment within cells (along the block/column axis). Both default to "stretch", which makes items fill their entire cell. Set them to "start", "end", or "center" to control how items sit within their allocated grid area. Use justify-self and align-self on individual items to override per-item.

Can I make a responsive grid without media queries?

Yes. Use auto-fill or auto-fit with minmax() to create a responsive grid that automatically adjusts the number of columns: grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)). This creates as many 250px+ columns as fit in the container, automatically wrapping to fewer columns on smaller screens — no media queries needed. auto-fill creates empty tracks if there are fewer items; auto-fit collapses empty tracks to zero.

What is the difference between grid-template-rows: auto vs 1fr?

auto makes a row height equal to the tallest content in that row — the row shrinks or grows with its content. 1fr divides the available height equally among rows, giving each row an equal share of the container height. Use auto for most content-driven layouts. Use 1fr when you want rows to fill the full container height equally, such as in full-height application layouts.

Is the generated CSS compatible with all browsers?

Yes. CSS Grid is supported in all modern browsers: Chrome 57+, Firefox 52+, Safari 10.1+, and Edge 16+. This covers over 97% of global users as of 2024. The generated CSS uses standard syntax with no vendor prefixes required. The only exception is Internet Explorer 11, which has partial Grid support using an older syntax — but IE11 is no longer supported by Microsoft.

What are named grid areas and why use them?

grid-template-areas lets you lay out a grid as an ASCII-art map of names, which is far more readable than line numbers. You name regions in the container — grid-template-areas: 'header header' 'sidebar main' 'footer footer' — and then each child just declares grid-area: header (or sidebar, main, footer). The layout is visible right there in the CSS: you can SEE that the header spans both columns and the sidebar sits left of main. It also makes responsive changes trivial — in a mobile media query you redraw the same areas in a single column ('header' 'main' 'sidebar' 'footer') and every item moves automatically, no per-item repositioning. It's the most maintainable way to express page-level layouts.

What is subgrid and what problem does it solve?

Subgrid lets a nested grid align to its PARENT grid's tracks instead of creating its own independent ones. The classic problem it fixes: a row of cards where each card has a title, body, and footer of differing lengths — normally each card's internal rows size independently, so the titles and footers don't line up across cards. With grid-template-rows: subgrid, the cards share the parent's row lines, so all titles align, all footers align, and the cards look like a clean table even though they're separate components. Before subgrid this required fixed heights or JavaScript. It's supported in all current major browsers and is the right tool whenever inner elements of sibling components need to align to a shared grid.

Troubleshooting & Technical Tips

Grid items overflowing their container

This usually happens when using fixed pixel values for column templates (e.g., 200px) that sum to more than the container width. Switch to fr units (1fr) or use minmax(0, 1fr) instead of 1fr — the "0" minimum prevents overflow when content is wider than the track. Also check that the container itself has a defined width, especially in flex parents.

Grid not applying: display: grid has no visible effect

The most common cause is that the grid container has no height and the content does not define it. For rows with fr units to work, the container needs a defined height (height: 100vh or height: 500px). Check that the selector .grid-container matches your actual HTML element. Also verify there are no conflicting display rules from parent styles.

Items not spanning multiple columns or rows as expected

grid-column: 1 / span 2 means "start at column 1 and span 2 tracks". Make sure the span value does not exceed the total number of grid columns. Also check for implicit grid creation: if grid-auto-flow creates extra tracks, explicit placement may not behave as expected. Use the browser DevTools Grid inspector to visualize the actual grid lines.

Related Guides

Related Tools

Embed this tool on your site

Paste this snippet into any HTML page or blog post to embed a live, fully working copy of CSS Grid Generator. Free for any use.