Free · Live · Nothing uploaded

CSS Flexbox & Grid generator

Adjust direction, alignment, wrapping and gap with a live visual preview, then copy production-ready CSS.

16px
6

How to build a Flexbox or Grid layout

Tweak the controls, watch the preview update live, and copy the exact CSS into your project.

🧱

1. Pick a mode

Flexbox for one-dimensional layouts, Grid for two-dimensional ones.

🎛️

2. Adjust the controls

Direction, alignment, wrapping, columns and gap — see it change instantly.

📋

3. Copy the CSS

Paste the generated class straight into your stylesheet.

Flexbox is best for a single row or column of items that need to align, distribute or reorder. Grid is best for two-dimensional layouts — rows and columns at once — including responsive card grids using repeat(auto-fit, minmax(...)), which wraps to fewer columns on narrow screens with no media queries. Everything runs client-side — this is a static CSS generator, not a live editor for your own page. For shadows and glass effects, try the CSS Shadow Generator or Glassmorphism Generator.

Grid or flexbox

The distinction that resolves most confusion is dimensionality. Flexbox lays out along one axis — a row or a column — and is the right tool when content size should drive the arrangement. Grid works in two dimensions simultaneously and is right when you want to define a structure that content fits into.

In practice: navigation bars, button groups, toolbars, and anything that should wrap naturally are flexbox problems. Page layouts, card galleries with aligned rows and columns, and any design with a defined structure are grid problems. They compose freely — a grid cell commonly contains a flex container.

The one-dimensional limitation of flexbox is concrete. In a wrapped flex container, each line sizes independently, so items in the second row do not align with those in the first. If column alignment across rows matters, that is grid.

Grid features worth knowing

minmax() combined with auto-fit or auto-fill produces responsive layouts with no media queries at all: repeat(auto-fit, minmax(250px, 1fr)) fits as many 250-pixel-minimum columns as the space allows and distributes the remainder equally. This single line replaces what used to take several breakpoints.

The difference between auto-fit and auto-fill shows only when there are fewer items than columns. auto-fill keeps the empty tracks, so items stay at their minimum width on the left; auto-fit collapses them, so items stretch to fill the row. Neither is wrong — collapsing usually looks better for a card grid, keeping is better where column alignment must stay stable.

The fr unit distributes free space after fixed sizes and gaps are accounted for, which is why 1fr is not the same as 50 percent in a two-column grid with a gap. Named grid areas via grid-template-areas make layouts genuinely readable, since the CSS shows the shape of the page, and rearranging at a breakpoint is a matter of redrawing the map.

Modern responsive layout

Container queries changed the model significantly. Media queries respond to viewport size, so a component cannot know whether it is in a wide main column or a narrow sidebar. A container query responds to the size of the component's own container, which makes genuinely reusable components possible — a card that switches to a horizontal layout when it has room, wherever it is placed.

Two smaller things solve long-standing problems. gap works on flexbox as well as grid, removing the need for negative margins on the container to offset child margins. And logical properties — margin-inline, padding-block, inset-inline-start — adapt automatically to writing direction, so a layout works in right-to-left languages without a separate stylesheet.

Two habits prevent the most common layout bugs: set box-sizing: border-box globally so padding and border are included in declared widths, and use min-width: 0 on flex and grid children that contain long text. Flex items default to a minimum size based on content, which is why a long unbroken string can push a layout wider than its container — this is the cause of most mysterious horizontal scrollbars.

Flexbox & Grid generator FAQ

Flexbox or Grid — which should I use?

Flexbox suits one-dimensional layouts (a row or column that aligns/distributes items). Grid suits two-dimensional layouts — rows and columns together.

What does auto-fit minmax do?

repeat(auto-fit, minmax(200px, 1fr)) creates a responsive grid that fits as many columns as comfortably fit the minimum width, wrapping to fewer on narrow screens — no media queries needed.

Is anything uploaded?

No — this is a static CSS generator that runs entirely in your browser.

Should I use grid or flexbox?

Flexbox for one-dimensional arrangements where content drives the layout — navigation, toolbars, wrapping button groups. Grid for two-dimensional structure where you define the layout and content fits it. They nest freely.

Why does my page scroll horizontally?

Usually a flex or grid child with long unbroken content. Those items default to a content-based minimum size, so set min-width: 0 on them to allow shrinking.