CSS Flexbox Generator

Change the container and item properties of a flex layout, watch the numbered items move in the live preview and copy clean CSS and HTML.

Loading tool…

About the Flexbox Generator

A flex container lays out its children along a main axis, set by flex-direction, and a perpendicular cross axis. justify-content distributes free space along the main axis, align-items positions the items on the cross axis within their line, and align-content distributes the lines themselves, which is why it only has an effect when flex-wrap allows several lines. With flex-direction: column the axes swap, so justify-content then works vertically.

The size of an item starts from its flex-basis (or its width or content when the basis is auto). Free space is then shared in proportion to flex-grow, and a lack of space is taken away in proportion to flex-shrink multiplied by the basis. The initial value is flex: 0 1 auto: items shrink but do not grow. Beware of the shorthand: flex: 1 means 1 1 0%, which ignores the content size, whereas flex-grow: 1 keeps the basis at auto. An item never shrinks below its minimum content size unless it has min-width: 0.

gap adds space between items only, not at the edges, and replaces the old technique of margins with negative offsets. order changes the visual order without changing the DOM, so keyboard and screen reader order stay as written in the HTML: use it sparingly. The generated CSS lists only the declarations that differ from the initial values, and it writes flex-flow when both the direction and the wrapping are set.

How to use it

  1. Choose the direction, wrapping, justification and alignment of the container, and set the gaps.
  2. Add or remove items, and click an item in the preview to select it.
  3. Set flex-grow, flex-shrink, flex-basis, align-self and order of the selected item.
  4. Resize the container with the sliders or by dragging its corner to see how the items react.
  5. Copy the CSS and the HTML.

Frequently asked questions

What is the difference between justify-content and align-items?
justify-content works along the main axis (horizontal for flex-direction: row) and distributes the free space between the items. align-items works along the cross axis and positions each item within its line: stretch, start, center, end or baseline.
Why does align-content do nothing?
align-content distributes the lines of a wrapping container. With flex-wrap: nowrap there is a single line that fills the container, so there is nothing to distribute. Set flex-wrap to wrap, and give the container more height than its lines need.
How do I center an element with flexbox?
Set display: flex, justify-content: center and align-items: center on the parent. The parent needs a height, for example min-height: 100vh, for the vertical centering to be visible.
When should I use grid instead of flexbox?
Flexbox arranges items in one dimension and lets their content decide the sizes: toolbars, navigation, a row of cards. Grid controls rows and columns at the same time and places items in a defined structure: page layouts, galleries, forms. They combine well: a grid for the page, flex inside its components.

Related tools