Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages

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

By Maaz Zindani · Updated

align-content only acts when flex-wrap is wrap or wrap-reverse. Sizes are for the preview and the code.

Live preview
1
2
3
  • 3 items, row, nowrap, space-between, center
CSS
.container { display: flex; flex-direction: row; justify-content: space-between; align-items: center; } .item { flex: 0 0 auto; width: 80px; height: 60px; }
HTML
<div class="container"> <div class="item">1</div> <div class="item">2</div> <div class="item">3</div> </div>

How to use the CSS Flexbox Generator

  1. Choose flex-direction and flex-wrap to set how the items run and whether they break onto new lines.
  2. Set justify-content for the main axis and align-items for the cross axis, and align-content if the items wrap.
  3. Type the number of Items, the Gap and the item size, and pick fixed items or items that share the space.
  4. 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.

Where the left edge of each item lands in a 400px row of 3 items, 80px wide
justify-contentNo gapGap of 10px
flex-start0px, 80px, 160px0px, 90px, 180px
flex-end160px, 240px, 320px140px, 230px, 320px
center80px, 160px, 240px70px, 160px, 250px
space-between0px, 160px, 320px0px, 160px, 320px
space-around26.67px, 160px, 293.33px23.33px, 160px, 296.67px
space-evenly40px, 160px, 280px35px, 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.

A 60px item in a container 240px tall: offset from the top
align-itemsFixed heightHeight not set
stretch0px0px, 240px tall
flex-start0px0px, 60px tall
flex-end180px180px, 60px tall
center90px90px, 60px tall
baseline0px0px, 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

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

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.