CSS Flexbox Generator
Change every flex container property and watch the items move, with the axis explained as you switch direction.
Loading tool…
The layout is rendered by your browser from the values on this page. Nothing is uploaded.
What the CSS Flexbox Generator does
This CSS flexbox generator will change every flex container property and watch the items move, with the axis explained as you switch direction. Everything runs inside this page, so the work happens on your own device rather than on a server. It is most often used for working out which property does the thing you are trying to do. The companion to this one is our XML formatter.
How to use it
- Change flex-direction first and read the note about which axis is which.
- Try each justify-content and align-items value and watch the items move.
- Turn on wrapping to see what align-content does — it has no effect without it.
- Copy the CSS.
How it works
The thing that confuses everyone about flexbox is not the property names, it is that two of them swap meaning when you change flex-direction. justify-content always works along the main axis and align-items always works across it — but the main axis is horizontal for `row` and vertical for `column`. So the property that centres things horizontally in a row centres them vertically in a column, which is why the note above the preview tells you which is which as you change direction.
align-content is the property that appears broken. It distributes the space between wrapped lines, so with flex-wrap set to nowrap there is only one line and it does nothing at all. Turning on wrapping and adding enough items to force a second line makes its effect obvious immediately.
The demo items are deliberately different heights, because align-items: stretch and align-items: flex-start look identical on equal-height children. Most flexbox playgrounds use uniform boxes and then wonder why half the values appear to do nothing.
gap is the modern replacement for margins between flex items and is supported everywhere that matters now. It is better than margins because it only applies between items, so you never need the nth-child rule that removes the margin from the last one, and it works identically in grid.
When to use it
- Working out which property does the thing you are trying to do.
- Getting the CSS for a centred or evenly spaced row.
- Teaching or learning how the flex axes work.
- Testing a layout idea before writing it into a stylesheet.
Common mistakes to avoid
- Expecting align-content to do something without flex-wrap: wrap. It distributes wrapped lines and there is only one.
- Forgetting that justify-content and align-items swap axes when you switch to column.
- Using margins between items where gap is simpler and avoids the last-child problem.
Frequently asked questions
What is the difference between justify-content and align-items?
justify-content moves items along the main axis, align-items across it. With flex-direction: row the main axis is horizontal, so justify-content moves things left and right. Switch to column and they swap over.
Why does align-content do nothing?
Because it only affects wrapped lines, and with flex-wrap: nowrap there is only one line. Set wrapping and add enough items to force a second row and it starts working.
Should I use gap or margins?
gap, wherever you can. It applies only between items, so there is no trailing margin to strip off the last child, and it behaves the same in flex and grid.