Dev In The Mountain Header
A Developer In The mountains having fun

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 row and top to bottom for column.
  • 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.

ValueMeaning
row (default)left to right (in left-to-right languages)
row-reverseright to left
columntop to bottom
column-reversebottom to top

flex-wrap

ValueMeaning
nowrap (default)all items stay on one line and shrink to fit
wrapitems wrap onto new lines
wrap-reverseitems 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.

ValueMeaning
flex-start (default)packed at the start
flex-endpacked at the end
centercentered
space-betweenfirst and last items at the edges, equal space between the rest
space-aroundequal space around each item, so the edge gaps are half the inner gaps
space-evenlyequal space between all items and the edges

align-items

Places items along the cross axis within their line.

ValueMeaning
stretch (default)stretch to fill the line (unless the item has a set height or width)
flex-startat the cross-axis start
flex-endat the cross-axis end
centercentered
baselinethe 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.

ValueMeaning
normal (default)behaves as stretch
stretchlines stretch to fill the space
flex-start / flex-end / centerlines packed at the start, end, or center
space-between / space-around / space-evenlysame 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/end vs flex-start/flex-end: browsers also accept start and end for the alignment properties. In most layouts they look the same. The difference shows up with row-reverse or column-reverse: flex-start follows the reversed direction, while start follows 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.

ValueMeaning
auto (default)use the item's width (or height for columns), or its content size if neither is set
contentsize it from the content
a length, e.g. 200px, 30%, 10remthat size
0start 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:

ShorthandExpands toWhat it does
flex: initial0 1 autothe default: sized by content, can shrink, won't grow
flex: auto1 1 autogrows and shrinks, starting from content size
flex: none0 0 autofixed at content size
flex: 11 1 0%shares the space equally, whatever the content
flex: 22 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 (or min-height: auto in a column), so long words, <pre> blocks, and wide tables can overflow the container. Add min-width: 0 (or overflow: hidden) to the item.
  • align-content does nothing on a single line. Use align-items unless the items wrap.
  • flex: 1 and flex: auto aren't the same. flex: 1 ignores content size and splits the space evenly. flex: auto starts 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, and vertical-align have no effect on flex items.

More


Quick reference chart

Every property on one page. Bold marks the default.

Container

PropertyValuesAxis
displayflex, inline-flexβ€”
flex-directionrow, row-reverse, column, column-reversesets main
flex-wrapnowrap, wrap, wrap-reverseβ€”
flex-flow<direction> <wrap>, row nowrapβ€”
gaplength, <row-gap> <column-gap>, 0 (normal)both
justify-contentflex-start, flex-end, center, space-between, space-around, space-evenlymain
align-itemsstretch, flex-start, flex-end, center, baselinecross
align-contentnormal, stretch, flex-start, flex-end, center, space-between, space-around, space-evenlycross (multi-line only)
place-content<align-content> <justify-content>both

Item

PropertyValuesDefault
orderinteger0
flex-grownumber β‰₯ 00
flex-shrinknumber β‰₯ 01
flex-basisauto, content, length, %auto
flex<grow> <shrink> <basis>, initial, auto, none, number0 1 auto
align-selfauto, stretch, flex-start, flex-end, center, baselineauto

Which property do I want?

I want to…Use
line items up in a row or columnflex-direction on the container
let items wrap onto new linesflex-wrap: wrap
add space between itemsgap
move items along the main axisjustify-content
move items along the cross axisalign-items
move one item on the cross axisalign-self on the item
space out wrapped linesalign-content
make an item fill the leftover spaceflex: 1 on the item
stop an item shrinkingflex-shrink: 0 (or flex: none)
push one item to the far endmargin-left: auto (or margin-top: auto in a column)
fix a long-content overflowmin-width: 0 on the item
More places to find me
Mental Health
follow me on Mastodon