Building a flex container
Pick the direction
rowlays items side by side,columnstacks them; the reverse options flip the order.Spread the items
justify-contentplaces them along the direction: start, centre, end, or with space between, around or evenly.Align across
align-itemssets the other axis: stretch, start, centre, end or baseline. The preview boxes have different heights so you can see it.Wrap and space
Choose
flex-wrapand type agapin pixels. Items in preview changes how many boxes are shown, up to 12.Copy the CSS
The
.containerrule below updates live. Copy it or downloadflex.css.
Example: a header with a logo left and a button right
A small business site needs its logo on the left and a 'Request a quote' button on the right, vertically centred. With two items in the preview, set justify-content to space-between and align-items to center, keep the 12 px gap.
The generated rule is below. Apply it to the header element and the logo and button sit at opposite ends at any screen width.
row, space-between, center, nowrap, gap 12, 2 items
.container {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
flex-wrap: nowrap;
gap: 12px;
}Common layouts and their settings
| Layout | direction | justify-content | align-items | wrap |
|---|---|---|---|---|
| Header: logo left, menu right | row | space-between | center | nowrap |
| Centre one thing in a box | row | center | center | nowrap |
| Strip of client logos | row | space-evenly | center | wrap |
| Stacked form fields | column | flex-start | stretch | nowrap |
| Tags or chips that wrap | row | flex-start | center | wrap |
Questions people ask
How do I centre something with flexbox?
Make the parent a flex container and set both justify-content and align-items to center. Here, choose center in both menus and copy the rule. The child is then centred horizontally and vertically, as long as the container has a height.
What is the difference between justify-content and align-items?
justify-content works along the direction the items flow, horizontally for a row. align-items works across it, vertically for a row. Switch flex-direction to column and the two swap roles, which the preview shows clearly.
Should I use gap or margins between flex items?
gap is simpler: it puts equal space between items and none on the outer edges, so there is nothing to undo on the first or last item. Current browsers all support gap with flexbox. Margins are only needed for unusual spacing on individual items.
Where your data goes
The preview and the CSS are generated in your browser. Nothing is uploaded.
Checked against the tool on