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
| Syntax | What it does |
|---|---|
display: flex | Make an element a block-level flex container; its children become flex items. |
display: inline-flex | A flex container that sits inline with surrounding text. |
flex-direction: row | Lay items out left to right. The default, and it defines the main axis. |
flex-direction: column | Lay items out top to bottom, which swaps what the main and cross axes mean. |
flex-direction: row-reverse | Right to left. Visual order only — focus and screen-reader order stay in DOM order. |
flex-direction: column-reverse | Bottom to top. |
flex-wrap: nowrap | Force everything onto one line. The default, and the usual cause of unexpected overflow. |
flex-wrap: wrap | Let items move onto new lines when they run out of room. |
flex-flow: row wrap | Shorthand for flex-direction plus flex-wrap. |
Aligning along the main axis
| Syntax | What it does |
|---|---|
justify-content: flex-start | Pack items at the start. The default. |
justify-content: center | Centre the items as a group. |
justify-content: flex-end | Pack items at the end. |
justify-content: space-between | First and last flush to the edges, equal space between the rest. |
justify-content: space-around | Equal space around each item, so the edge gaps are half the size of the inner ones. |
justify-content: space-evenly | Every gap, edges included, exactly the same size. |
gap: 16px | Space between items without margins. Supported in flex layout in every current browser. |
column-gap / row-gap | Set the two gaps independently. |
Aligning across the cross axis
| Syntax | What it does |
|---|---|
align-items: stretch | Items fill the container's cross size. The default, and why cards end up equal height. |
align-items: center | Centre each item on the cross axis. |
align-items: flex-start / flex-end | Pin items to the start / end of the cross axis. |
align-items: baseline | Line up the items by their text baselines rather than their boxes. |
align-content: center | Position the wrapped lines as a block. Only does anything when the container wraps. |
align-content: space-between | Spread the wrapped lines apart. |
align-self: center | Override align-items for a single item. |
margin-left: auto | Push this item and everything after it to the far end of the row. |
margin: auto | Centre a lone item on both axes. |
Sizing the items
| Syntax | What it does |
|---|---|
flex-grow: 1 | Take a share of the leftover space. 0, the default, means never grow. |
flex-shrink: 0 | Refuse to shrink below the base size. This is how you stop a label being squashed. |
flex-basis: 200px | The size to start from, before growing or shrinking. |
flex: 1 | Shorthand for 1 1 0% — equal columns regardless of their content. |
flex: auto | Shorthand for 1 1 auto — grow, but start from the content's own size. |
flex: none | Shorthand for 0 0 auto — fixed at the content size. |
flex: 0 0 240px | A 240px column that neither grows nor shrinks. |
order: -1 | Move an item earlier visually. Visual only, so use it sparingly. |
min-width: 0 | The fix for an item that refuses to shrink: flex items default to min-width: auto. |
Every flex property at a glance
| Syntax | What it does |
|---|---|
flex-direction | Container. Initial value row. Chooses the main axis. |
flex-wrap | Container. Initial value nowrap. |
flex-flow | Container. Shorthand for flex-direction and flex-wrap. |
justify-content | Container. Initial value flex-start. Distributes items along the main axis. |
align-items | Container. Initial value stretch. Aligns each item on the cross axis. |
align-content | Container. Initial value stretch. Distributes the lines of a wrapped container. |
gap / row-gap / column-gap | Container. Initial value normal, which computes to zero in flex layout. |
order | Item. Initial value 0. Lower numbers paint first. |
flex-grow | Item. Initial value 0. |
flex-shrink | Item. Initial value 1 — which is why items shrink by default. |
flex-basis | Item. Initial value auto, meaning use the item's own width or height. |
align-self | Item. Initial value auto, which means inherit the container's align-items. |
Recipes
| Syntax | What it does |
|---|---|
display: flex; justify-content: center; align-items: center | Centre a child horizontally and vertically. Add min-height for a full-screen hero. |
display: flex; justify-content: space-between; align-items: center | The classic navbar: logo on the left, links on the right, everything vertically centred. |
flex-wrap: wrap; gap: 16px with flex: 1 1 240px on children | Cards that reflow to fit, with no media query at all. |
flex-direction: column; min-height: 100vh with flex: 1 on main | A sticky footer that stays down even on short pages. |
margin-inline-start: auto | Push 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: ellipsis | Truncate long text inside a flex row. |
align-items: flex-start | Stop equal-height stretching when you want items to keep their natural height. |
Gotchas
| Syntax | What it does |
|---|---|
flex-basis beats width | When both are set, flex-basis wins along the main axis. |
min-width: auto | Flex items will not shrink below their content by default, which is why long words overflow a row. |
align-content needs wrapping | On a single-line flex container it has no effect whatsoever. |
gap versus space-between | gap sets a minimum; space-between can add more on top of it. |
flex: 1 versus flex: auto | flex: 1 sets basis to 0 and ignores content size; flex: auto respects it. |
order and row-reverse | They change paint order only. Tab order and screen-reader order follow the DOM. |
Percentage flex-basis | Resolves 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 editorCSS 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.