CSS Flexbox Generator

Builds a flexbox layout visually — direction, wrapping, and alignment on both axes — with a live preview and the CSS to copy. Flexbox governs one dimension at a time, which is the single idea that makes the property names stop being confusing.

Live demo · real result from this tool

Press Generate to create a result.

Processed on your device — nothing is uploaded.

How to use CSS Flexbox Generator

  1. Set flex-direction to row or column — this decides what the other properties mean.
  2. Adjust justify-content to distribute items along that axis.
  3. Adjust align-items to align them across it.
  4. Turn on wrapping if items should move to a new line, then copy the generated CSS.
  5. gap works in flexbox in every current browser — use it instead of margins on children.
  6. align-content only has an effect when items wrap onto multiple lines; on a single line it does nothing, which is a frequent source of confusion with align-items.
  7. An item’s own align-self overrides the container’s align-items for that item alone.

Main axis and cross axis, and why the names change meaning

justify-content aligns along the main axis; align-items aligns across it. Which direction those are depends entirely on flex-direction: with row, the main axis is horizontal, so justify-content moves things left and right. Switch to column and the same property now moves them up and down. Nearly every moment of confusion with flexbox comes from reaching for justify-content when the direction has changed under you. Learn the pair as “along” and “across” rather than as horizontal and vertical.

The centring problem, solved

display: flex with justify-content: center and align-items: center centres a child both ways, in two lines, on a container of any size. This was genuinely difficult before flexbox — the old approaches involved absolute positioning with negative margins, or table-cell display, and none worked for content of unknown height. If you learn one flexbox pattern, learn this one.

flex-wrap, and the three-number shorthand

By default flex items refuse to wrap and shrink instead, which is why a row of cards gets narrower and narrower on a phone rather than stacking. flex-wrap: wrap lets them move to a new line. On the items themselves, flex is shorthand for grow, shrink and basis: flex: 1 means grow to fill the space, flex: 0 0 auto means keep your natural size, and flex: 1 1 300px means start at 300px and flex from there — which, with wrap, is the whole recipe for a responsive card grid without a media query.

When to use grid instead

Flexbox lays out one dimension; grid lays out two. If you are aligning a row of buttons, centring something, spacing a navbar, or distributing items along a line, flexbox is simpler and right. If you need rows and columns to align with each other — a page layout, a dashboard, a genuine table of cards where column three lines up across every row — that is grid. They compose: a grid of cards whose contents are each laid out with flex is entirely normal.

Frequently asked questions

What is Flexbox?
A one-dimensional CSS layout model that distributes space and aligns items in a container.
When should I use Flexbox vs Grid?
Flexbox is ideal for one-dimensional layouts (rows OR columns). CSS Grid is better for two-dimensional layouts.
What is flex-grow?
flex-grow determines how much a flex item grows relative to siblings when extra space is available.
How do I centre a div horizontally and vertically?
Put display: flex, justify-content: center and align-items: center on the parent, and give the parent a height. That is the whole answer, and it works whatever size the child is.
What is the difference between justify-content and align-items?
justify-content distributes items along the main axis, align-items aligns them across it. With flex-direction: row the main axis is horizontal; with column it is vertical, and the two swap roles. Think along and across, not horizontal and vertical.
Should I use flexbox or CSS grid?
Flexbox for one dimension — a row, a column, a navbar, centring. Grid when you need rows and columns to line up with one another. Nesting flex containers inside grid cells is normal and often the cleanest answer.

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close