Flexbox generator and playground
Change the flex properties of the container and of a selected item — the preview updates instantly and the ready-made CSS below follows along.
.container {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
justify-content: flex-start;
align-items: stretch;
gap: 8px;
}
How to use it
- In the Container block choose
flex-direction,flex-wrap,justify-content,align-items,align-contentandgap. Use the “−” and “+” buttons to set 1 to 8 items. - Click any item in the preview (or select it with
TabandEnter) — itsflex-grow,flex-shrink,flex-basis,align-selfandorderfields appear. - Watch the axes:
justify-contentworks along the main axis andalign-itemsalong the cross axis;flex-directionchanges which way those axes point. - Press “Copy”. The code contains the
.containerrule plus.item:nth-child(N)rules only for items that have non-default values.
How it works
Container and items
An element with display: flex becomes a flex container and its direct children become flex items. The container sets the general rules (flex-direction, justify-content, align-items, gap), while each item is controlled by its own properties (flex-grow, flex-basis, align-self, order).
.container {
display: flex;
gap: 8px;
}
Main axis and cross axis
With flex-direction: row the main axis runs left to right and the cross axis runs top to bottom; with column they swap. justify-content distributes items along the main axis (flex-start, center, space-between, space-evenly), while align-items aligns them along the cross axis. The default align-items value is stretch: items grow to fill the container along the cross axis.
.container {
display: flex;
justify-content: space-between;
align-items: center;
}
flex-grow, flex-shrink and flex-basis
flex-basis is the starting size of an item (with auto it uses width or the content size). If the container has free space left, flex-grow shares it out in proportion; if there is not enough room, flex-shrink squeezes the items. An item with flex-grow: 0 does not take extra space, and one with flex-shrink: 0 never shrinks.
.item {
flex: 1 1 200px; /* grow shrink basis */
}
flex-wrap, gap and align-content
By default flex-wrap: nowrap keeps every item on one line and shrinks them when space runs out. With wrap, items move onto new lines. Once there are several lines, align-content decides how those lines are distributed inside the container; on a single-line container it has no effect. For spacing between items use gap instead of margin — there is no extra space left at the edges.
align-self and order
align-self overrides the cross-axis alignment for a single item without touching the others. order rearranges items visually without changing the HTML (the default is 0 and lower values come first). Keep in mind that order only changes how things look: tab order and screen readers still follow the HTML.
.item:nth-child(2) {
align-self: flex-end;
order: -1;
}
FAQ
What is the difference between justify-content and align-items?
justify-content distributes items along the main axis, while align-items aligns them along the cross axis. With flex-direction: column the axes swap, so justify-content works vertically and align-items works horizontally.
Why is align-content not working?
align-content only works when items are split across several lines: you need flex-wrap: wrap and the container must have free space beyond the height of the lines (for example, a fixed height). On a single-line container use align-items instead.
What does `flex: 1` mean?
flex: 1 is the same as flex: 1 1 0%: the item shares free space, may shrink, and starts from a size of zero. If you give flex: 1 to several items, they split the free space equally.
Does gap work in flexbox in every browser?
Yes, gap is supported for flexbox in all modern browsers (Safari since version 14.1). If you do not need to support old browsers, margin tricks for spacing are no longer necessary.