How to Generate CSS Grid Layouts
Design CSS Grid layouts visually by defining columns, rows, gaps, and item placement, then export production-ready CSS.
- 1
Define columns and rows
Set the number of columns and rows using the visual editor. Specify sizes with pixels, fractions (fr), percentages, or auto for each track.
- 2
Set gaps
Adjust the row gap and column gap to control spacing between grid cells. Use consistent gap values for a clean, uniform layout.
- 3
Place items in the grid
Drag items to span multiple columns or rows. Use named grid areas or explicit line numbers to position items precisely.
- 4
Copy the CSS
Copy the generated CSS for the grid container and any positioned items. Paste it into your stylesheet and assign the classes to your HTML elements.
CSS Grid is the most powerful layout system in CSS, but writing grid-template-columns, grid-template-rows, and placement rules from scratch requires a mental model that takes time to develop. The CSS Grid Generator lets you design grid layouts visually and generates the CSS code, so you can focus on the design rather than the syntax.
Understanding CSS Grid
CSS Grid works with a two-dimensional model of rows and columns. You define a grid on the container, then place child items into grid cells. Unlike flexbox, which works along a single axis, Grid controls both axes simultaneously, making it ideal for full page layouts, card grids, and any design that requires precise row and column alignment.
The grid-template-columns and grid-template-rows properties define the grid tracks. The fr unit is Grid-specific and represents a fraction of available space: grid-template-columns: 1fr 2fr creates two columns where the second is twice as wide as the first. You can mix units freely: 200px 1fr auto creates a fixed-width column, a flexible column, and a column that sizes to its content.
Grid items are placed using grid-column and grid-row properties, or by defining named grid-template-areas that create a visual ASCII map of your layout. Named areas are especially intuitive for page-level layouts where you want to define regions like header, sidebar, main, and footer.
Tips and best practices
- Use
frunits for flexible columns. Fractional units distribute available space proportionally, making responsive layouts simpler than percentage-based calculations. - Use
minmax()for responsive track sizing.grid-template-columns: repeat(auto-fill, minmax(250px, 1fr))creates a responsive grid that automatically adjusts the number of columns based on available width — no media queries needed. - Name your grid areas for clarity.
grid-template-areas: "header header" "sidebar main" "footer footer"is immediately readable and easier to maintain than line-number-based placement. - Use
gapinstead of margins. Like flexbox, thegapproperty adds space between grid cells without affecting the outer edges, resulting in cleaner spacing. - Combine Grid and Flexbox. Use Grid for the overall page layout and flexbox for component-level layouts within grid cells. They complement each other well.
Common issues
- Items overflowing their grid cells. Grid cells constrain item placement but not item size by default. Long words or fixed-width content can overflow. Use
overflow: hidden,min-width: 0, orword-break: break-wordon grid items. - Implicit grid creating unexpected rows. If you place an item outside the defined grid, CSS creates implicit tracks to accommodate it. These implicit tracks use
autosizing by default. Control them withgrid-auto-rowsandgrid-auto-columns. auto-fillvsauto-fitconfusion.auto-fillcreates empty tracks to fill available space, whileauto-fitcollapses empty tracks so items stretch to fill the container. Useauto-fitwhen you want items to grow, andauto-fillwhen you want consistent column widths.
Open CSS Grid Generator
Use the CSS Grid Generator tool directly — no sign-up needed. Runs entirely in your browser.
Open CSS Grid Generator
Comments