CSS Flexbox Generator

Change the properties and watch the boxes move. The reason flexbox feels unpredictable is that justify-content and align-items are not horizontal and vertical — they are the main axis and the cross axis, and flex-direction decides which is which. Switch to column and the two swap what they control, which is why a layout that was centred one way suddenly centres the other. This tool tells you which axis is which as you change direction, rather than leaving you to work it out. align-content is left out of the output when wrapping is off, because it genuinely does nothing on a single line, and a declaration that implies an effect it cannot have is how people conclude flexbox ignored them.

Which axis is which

flex-directionjustify-contentalign-items
rowLeft to rightTop to bottom
row-reverseRight to leftTop to bottom
columnTop to bottomLeft to right
column-reverseBottom to topLeft to right

When to use it

  1. Centre something horizontally and vertically.
  2. Space navigation items evenly across a bar.
  3. Work out why your flex layout is not doing what you expect.
  4. Build a wrapping row of cards with an even gap.
  5. Learn what each flex property actually controls.

How to Use

  1. Change the property dropdowns and watch the preview.
  2. Adjust the item count to test wrapping.
  3. Copy the CSS, and the HTML if you need the structure.

Features

  1. Every core flex container property
  2. Live preview with an adjustable number of items
  3. Explains which axis each property controls
  4. align-content shown only when it can do something
  5. CSS and matching HTML output
  6. Runs entirely in your browser

FAQs

How do I centre something with flexbox?

display: flex, then justify-content: center and align-items: center on the container. That centres on both axes regardless of direction, because you have centred along each one.

Why did my layout flip when I changed flex-direction?

Because justify-content and align-items follow the axes, not the screen. In a row, justify-content works horizontally; in a column it works vertically, and align-items swaps with it. Nothing broke — the properties mean something different in the new direction.

Why does align-content do nothing?

Because it only affects how multiple lines are distributed, and with flex-wrap: nowrap there is only ever one line. Turn wrapping on and it starts working. It is omitted from the output here when wrapping is off, so the CSS does not claim an effect it cannot have.

What is the difference between align-items and align-content?

align-items positions items within their line on the cross axis. align-content distributes the lines themselves when there are several. One is about items inside a row, the other about the rows inside the container.

Should I use flexbox or grid?

Flexbox for one dimension — a row of buttons, a navigation bar, centring a thing. Grid for two — a page layout, a card grid with aligned rows and columns. The usual mistake is forcing a two-dimensional layout into flexbox and fighting it with widths and wrapping.

What does gap do that margin does not?

It spaces items without adding anything at the outer edges, so you do not need the last-child margin reset that margin-based spacing always requires. It is supported everywhere now.

Technical Details

The single thing that makes flexbox feel unpredictable is that justify-content and align-items do not map to horizontal and vertical — they map to the main axis and the cross axis, and flex-direction decides which is which. Set direction to column and the two properties silently swap what they control, which is why a layout that was centred horizontally suddenly centres vertically instead. The preview here states which axis is which as you change direction, rather than leaving it to be rediscovered. align-content is omitted from the output when flex-wrap is nowrap, because it has no effect at all on a single line; emitting it anyway implies a control the declaration does not have and is a common source of people believing flexbox ignored them. A zero gap is omitted for the same reason — it changes nothing and no one writes it by hand. The preview items carry flex-shrink: 0 deliberately, since letting them shrink to fit makes several justify-content values produce visually identical results and the tool would appear broken. The generated HTML is offered alongside the CSS because the class names in the CSS are meaningless without the structure they apply to.

Web & CSS Tools

Web & CSS Tools

  • CSS Grid Generator
  • CSS Border Radius Generator
  • CSS Box Shadow Generator
  • Cubic Bezier Generator
  • CSS Clip Path Generator
  • CSS Keyframes Generator
  • CSS Unit Converter
  • Layout Grid Calculator