Flexbox is the go-to tool for lining things up: buttons in a toolbar, links in a header, cards in a row. The properties are easy to list but hard to remember, because each one depends on the direction you choose. A playground lets you learn by doing. Change one setting, watch the boxes move, and the meaning sticks.
The two axes
Everything in flexbox happens along two lines:
- The main axis follows
flex-direction. In a row it runs left to right; in a column, top to bottom.justify-contentworks here. - The cross axis is at right angles to it.
align-itemsandalign-contentwork here.
Once that clicks, the rest follows. For example, “space-between” on a row pushes the first item to the left edge and the last to the right, which is exactly what a navbar with a logo on one side and links on the other needs.
Item properties
Click any box to edit it on its own:
- flex-grow decides who takes extra space. Give the main content 1 and the sidebar 0.
- flex-shrink decides who gives up space first when it gets tight. Set 0 to stop an item from squashing.
- flex-basis is the starting size before growing or shrinking, such as
200pxor30%. - order moves an item visually without changing the HTML. Keep the source order logical for keyboard and screen reader users.
When to reach for grid instead
If you find yourself fighting flexbox to line items up in both rows and columns, the layout probably wants CSS grid. Our grid generator handles that case.
How to use the Flexbox Playground
- Pick values for
flex-direction,flex-wrap,justify-content,align-itemsandalign-content. - Set the gap and the number of items. Untick Mixed item sizes for equal boxes.
- Click an item to edit its
flex-grow,flex-shrink,flex-basis,align-selfandorder. - Try a recipe such as Center anything or Navbar.
- Drag the preview’s bottom-right corner to resize it, then copy the CSS.
Features
- All container properties, with a short hint for each when you focus it.
- Per-item flex, align-self and order, written as
:nth-child()rules. - Output lists only the properties that differ from the defaults.
- Resizable preview to see wrapping and shrinking in action.
- Four common recipes: centring, navbar, wrapping cards and vertical stack.
Frequently asked questions
What is the difference between justify-content and align-items?
justify-content spaces items along the main axis (left to right in a row). align-items positions them on the cross axis (top to bottom in a row). Switching flex-direction to column swaps which is which.
How do I centre a div with flexbox?
On the parent, set display: flex, justify-content: center and align-items: center. The Center anything recipe shows it. The parent needs a height for vertical centring to be visible.
Why does align-content do nothing?
align-content only affects containers with more than one line, so it needs flex-wrap: wrap and enough items to wrap. With a single line, use align-items instead.
What does flex: 1 mean?
It is short for flex-grow: 1, flex-shrink: 1, flex-basis: 0%. The item takes an equal share of the free space. Give every item flex: 1 for equal columns.
Written by Dewan Razib