Skip to main content

61 entries · 7 sections

CSS Flexbox Cheat Sheet

Every flexbox property with what it actually does, split into container properties and item properties, plus the layout recipes people reach for and the handful of gotchas that cause most flexbox confusion.

Turning on flex

SyntaxWhat it does
display: flexMake an element a block-level flex container; its children become flex items.
display: inline-flexA flex container that sits inline with surrounding text.
flex-direction: rowLay items out left to right. The default, and it defines the main axis.
flex-direction: columnLay items out top to bottom, which swaps what the main and cross axes mean.
flex-direction: row-reverseRight to left. Visual order only — focus and screen-reader order stay in DOM order.
flex-direction: column-reverseBottom to top.
flex-wrap: nowrapForce everything onto one line. The default, and the usual cause of unexpected overflow.
flex-wrap: wrapLet items move onto new lines when they run out of room.
flex-flow: row wrapShorthand for flex-direction plus flex-wrap.

Aligning along the main axis

SyntaxWhat it does
justify-content: flex-startPack items at the start. The default.
justify-content: centerCentre the items as a group.
justify-content: flex-endPack items at the end.
justify-content: space-betweenFirst and last flush to the edges, equal space between the rest.
justify-content: space-aroundEqual space around each item, so the edge gaps are half the size of the inner ones.
justify-content: space-evenlyEvery gap, edges included, exactly the same size.
gap: 16pxSpace between items without margins. Supported in flex layout in every current browser.
column-gap / row-gapSet the two gaps independently.

Aligning across the cross axis

SyntaxWhat it does
align-items: stretchItems fill the container's cross size. The default, and why cards end up equal height.
align-items: centerCentre each item on the cross axis.
align-items: flex-start / flex-endPin items to the start / end of the cross axis.
align-items: baselineLine up the items by their text baselines rather than their boxes.
align-content: centerPosition the wrapped lines as a block. Only does anything when the container wraps.
align-content: space-betweenSpread the wrapped lines apart.
align-self: centerOverride align-items for a single item.
margin-left: autoPush this item and everything after it to the far end of the row.
margin: autoCentre a lone item on both axes.

Sizing the items

SyntaxWhat it does
flex-grow: 1Take a share of the leftover space. 0, the default, means never grow.
flex-shrink: 0Refuse to shrink below the base size. This is how you stop a label being squashed.
flex-basis: 200pxThe size to start from, before growing or shrinking.
flex: 1Shorthand for 1 1 0% — equal columns regardless of their content.
flex: autoShorthand for 1 1 auto — grow, but start from the content's own size.
flex: noneShorthand for 0 0 auto — fixed at the content size.
flex: 0 0 240pxA 240px column that neither grows nor shrinks.
order: -1Move an item earlier visually. Visual only, so use it sparingly.
min-width: 0The fix for an item that refuses to shrink: flex items default to min-width: auto.

Every flex property at a glance

SyntaxWhat it does
flex-directionContainer. Initial value row. Chooses the main axis.
flex-wrapContainer. Initial value nowrap.
flex-flowContainer. Shorthand for flex-direction and flex-wrap.
justify-contentContainer. Initial value flex-start. Distributes items along the main axis.
align-itemsContainer. Initial value stretch. Aligns each item on the cross axis.
align-contentContainer. Initial value stretch. Distributes the lines of a wrapped container.
gap / row-gap / column-gapContainer. Initial value normal, which computes to zero in flex layout.
orderItem. Initial value 0. Lower numbers paint first.
flex-growItem. Initial value 0.
flex-shrinkItem. Initial value 1 — which is why items shrink by default.
flex-basisItem. Initial value auto, meaning use the item's own width or height.
align-selfItem. Initial value auto, which means inherit the container's align-items.

Recipes

SyntaxWhat it does
display: flex; justify-content: center; align-items: centerCentre a child horizontally and vertically. Add min-height for a full-screen hero.
display: flex; justify-content: space-between; align-items: centerThe classic navbar: logo on the left, links on the right, everything vertically centred.
flex-wrap: wrap; gap: 16px with flex: 1 1 240px on childrenCards that reflow to fit, with no media query at all.
flex-direction: column; min-height: 100vh with flex: 1 on mainA sticky footer that stays down even on short pages.
margin-inline-start: autoPush the remaining items to the end in a way that also works right-to-left.
flex: 1; min-width: 0 with overflow: hidden; text-overflow: ellipsisTruncate long text inside a flex row.
align-items: flex-startStop equal-height stretching when you want items to keep their natural height.

Gotchas

SyntaxWhat it does
flex-basis beats widthWhen both are set, flex-basis wins along the main axis.
min-width: autoFlex items will not shrink below their content by default, which is why long words overflow a row.
align-content needs wrappingOn a single-line flex container it has no effect whatsoever.
gap versus space-betweengap sets a minimum; space-between can add more on top of it.
flex: 1 versus flex: autoflex: 1 sets basis to 0 and ignores content size; flex: auto respects it.
order and row-reverseThey change paint order only. Tab order and screen-reader order follow the DOM.
Percentage flex-basisResolves against the container's main size, which may itself be content-sized.

Worth knowing

  • When a flex row overflows its container, the cause is nearly always min-width: auto on the items. Adding min-width: 0 (or overflow: hidden) to the child that contains the long content fixes it, and it is why the ellipsis recipe needs both properties.
  • Reach for flexbox when you are laying out along one axis and letting content decide the sizes; reach for grid when you are defining a two-dimensional structure up front. A card component's internal stack is flex, the page of cards is grid.
  • flex: 1 is not the same as flex-grow: 1. The shorthand also sets flex-basis to 0, so items become equal width regardless of their content. flex-grow: 1 on its own leaves the basis at auto, so wider content stays wider.

Try a snippet instead of reading about it

Reference is faster when you can run the thing. Paste a snippet into AltF Code, the free in-browser editor — real syntax highlighting for HTML, CSS, JavaScript, Python, SQL, JSON and more, with HTML, CSS and JavaScript running live in the preview. No install and no sign-up.

Open the editor

CSS Flexbox — common questions

How do I centre a div with flexbox?
Put display: flex; justify-content: center; align-items: center on the parent. justify-content centres along the main axis and align-items along the cross axis, so with the default row direction that is horizontal and vertical respectively. Add min-height to the parent, otherwise it is only as tall as its content and vertical centring has nothing to work with.
What is the difference between justify-content and align-items?
justify-content distributes items along the main axis — horizontal in a row, vertical in a column. align-items positions each item on the cross axis, which is the other one. Change flex-direction and the two swap meaning, which is why remembering the axes matters more than remembering the property names.
When should I use flexbox instead of CSS Grid?
Flexbox handles one dimension: a row of buttons, a navbar, a stack that needs to distribute space. Grid handles two at once: page layouts, card galleries with aligned rows and columns, anything where you want a defined structure rather than content-driven sizing. They compose happily — grid the page, flex the components inside it.
Why is my flex item overflowing its container?
Flex items have min-width: auto by default, which means they will not shrink below the intrinsic size of their content — a long URL or an unbroken string keeps the item wide. Set min-width: 0 on the item (or overflow: hidden, which implies it) and it will shrink as intended.