Align items with flexbox and see the result as you go
This flexbox generator lets you set up a flex container and its items, then writes the matching CSS and HTML. Every change shows up straight away in the preview, which uses exactly the values from the code. Flexbox lines up boxes in a row or a column: a menu, a row of buttons, cards that wrap onto new lines, a box centered in the middle of the screen.
Three ready-made examples give you a starting point: "Navigation bar", "Cards" and "Centering". For a full page laid out in rows and columns, with a header, sidebar and footer, switch to the CSS grid generator instead.
How to use it
- Under "Container", pick the "Direction", the "Wrapping", the "Main axis" (
justify-content) and the "Cross axis" (align-items). - Set the gaps and, if needed, a "Minimum height" so you can see the effect of vertical alignment.
- Click an item in the preview, then adjust its "Grow", "Shrink", "Basis", "Order" and "Own alignment".
- Use "Add an item" and "Remove the selected item" below the preview; drag the corner of the preview to check how the layout behaves on a narrow screen.
- Click "Copy CSS", then "Copy HTML".
Flexbox concepts in plain terms
Main axis and cross axis
With flex-direction: row, the main axis runs horizontally: justify-content spreads the items from left to right and align-items places them vertically. With column, the roles swap. This is the most common source of confusion: if justify-content: center does not center things horizontally, check the direction first.
Centering a box
The "Centering" example gives you the snippet people ask for most:
.container { display: flex; justify-content: center; align-items: center; min-height: 240px; }
A 100 × 40 px item inside a 600 × 240 px container ends up 250 px from the left edge and 100 px from the top.
flex-grow, flex-shrink and flex-basis
flex-basis is the starting size, flex-grow is the share of free space the item takes, and flex-shrink controls how it gives up space when there is not enough room. The tool writes the flex shorthand in that order. In a 600 px container with no gap, three items with no padding or border set to flex: 1 1 0, flex: 2 1 0 and flex: 1 1 0 measure 150, 300 and 150 px: the space is split into 4 shares. With box-sizing: border-box and 16 px of padding plus border on each item, those 48 px are set aside first, leaving 552 px to share, so the widths become 154, 292 and 154 px.
Cards that wrap
The "Cards" example uses flex-wrap: wrap, gap: 16px and flex: 1 1 200px on each card. In 680 px, three cards fit on one line (3 × 200 + 2 × 16 = 632 px); the remaining 48 px are shared out, so each card is 216 px wide. On a phone, only one card per line is left, with no media query. To finish the cards, round the corners with the border radius generator and add depth with the box shadow generator.
The navigation bar
In the "Navigation bar" example, only the logo gets flex: 1 1 auto: it takes all the free space and pushes the links to the right, while align-items: center lines them up vertically.
order and align-self
order changes the display order without touching the HTML: an item with order: -1 moves to the front. align-self gives a single item a different alignment from the others. Keep in mind that keyboard navigation and screen readers follow the HTML order, not the order on screen.
Browser support
Flexbox and the gap property work in all current browsers, with no prefix.
Frequently asked questions
Why does align-content do nothing?
align-content spaces out the lines relative to each other. With flex-wrap: nowrap there is only one line, so the property has no effect. Switch to wrap and give the container a height.
Why won't my item shrink below the width of its text?
By default, a flex item does not get narrower than its longest word. Add min-width: 0 to the item to allow it.
Flexbox or Grid?
Use flexbox for one dimension (a row or a column) and Grid to place boxes in rows and columns at the same time. The two work well together.
Are my settings saved?
Your last settings stay on this device. Nothing is sent anywhere: everything is calculated in your browser.