CSS Flexbox Layout Generator

How to use 3 steps
  1. Set flex-direction, justify-content and align-items.
  2. Set flex-wrap and the gap; the preview shows the layout live.
  3. Press Copy Flexbox CSS and paste it into your stylesheet.
Box 1
Box 2
Box 3
Generated Flexbox CSS

How this tool works

Flexbox lays items out along one axis and aligns them across the other. Which axis is which is decided entirely by flex-direction: in a row the main axis runs horizontally, so justify-content spaces items left to right and align-items positions them vertically. Switch to column and the two swap over. Nearly every moment of confusion with flexbox traces back to that swap.

justify-content distributes items along the main axis, align-items positions them across it, flex-wrap decides whether items that do not fit spill onto a new line or are squeezed, and gap sets the space between them. The preview gains a height in column mode, because align-items has nothing to demonstrate in a container only as tall as its contents.

Which property does what

  • The axes swap when you change direction: justify-content is horizontal in a row and vertical in a column. If a property has stopped doing what you expect, check the direction before anything else.
  • gap replaced margins on the children: one declaration on the container rather than a margin on each item, with no leading or trailing space to strip off. It is the reason flex layouts need far less tidying than they used to.
  • nowrap is the default and it squeezes: items that do not fit are compressed rather than moved to a new line. If a row is crushing its contents on a narrow screen, wrap is the fix.
  • The reverse directions change order visually, not in the document: keyboard and screen-reader order follow the markup. Reversing for visual effect leaves those two disagreeing, which is disorienting for anyone not using a mouse.

The axes swap, and that is the whole confusion

Flexbox lays items out along one axis and aligns them across the other. Which is which is decided entirely by flex-direction.

In a row, the main axis runs horizontally: justify-content spaces items left to right and align-items positions them vertically. Switch to a column and the two swap over — justify-content now runs down the page and align-items across it.

Nearly every moment of confusion with flexbox traces back to that swap. If a property has stopped doing what you expect, check the direction before anything else. Seeing it happen in the [msm_tool slug="flexbox-generator"] tends to fix it permanently in a way that reading about it does not.

The values worth knowing

justify-content distributes along the main axis. flex-start, center and flex-end are self-explanatory. The three that get confused:

  • space-between — no space at the ends, so the first and last items touch the edges. This is the one you want for a header with a logo on one side and navigation on the other.
  • space-around — each item gets equal space on both sides, so the outer gaps end up half the size of the inner ones. It looks slightly wrong and is rarely what anyone meant.
  • space-evenly — every gap is equal, including the outer ones. Usually what people reach for space-around expecting.

align-items positions across the other axis. stretch is the default and is why a row of cards with different content lengths all end up the same height — useful, and occasionally surprising. center is the one that makes vertical centring trivial, which was famously not the case before flexbox existed.

Centring, finally

justify-content: center and align-items: center together centre on both axes, regardless of direction. That pair is the answer to a problem that consumed an unreasonable amount of collective effort for about fifteen years.

One caveat: the container needs a height for vertical centring to have anywhere to happen. A flex container that is only as tall as its contents centres them in exactly the space they already occupy, which looks like nothing happened.

nowrap is the default, and it squeezes

Items that do not fit are compressed rather than moved to a new line. If a row is crushing its contents on a narrow screen, flex-wrap: wrap is the fix, and it is the single most common omission in flex layouts that break on mobile.

Worth knowing what wrapping changes: once items wrap, align-items applies within each line, and align-content controls how the lines themselves are distributed. That second property only does anything on a wrapped, multi-line container, which is why it appears to be broken when tried on a single line.

gap replaced margins

One declaration on the container instead of a margin on every child, with no leading or trailing space to strip off and no :last-child rule. It is the reason modern flex layouts need far less tidying than they used to, and it is supported everywhere current.

The reverse directions have an accessibility cost

row-reverse and column-reverse change the order items appear in visually. They do not change the order in the document, which is what keyboard navigation and screen readers follow.

Reversing for visual effect leaves those two disagreeing: someone tabbing through the page moves in an order that does not match what they see. The same applies to the order property. Use them where the visual and document order genuinely should differ and you have thought about it — not as a quick way to move something to the right.

flex-grow, flex-shrink and flex-basis

These sit on the children rather than the container, and the shorthand catches people out. flex: 1 means grow to fill, do not resist shrinking, and start from zero — which is why flex: 1 on several items makes them equal width regardless of their content.

If you want items sized by their content but allowed to grow, flex: 1 1 auto is the form. The difference between a basis of 0 and auto accounts for most of the surprise when items come out a different size than expected.

Flexbox or grid

Use flexbox when the content decides the sizes: a navigation bar, a row of buttons, a card's internal layout. Use grid when the layout is defined before the content: a page skeleton, a gallery of equal tiles, anything two-dimensional.

They nest happily and most real pages use both — a grid for the page, flexbox inside each card.

Frequently Asked Questions

Specific to the CSS Flexbox Layout Generator. Broader questions about this category are answered on the category page.

The direction is set to column, which swaps the axes. In a column, justify-content runs down the page and align-items runs across it.
justify-content: center and align-items: center together. That pair centres on both axes regardless of which direction is set.
space-between puts no space at the ends, so the first and last items touch the edges. space-evenly makes every gap equal, including the outer ones.
flex-wrap defaults to nowrap, which compresses items to fit. Set it to wrap so they move onto a new line instead.
Flexbox for a single row or column where content decides the sizes. Grid when you need rows and columns at once, or a layout defined before the content is known.

Subscribe to receive notifications about new tools, features, and productivity tips.