Free tool · Developer tools
CSS flexbox generator
This CSS flexbox generator shows a live preview of your flex container and writes the CSS and HTML as you change each setting. The starting layout has 3 items of 80 by 60 pixels, spread with space-between and centered across.
- Free
- No sign-up
- Runs in your browser
- 3 items, row, nowrap, space-between, center
How to use the CSS Flexbox Generator
- Choose flex-direction and flex-wrap to set how the items run and whether they break onto new lines.
- Set justify-content for the main axis and align-items for the cross axis, and align-content if the items wrap.
- Type the number of Items, the Gap and the item size, and pick fixed items or items that share the space.
- Check the preview, then press Copy CSS, Copy HTML or Copy both.
What does justify-content do in flexbox?
It decides how free space is shared along the main axis. In a row, the main axis runs left to right. Take a 400px wide container with 3 items of 80px. The items use 240px, so 160px is free. With flex-start the items sit at 0px, 80px, 160px from the left edge. With flex-end they start at 160px, 240px, 320px. With center the first item starts at 80px.
The three spacing values split the free space differently. Space-between puts no space at the edges and 80px between items. Space-around gives each item an equal share on both sides, so the edges get half a share, and the items start at 26.67px, 160px, 293.33px. Space-evenly makes every gap equal, edges included, and starts the items at 40px, 160px, 280px. A gap of 10px is added between items before the free space is shared, as the last column shows.
| justify-content | No gap | Gap of 10px |
|---|---|---|
| flex-start | 0px, 80px, 160px | 0px, 90px, 180px |
| flex-end | 160px, 240px, 320px | 140px, 230px, 320px |
| center | 80px, 160px, 240px | 70px, 160px, 250px |
| space-between | 0px, 160px, 320px | 0px, 160px, 320px |
| space-around | 26.67px, 160px, 293.33px | 23.33px, 160px, 296.67px |
| space-evenly | 40px, 160px, 280px | 35px, 160px, 285px |
What is the difference between align-items and align-content?
Align-items places the items on the cross axis, which is vertical in a row. In a container 240px tall, an item 60px high sits at 0 with flex-start, at 180px with flex-end and at 90px with center. Stretch only grows an item whose height is not set. An item with a fixed height behaves like flex-start, which is why the first column shows 0 for stretch.
Align-content works on the whole set of lines, so it only matters when flex-wrap is set to wrap or wrap-reverse. In a single line it has no effect, which is why the menu is labeled for wrapping containers.
| align-items | Fixed height | Height not set |
|---|---|---|
| stretch | 0px | 0px, 240px tall |
| flex-start | 0px | 0px, 60px tall |
| flex-end | 180px | 180px, 60px tall |
| center | 90px | 90px, 60px tall |
| baseline | 0px | 0px, 60px tall |
How do flex-direction and flex-wrap change the layout?
Flex-direction picks the main axis. Row runs items along the text direction and column stacks them from top to bottom. The reverse values start from the opposite edge, so the first item sits at the end. When you choose column, the roles swap: justify-content now spreads items vertically and align-items works across the width.
Flex-wrap decides what happens when the items do not fit. With nowrap, all the items stay on one line and can overflow. With wrap, extra items move to a new line, and wrap-reverse stacks the new lines the other way. Choose wrap and set the items to 12 in the tool to watch the new lines form.
Should items have a fixed size or share the space?
The Item size menu switches between two common patterns. Fixed writes flex: 0 0 auto, so an item neither grows nor shrinks and keeps the width and height you typed. Share the space writes flex: 1 1 0, so the items divide the free space equally and only the cross size stays fixed.
The gap property sets the space between items and between lines. It adds nothing at the outer edges of the container, so padding is the tool for outer space. The code the page writes is plain CSS with no prefixes and no script.
Use it with
- Box Shadow Generator: visual CSS box-shadow builder.
- CSS Grid Generator: build a CSS grid layout and copy the code.
- Px to Rem Converter: convert px, rem, em.
Questions
How do I center items with flexbox?
Set justify-content and align-items to center. In a 400px row, three 80px items start at 80px from the left edge and sit 90px from the top of a 240px container.
What is the default value of flex-direction?
Row. The main axis follows the text direction, so items run from left to right in left-to-right languages.
Does align-content work without wrapping?
No. Align-content has no effect on a container with flex-wrap set to nowrap, because there is only one line to place.
What is the difference between space-around and space-evenly?
Space-around gives each item an equal share on both sides, so the edge space is half of the space between items. Space-evenly makes every gap, edges included, the same size. With 3 items of 80px in 400px, the first item starts at 26.67px with space-around and at 40px with space-evenly.
What does flex: 1 mean?
It is a shorthand for flex: 1 1 0%. The item can grow to take its share of free space and can shrink, and its starting size is 0%.
Can I use the generated CSS in any project?
Yes. The output is standard CSS and plain HTML. Change the class names to fit your own naming.
Sources
- MDN: justify-content, read
- MDN: align-items, read
- MDN: align-content, read
- MDN: flex-direction, read
- MDN: flex-wrap, read
- MDN: flex, read
- MDN: gap, read
The tool's results are computed in your browser by the code on this page, using the sources listed above; the explanations are checked against the tool's own output.