Skip to content

Menu

CSS Flexbox Cheat Sheet: Container, Item and Alignment Properties

Checked against CSS Flexbox CSS Flexbox Level 1 (W3C CR Draft, 14 Oct 2025) and MDN reference on 28 November 2026. Each section links to the official docs it was checked against.

This sheet is for front-end developers who use display: flex daily but still look up which property goes on the container and which on the item. It follows the W3C Flexible Box Layout Level 1 draft dated 14 October 2025 and the MDN reference pages read on 28 November 2026. Layout rows are checked against the docs only, because rendering needs a real browser and none was run for this sheet.

When to use what

Reach for flexbox when content flows along one axis and the items should decide their own size: a nav bar, a row of buttons, a card footer, a media object with an image and text. If you are lining things up in rows and columns at the same time, grid is the better tool; the grid vs flexbox post walks through the borderline cases.

For sizing, learn three flex values and skip the rest. flex: 1 means grow from a basis of zero, so siblings with flex: 1 end up equal width whatever their content. flex: auto grows from the content size, so wider content stays wider. flex: none freezes the item at its natural size, which is what you want for icons and avatars.

For spacing between items, use gap on the container rather than margins on the children. It never adds space at the outer edges and you don't need a :last-child reset. Keep auto margins for the one job they do well: margin-left: auto on the last item pushes it to the far end of the row.

To try values before writing them, open the flexbox playground; the flexbox generator gives you copyable CSS, and our flexbox guide covers the model in more depth.

Initial values per MDN: flex-direction: row, flex-wrap: nowrap, justify-content: flex-start, align-items: stretch.

display: flex

Turn the element into a block-level flex container; its direct children become flex items

Try it: Flexbox PlaygroundFrom the docs, not run by us
display: inline-flex

Same flex behaviour, but the container itself sits inline in the text flow like a button

From the docs, not run by us
flex-direction: row | row-reverse | column | column-reverse

Pick the main axis; the reverse values swap the start and end edges, not the DOM order

From the docs, not run by us
flex-wrap: wrap

Let items move to new lines instead of overflowing; the default nowrap keeps everything on one line

From the docs, not run by us
flex-flow: row wrap

Shorthand that sets flex-direction and flex-wrap together

From the docs, not run by us
gap: 1rem 2rem

Row gap first, then column gap; one value sets both, and no space is added at the outer edges

From the docs, not run by us

Longhand initial values are flex-grow: 0, flex-shrink: 1 and flex-basis: auto.

flex: initial

Expands to 0 1 auto: no growing, can shrink, sized from width or content; this is what you get without setting flex

From the docs, not run by us
flex: 1

Expands to 1 1 0%: grows from a zero basis, so siblings with flex: 1 share space equally

Try it: Flexbox GeneratorFrom the docs, not run by us
flex: auto

Expands to 1 1 auto: grows and shrinks, but starts from its content size so bigger content stays bigger

From the docs, not run by us
flex: none

Expands to 0 0 auto: fixed at its natural size, never grows or shrinks

From the docs, not run by us
flex-basis: 200px

Starting main-axis size before free space is shared out; overrides width in a row container

From the docs, not run by us
order: -1

Move one item visually before its siblings (initial value 0); ties keep source order

Does not change tab or reading order

From the docs, not run by us

justify-items and justify-self do nothing in a flex container (see https://developer.mozilla.org/en-US/docs/Web/CSS/place-items); use justify-content or auto margins on the main axis.

justify-content: space-between

Spread items along the main axis with the first and last flush against the edges

Try it: Flexbox PlaygroundFrom the docs, not run by us
justify-content: space-evenly

Equal space before, between and after every item

From the docs, not run by us
align-items: center

Centre items on the cross axis; the default stretch makes them fill the line's height

From the docs, not run by us
align-items: baseline

Line items up on their first text baseline, handy for mixed font sizes in a toolbar

From the docs, not run by us
align-self: flex-end

Override the container's align-items for one item only

From the docs, not run by us
align-content: space-between

Distribute wrapped lines on the cross axis; has no effect when flex-wrap is nowrap

From the docs, not run by us

A flex item's min-width: auto resolves to roughly its min-content width, so a long URL or a <pre> stops the item shrinking. A scroll container (overflow other than visible) gets an automatic minimum of 0 instead.

.row { display: flex; justify-content: center; align-items: center; }

Centre a single child both ways inside a container with a set height

From the docs, not run by us
.bar > .push { margin-left: auto; }

Push one item (say a logout button) to the far end of a nav row

From the docs, not run by us
.main { flex: 1; min-width: 0; }

Let a content column shrink below its longest word so it stops overflowing the row

From the docs, not run by us
.avatar { flex: none; }

Stop an icon or avatar from being squashed when the text beside it is long

From the docs, not run by us
.cards { display: flex; flex-wrap: wrap; gap: 1rem; } .card { flex: 1 1 15rem; }

Wrapping card row where each card is at least about 15rem before it wraps

Try it: flexbox-code-generatorFrom the docs, not run by us

Worked example: Fix a sidebar layout where long text pushes the page sideways

The setup: a fixed sidebar and a main column. The main column holds a code block with a long line.

.layout { display: flex; gap: 1.5rem; }
.sidebar { flex: 0 0 16rem; }
.main { flex: 1; }
.main pre { overflow-x: auto; }

You'd expect the pre to scroll. It doesn't: the whole page gets a horizontal scrollbar. The reason is that .main is a flex item with the default min-width: auto, so its minimum width is the width of that long line. flex: 1 lets it grow, but it can never shrink below that minimum.

.main { flex: 1; min-width: 0; }

With min-width: 0 the main column is allowed to be narrower than its content. It now fills whatever the sidebar and gap leave over, and the pre inside it becomes the element that overflows, so its own overflow-x: auto finally kicks in and you get a scrollbar on the code block only. Setting overflow: hidden on .main would also work, since a scroll container gets a zero automatic minimum, but it clips focus rings and dropdowns, so min-width: 0 is the safer default. In a column-direction container the same problem shows up vertically and the fix is min-height: 0.

Common mistakes

  • Expecting flex: 1 and flex: auto to give the same widths.

    flex: 1 starts from a zero basis so items end up equal; flex: auto starts from content size. Pick flex: 1 for equal columns.

  • Text or a code block overflows the container even though the item has flex: 1.

    Add min-width: 0 (or min-height: 0 in a column) to the flex item so it can shrink below its content.

  • Setting align-content and seeing nothing change.

    It only acts on wrapped lines. Add flex-wrap: wrap, or use align-items for a single line.

  • Using order or row-reverse to fix the reading order of a component.

    Change the HTML instead. Keyboard focus and screen readers follow the DOM, so visual reordering confuses them.

  • Adding justify-items: center to a flex container and wondering why nothing moves.

    justify-items is ignored in flexbox. Use justify-content on the container or auto margins on the item.

  • Spacing items with margin-right and then fighting the extra space on the last child.

    Use gap on the container; it only puts space between items, and it works on wrapped lines too.