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: flexTurn the element into a block-level flex container; its direct children become flex items
display: inline-flexSame flex behaviour, but the container itself sits inline in the text flow like a button
flex-direction: row | row-reverse | column | column-reversePick the main axis; the reverse values swap the start and end edges, not the DOM order
flex-wrap: wrapLet items move to new lines instead of overflowing; the default nowrap keeps everything on one line
flex-flow: row wrapShorthand that sets flex-direction and flex-wrap together
gap: 1rem 2remRow gap first, then column gap; one value sets both, and no space is added at the outer edges
Longhand initial values are flex-grow: 0, flex-shrink: 1 and flex-basis: auto.
flex: initialExpands to 0 1 auto: no growing, can shrink, sized from width or content; this is what you get without setting flex
flex: 1Expands to 1 1 0%: grows from a zero basis, so siblings with flex: 1 share space equally
flex: autoExpands to 1 1 auto: grows and shrinks, but starts from its content size so bigger content stays bigger
flex: noneExpands to 0 0 auto: fixed at its natural size, never grows or shrinks
flex-basis: 200pxStarting main-axis size before free space is shared out; overrides width in a row container
order: -1Move one item visually before its siblings (initial value 0); ties keep source order
Does not change tab or reading order
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-betweenSpread items along the main axis with the first and last flush against the edges
justify-content: space-evenlyEqual space before, between and after every item
align-items: centerCentre items on the cross axis; the default stretch makes them fill the line's height
align-items: baselineLine items up on their first text baseline, handy for mixed font sizes in a toolbar
align-self: flex-endOverride the container's align-items for one item only
align-content: space-betweenDistribute wrapped lines on the cross axis; has no effect when flex-wrap is nowrap
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
.bar > .push { margin-left: auto; }Push one item (say a logout button) to the far end of a nav row
.main { flex: 1; min-width: 0; }Let a content column shrink below its longest word so it stops overflowing the row
.avatar { flex: none; }Stop an icon or avatar from being squashed when the text beside it is long
.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
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: 1andflex: autoto give the same widths.flex: 1starts from a zero basis so items end up equal;flex: autostarts from content size. Pickflex: 1for equal columns.Text or a code block overflows the container even though the item has
flex: 1.Add
min-width: 0(ormin-height: 0in a column) to the flex item so it can shrink below its content.Setting
align-contentand seeing nothing change.It only acts on wrapped lines. Add
flex-wrap: wrap, or usealign-itemsfor a single line.Using
orderorrow-reverseto 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: centerto a flex container and wondering why nothing moves.justify-itemsis ignored in flexbox. Usejustify-contenton the container or auto margins on the item.Spacing items with
margin-rightand then fighting the extra space on the last child.Use
gapon the container; it only puts space between items, and it works on wrapped lines too.
Related cheat sheets
Sources
- https://www.w3.org/TR/css-flexbox-1/
- https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_flexible_box_layout/Basic_concepts_of_flexbox
- https://developer.mozilla.org/en-US/docs/Web/CSS/flex
- https://developer.mozilla.org/en-US/docs/Web/CSS/min-width
- https://developer.mozilla.org/en-US/docs/Web/CSS/gap
- https://developer.mozilla.org/en-US/docs/Web/CSS/order
- https://developer.mozilla.org/en-US/docs/Web/CSS/align-content
- https://developer.mozilla.org/en-US/docs/Web/CSS/place-items
How we research: where our facts come from, how often we re-check them, and why no one can pay for a review or a verdict.