CSS Flexbox Quick Reference
Flexbox lays items out along one axis, a row or a column. For two-dimensional layouts, where rows and columns both need to line up, use CSS Grid.
Two terms you need:
- Main axis: the direction items flow. It runs left to right for
rowand top to bottom forcolumn. - Cross axis: the axis at 90Β° to the main axis.
justify-* properties work on the main axis. align-* properties work on the cross axis.
Container (parent) properties
display
.container { display: flex; } /* block-level flex container */
.container { display: inline-flex; } /* inline-level flex container */Only the direct children become flex items.
flex-direction
Sets the main axis.
| Value | Meaning |
|---|---|
row (default) | left to right (in left-to-right languages) |
row-reverse | right to left |
column | top to bottom |
column-reverse | bottom to top |
flex-wrap
| Value | Meaning |
|---|---|
nowrap (default) | all items stay on one line and shrink to fit |
wrap | items wrap onto new lines |
wrap-reverse | items wrap, and new lines go above the first |
flex-flow
Shorthand for flex-direction and flex-wrap.
flex-flow: row nowrap; /* default */
flex-flow: column wrap;gap
Space between items. It doesn't add space at the outer edges, so you don't need margin hacks.
gap: 1rem; /* rows and columns */
gap: 1rem 2rem; /* row-gap column-gap */
row-gap: 1rem;
column-gap: 2rem;justify-content
Places items along the main axis.
| Value | Meaning |
|---|---|
flex-start (default) | packed at the start |
flex-end | packed at the end |
center | centered |
space-between | first and last items at the edges, equal space between the rest |
space-around | equal space around each item, so the edge gaps are half the inner gaps |
space-evenly | equal space between all items and the edges |
align-items
Places items along the cross axis within their line.
| Value | Meaning |
|---|---|
stretch (default) | stretch to fill the line (unless the item has a set height or width) |
flex-start | at the cross-axis start |
flex-end | at the cross-axis end |
center | centered |
baseline | the first lines of text line up |
align-content
Places the lines along the cross axis when there's extra space. It only does anything when items wrap onto more than one line.
| Value | Meaning |
|---|---|
normal (default) | behaves as stretch |
stretch | lines stretch to fill the space |
flex-start / flex-end / center | lines packed at the start, end, or center |
space-between / space-around / space-evenly | same as justify-content |
place-content
Shorthand for align-content and justify-content.
place-content: center; /* both */
place-content: center space-between; /* align-content justify-content */
start/endvsflex-start/flex-end: browsers also acceptstartandendfor the alignment properties. In most layouts they look the same. The difference shows up withrow-reverseorcolumn-reverse:flex-startfollows the reversed direction, whilestartfollows the writing direction.
Item (child) properties
order
An integer. Items are laid out from lowest to highest. The default is 0, and negative values are allowed.
.first { order: -1; }This only changes the visual order. Screen readers and keyboard tab order still follow the HTML source order, so don't use it to reorder content that has meaning.
flex-grow
A unitless number: how much of the leftover space the item takes. The default is 0, so items don't grow.
If one item has flex-grow: 2 and the others have 1, it gets twice as much of the extra space as each of them. It is not twice as wide.
flex-shrink
A unitless number: how much the item shrinks when there isn't enough room. The default is 1. Use 0 to stop an item shrinking.
flex-basis
The item's starting size on the main axis, before space is shared out.
| Value | Meaning |
|---|---|
auto (default) | use the item's width (or height for columns), or its content size if neither is set |
content | size it from the content |
a length, e.g. 200px, 30%, 10rem | that size |
0 | start from zero, so all space is shared out by flex-grow |
flex
Shorthand for flex-grow, flex-shrink, and flex-basis. Use this rather than setting the three properties separately.
flex: 0 1 auto; /* default */
flex: <grow> <shrink> <basis>;The common values:
| Shorthand | Expands to | What it does |
|---|---|---|
flex: initial | 0 1 auto | the default: sized by content, can shrink, won't grow |
flex: auto | 1 1 auto | grows and shrinks, starting from content size |
flex: none | 0 0 auto | fixed at content size |
flex: 1 | 1 1 0% | shares the space equally, whatever the content |
flex: 2 | 2 1 0% | gets twice the share of a flex: 1 sibling |
align-self
Overrides the container's align-items for one item.
Values: auto (default, uses the parent's align-items), stretch, flex-start, flex-end, center, baseline.
Common recipes
Center something both ways
.container {
display: flex;
justify-content: center;
align-items: center;
}Push one item to the far end
Auto margins take up all the free space on that side.
.nav { display: flex; gap: 1rem; }
.nav .logout { margin-left: auto; }Equal-width columns
.row { display: flex; gap: 1rem; }
.row > * { flex: 1; }Fixed sidebar, flexible content
.layout { display: flex; }
.sidebar { flex: 0 0 250px; }
.main { flex: 1; min-width: 0; }Wrapping cards
.cards {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.card { flex: 1 1 250px; } /* at least 250px, then grow to fill the row */Footer at the bottom of a short page
body {
display: flex;
flex-direction: column;
min-height: 100vh;
}
main { flex: 1; }Gotchas
- Items won't shrink below their content. Flex items have
min-width: auto(ormin-height: autoin a column), so long words,<pre>blocks, and wide tables can overflow the container. Addmin-width: 0(oroverflow: hidden) to the item. align-contentdoes nothing on a single line. Usealign-itemsunless the items wrap.flex: 1andflex: autoaren't the same.flex: 1ignores content size and splits the space evenly.flex: autostarts from content size, so items with more content end up wider.- Percentage heights in columns need the container to have a set height.
float,clear, andvertical-alignhave no effect on flex items.
More
- MDN: Basic concepts of flexbox
- CSS-Tricks: A Complete Guide to Flexbox
- Flexbox Froggy: a game for practicing the alignment properties
Quick reference chart
Every property on one page. Bold marks the default.
Container
| Property | Values | Axis |
|---|---|---|
display | flex, inline-flex | β |
flex-direction | row, row-reverse, column, column-reverse | sets main |
flex-wrap | nowrap, wrap, wrap-reverse | β |
flex-flow | <direction> <wrap>, row nowrap | β |
gap | length, <row-gap> <column-gap>, 0 (normal) | both |
justify-content | flex-start, flex-end, center, space-between, space-around, space-evenly | main |
align-items | stretch, flex-start, flex-end, center, baseline | cross |
align-content | normal, stretch, flex-start, flex-end, center, space-between, space-around, space-evenly | cross (multi-line only) |
place-content | <align-content> <justify-content> | both |
Item
| Property | Values | Default |
|---|---|---|
order | integer | 0 |
flex-grow | number β₯ 0 | 0 |
flex-shrink | number β₯ 0 | 1 |
flex-basis | auto, content, length, % | auto |
flex | <grow> <shrink> <basis>, initial, auto, none, number | 0 1 auto |
align-self | auto, stretch, flex-start, flex-end, center, baseline | auto |
Which property do I want?
| I want to⦠| Use |
|---|---|
| line items up in a row or column | flex-direction on the container |
| let items wrap onto new lines | flex-wrap: wrap |
| add space between items | gap |
| move items along the main axis | justify-content |
| move items along the cross axis | align-items |
| move one item on the cross axis | align-self on the item |
| space out wrapped lines | align-content |
| make an item fill the leftover space | flex: 1 on the item |
| stop an item shrinking | flex-shrink: 0 (or flex: none) |
| push one item to the far end | margin-left: auto (or margin-top: auto in a column) |
| fix a long-content overflow | min-width: 0 on the item |

