Skip to content
en

CSS Flexbox generator

Builds a ready display: flex block – direction, wrap, alignment and gaps. Shows how many items fit in one row and what each property does.

Input

Wrap
Spread along the main axis
Alignment on the cross axis

Result

Fill in the fields – the result will appear here.

Report a problem
This will be sent with your message:
Advertisement

How Flexbox works

Two axes, and everything turns around them

display: flex turns on a layout with a main axis and a cross axis. flex-direction decides where the main one points: row – along the line, column – down. The cross axis is always at right angles to it. All the other container properties just say what to do along one of these axes.

justify-content is the main axis

justify-content spreads items along the main axis. With row that's horizontal, with column vertical, and this is where people mix things up most: "center it" in a column is done by align-items, not justify-content.

space-between pushes the first and last items to the edges, space-around gives each one equal space on both sides, so the outer gaps look half the size of the inner ones, and space-evenly makes all gaps equal.

align-items is the cross axis

The default is stretch: items stretch to the full height of the row. That's why cards in a row come out the same height without any height: 100%. As soon as you set center or flex-start, each item is as tall as its content again.

wrap decides whether there will be a second row

nowrap is the CSS default, and it means "one row at any cost": items shrink below their width. wrap allows wrapping, and then the item width starts to matter.

gap is read from the rows

gap: 16px 24px means 16 between rows and 24 between items in a row, in that order. If the numbers are equal, write one. There is never a gap at the container's outer edges.

Common questions about Flexbox

Flexbox or Grid?

Flexbox – when items sit along one axis and share the space themselves: a button bar, a header, a row of tags. Grid – when the layout is described in two directions at once, rows and columns. A gallery of same-size cards is usually grid, a row of items of different widths is flex.

Why do items shrink instead of wrapping?

Because nowrap is set. It's the default, and it lets the browser make items smaller than the set width. The fix is flex-wrap: wrap, and to make sure an item never shrinks – flex-shrink: 0.

How do I make items the same width?

flex: 1 on the children. It's short for flex-grow: 1; flex-shrink: 1; flex-basis: 0, and it's the zero flex-basis that makes the widths equal – with flex-basis: auto they only share the free space, not the whole row.

How do I center an item on both axes?

justify-content: center together with align-items: center. One property centers on the main axis, the other on the cross axis, so you need both.

Does row-reverse change the order for a screen reader?

No. row-reverse and wrap-reverse only change what's seen on screen; the markup order, focus order and reading order stay the old ones. If items really need to go in a different order, move them in the HTML.

Advertisement

Report a problem

This will be sent with your message: