~/css/flexbox-generator
CSS Flexbox Generator
Arrange items with flexbox and see the result instantly: choose the direction and wrapping, distribute items with justify-content, align them with align-items, set the gap and the number of items, then copy the container CSS.
Using the CSS Flexbox Generator
Tutorial: a navbar in four settings
Most headers are a logo on the left and links on the right. Set flex-direction to row, justify-content to space-between and align-items to center, and set the item count to 2: one item stands for the logo, the other for the group of links. Press Copy, add the CSS to your header element and put the links inside one wrapper so they move as a block. Give the wrapper its own display: flex and a gap of 16 to 24px to space the links.
Tutorial: centre anything
To put a box in the exact middle of a section, choose center for both justify-content and align-items. The container needs a height for vertical centring to show, such as min-height: 60vh on a hero, because a flex container is only as tall as its content by default.
Tutorial: a row of cards that wraps
For tags, chips, buttons or small cards, use row with flex-wrap: wrap, flex-start and a gap of 12 to 16px. Raise the item count to see how the row breaks onto new lines. If the cards should line up in strict columns as well as rows, that is a grid job, not a flexbox one.
Main axis and cross axis
Flexbox lays items along a main axis, set by flex-direction: left to right for row, top to bottom for column. justify-content distributes items along the main axis; align-items positions them on the cross axis. Switch direction and the two properties swap roles.
Distributing space
space-between pushes the first and last items to the edges with equal space between the rest, ideal for a header with a logo on one side and links on the other. space-evenly gives equal space everywhere, including the edges. center keeps everything together in the middle.
Wrapping
By default flex items squeeze onto one line. flex-wrap: wrap lets them flow onto new lines when there is no room, which is what you want for tags, chips and button groups on small screens.
Use gap
gap adds space between items only, never at the edges, and works on wrapped rows too. It replaces the old margin-and-negative-margin tricks.
Sizing the items
This generator styles the container. The size of each item comes from its content plus the item properties: give items flex: 1 to share the row equally, flex: none to keep their natural size, or a flex-basis such as 200px as a starting width. The flexbox cheat sheet lets you try each item property live.
Frequently asked questions
How do I centre something with flexbox?
Set display: flex; justify-content: center; align-items: center; on the parent. Choose center for both in this generator to see it.
When should I use grid instead?
When you need rows and columns to line up with each other, such as a card gallery. See the CSS grid generator.
Is there a quick reference?
Yes, the flexbox cheat sheet shows every container and item property with a live example.
What is the difference between justify-content and align-items?
justify-content places items along the main axis (horizontally in a row), align-items places them on the cross axis (vertically in a row). In a column the two swap.
Why does align-items: center do nothing?
The container is no taller than its tallest item, so there is no spare space to centre in. Give the container a height or min-height.
Does the copied CSS work in every browser?
Yes. Flexbox and the gap property for flex containers work in all current versions of Chrome, Edge, Firefox and Safari.