CSS Grid & Flexbox Layout Builder
Drag and drop child elements, adjust container properties, and copy clean CSS output. Switch between Grid and Flexbox modes instantly.
display: grid.display: flex on the container.1fr 2fr creates two columns where the second is twice the width of the first.flex-start, center, space-between, and more.stretch, center, flex-start, flex-end.nowrap) or wrap onto multiple lines (wrap) when they overflow the container.repeat() function.The Complete Guide to CSS Grid and Flexbox
CSS layout stopped being painful once Grid and Flexbox arrived. Together they cover virtually every layout pattern you encounter in modern frontend work - from full-page scaffolding to fine-grained component alignment. This guide explains when to reach for each one, how to read the generated CSS from this tool, and the most common pitfalls to avoid.
How to Use This Tool
Select your display mode (Grid or Flexbox) using the toggle at the top left. The control panels will adjust to show only the relevant properties - unused panels fade out to keep the UI clean. Adjust container properties and the child element count to see the live preview update in real time. Drag and drop the colored boxes in the preview to reorder them. The Generated CSS panel always reflects the current state and is ready to copy.
Understanding the Generated CSS
The output uses two class selectors: .layout-container for the parent element and .layout-item for each child. Drop these into your own stylesheet and apply the classes to your HTML. You do not need to keep any of the preview styling - the generated CSS contains only the structural layout properties.
In Grid mode the output includes display: grid, grid-template-columns, grid-template-rows, and gap. In Flexbox mode it includes display: flex, flex-direction, justify-content, align-items, flex-wrap, and gap.
Grid vs. Flexbox: Practical Patterns
Grid excels at card grids, page shells (header, sidebar, main, footer), and any layout where you want rows and columns to line up across the entire container. The canonical use case is a responsive card grid: grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)) creates as many columns as will fit without a single media query.
Flexbox excels at navigation bars, toolbars, button groups, and centering a single element inside a container. Its strength is that items can shrink and grow to fill available space (flex-grow, flex-shrink, flex-basis) without you having to calculate widths explicitly.
The repeat() and minmax() Functions
repeat(3, 1fr) is shorthand for 1fr 1fr 1fr - three equal columns. repeat(auto-fill, minmax(200px, 1fr)) creates as many columns as will fit, each at least 200px wide. minmax(min, max) sets a floor and ceiling for a track size, making grids intrinsically responsive. These are the two Grid functions you will use in nearly every project.
Frequently Asked Questions
grid-template-columns: 1fr 2fr creates two columns where the second is always twice as wide as the first, and together they fill the container. Writing repeat(3, 1fr) is shorthand for three equal-width columns. The fr unit is similar to flex-grow in Flexbox - it distributes leftover space proportionally. Unlike percentages, fr values automatically adapt as you add or remove tracks without you needing to recalculate.
justify-content controls alignment along the main axis (the direction set by flex-direction), while align-items controls alignment along the cross axis (perpendicular to it). If flex-direction is row, justify-content aligns items horizontally and align-items aligns them vertically. If flex-direction is column, the axes flip. In CSS Grid, justify-items aligns grid items within their cells on the inline (horizontal) axis, while align-items aligns them on the block (vertical) axis. The justify prefix always refers to the inline or main axis; the align prefix always refers to the block or cross axis.