CSS Flexbox Generator

Build a flexbox layout visually and copy the CSS.

Preview

Set flex-direction, justify-content, align-items, flex-wrap and gap with a live preview, then copy the ready-to-use CSS. A quick way to learn and build flexbox layouts.

The main flexbox properties

Flexbox lays out the children of a container in a single direction and controls how they share space. Every property in this generator is set on the container (the parent with display: flex):

  • flex-direction sets the main axis: row (left to right), column (top to bottom), or either reversed.
  • justify-content distributes items along the main axis — packed at the start, centered, at the end, or with space between and around them.
  • align-items aligns items on the cross axis. stretch (the default) makes them fill the container's height in a row.
  • flex-wrap decides whether items squeeze onto one line or wrap onto new lines when they run out of room.
  • gap adds consistent spacing between items without margin tricks.

Common layouts

  • Center anything: justify-content: center; align-items: center;
  • Navigation bar with logo on the left and links on the right: justify-content: space-between; align-items: center;
  • Vertical stack with even spacing: flex-direction: column; gap: 16px;
  • Responsive card row: flex-wrap: wrap; gap: 16px; with a minimum width on each card.

Main axis vs. cross axis

The single idea that makes flexbox click is that justify-content and align-items follow the direction, not the screen. Switch the preview from row to column and watch the two properties swap what they control: justify-content becomes vertical and align-items becomes horizontal.

Frequently asked questions

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

justify-content positions items along the main axis (the direction set by flex-direction), while align-items positions them along the cross axis. In a row, justify-content is horizontal and align-items is vertical; in a column, the two swap.

How do I center something with flexbox?

Set the parent to display: flex with justify-content: center and align-items: center. The child will be centered both horizontally and vertically, as long as the parent has a height.

When should I use flexbox instead of CSS grid?

Flexbox is designed for laying items out in one direction — a row of buttons, a navigation bar, a vertical stack. CSS grid is better for two-dimensional layouts where you need both rows and columns to line up.

Why does align-items seem to do nothing?

The container usually has no extra space on the cross axis. In a row, give the container a height taller than its items; align-items then has room to move them.