← All cheat sheets

CSS

CSS Flexbox Cheat Sheet — Properties Reference

Flexbox properties split into two groups — those you set on the container, and those you set on the items. This reference covers both.

Put it into practice with the matching tool.

Try the Flexbox playground →

Advertisement

Container properties

display: flexMake the element a flex container
flex-direction: row | columnMain axis direction (also row-reverse, column-reverse)
justify-content: centerAlign items along the main axis
justify-content: space-betweenEven gaps, no space at the ends
align-items: centerAlign items along the cross axis
align-items: stretchItems fill the cross axis (default)
flex-wrap: wrapAllow items to wrap onto multiple lines
align-content: centerAlign wrapped lines (only with wrapping)
gap: 12pxSpace between items

Item properties

flex: 1Grow to fill available space (shorthand)
flex-grow: 1How much the item grows relative to siblings
flex-shrink: 0Prevent the item from shrinking
flex-basis: 200pxStarting size before growing/shrinking
align-self: flex-endOverride align-items for one item
order: -1Change visual order without changing the HTML

Common recipes

display:flex; justify-content:center; align-items:centerPerfectly center a child both ways
display:flex; justify-content:space-betweenNavbar — logo left, links right
margin-left: autoPush one item to the far right
display:flex; flex-wrap:wrap; gap:16pxResponsive wrapping card row
flex-direction: column; min-height: 100vhSticky footer layout

Flexbox in practice

Main axis and cross axis

Everything in flexbox is relative to two axes. The main axis runs in the flex-direction — horizontally for row, vertically for column. justify-content distributes items along the main axis; align-items aligns them on the cross axis. When you switch to flex-direction: column, the two properties swap their visual effect, which is why centring "stops working" after changing direction. Think in axes, not in left-right and top-bottom.

How flex sizing really works

flex: 1 is shorthand for flex-grow: 1; flex-shrink: 1; flex-basis: 0%, so items share all the space equally regardless of their content. flex: auto uses the content size as the starting point, so larger content gets larger items. flex: none keeps an item at its natural size. When items are too large, they shrink in proportion to flex-shrink multiplied by their basis, which is why an item with long content sometimes refuses to shrink as much as you expect.

The min-width: 0 fix

A flex item will not shrink below its minimum content width by default. Long words, URLs, code blocks and wide images therefore push items beyond the container and cause horizontal overflow. Adding min-width: 0 to the item (or min-height: 0 in a column layout) lets it shrink, after which overflow: hidden or text-overflow: ellipsis can handle the content. This one line fixes a large share of flexbox layout bugs.

Useful patterns

Push one item to the far end with margin-left: auto — the classic way to place a logout button at the right of a navigation bar. Make a card footer stick to the bottom by making the card a column flex container and giving the body flex: 1. Use gap instead of margins between items, so there is no extra space after the last one. And use flex-wrap: wrap with a flex-basis such as 200px for simple responsive rows of tags or cards.

When to switch to grid

If items must line up in rows and columns at the same time — a gallery whose cards align across rows, a form whose labels and inputs line up — CSS Grid expresses that directly. Flexbox wraps each line independently, so items on different lines do not align. Use flexbox for one-dimensional arrangements and grid for two-dimensional layouts; most pages use both.

Related guides

More cheat sheets