CSSFlexbox Cheat Sheet

Flexbox Cheatsheet

A complete, practical reference for CSS Flexbox. Flexbox is a one-dimensional layout system: it distributes space along a single axis (a row or a column) and gives you powerful alignment on both axes. Bookmark this page — it covers every container property, every item property, and the recipes you reach for daily.

Note
Flexbox has two axes: the main axis (the direction items flow, set by flex-direction) and the cross axis (perpendicular to it). justify-content works on the main axis; align-items works on the cross axis. Almost every flexbox confusion comes from forgetting which axis is which.
Container Properties (set on the parent)

Property

Values

What it does

display

flex, inline-flex

Turns the element into a flex container; direct children become flex items

flex-direction

row (default), row-reverse, column, column-reverse

Sets the main axis direction

flex-wrap

nowrap (default), wrap, wrap-reverse

Allows items to break onto multiple lines

flex-flow

direction + wrap

Shorthand: flex-flow: row wrap

justify-content

flex-start, flex-end, center, space-between, space-around, space-evenly

Distributes items along the main axis

align-items

stretch (default), flex-start, flex-end, center, baseline

Aligns items along the cross axis within a line

align-content

stretch, flex-start, flex-end, center, space-between, space-around

Distributes multiple wrapped lines on the cross axis (needs flex-wrap: wrap)

gap

length, or row-gap column-gap

Space between items — use this instead of margins

CSS
/* The canonical flex container */
.container {
  display: flex;
  flex-direction: row;            /* main axis: horizontal */
  flex-wrap: wrap;                /* allow wrapping */
  justify-content: space-between; /* main-axis distribution */
  align-items: center;            /* cross-axis alignment */
  gap: 1rem;                      /* spacing between items */
}
justify-content Values Visualized

CSS
/* Main-axis distribution (row direction shown) */

justify-content: flex-start;    /* |AAA BBB CCC       |                    */
justify-content: flex-end;      /* |       AAA BBB CCC|                    */
justify-content: center;        /* |   AAA BBB CCC    |                    */
justify-content: space-between; /* |AAA    BBB    CCC | edges touch        */
justify-content: space-around;  /* | AAA   BBB   CCC  | half-space at ends */
justify-content: space-evenly;  /* |  AAA  BBB  CCC   | equal everywhere   */
align-items Values Visualized

CSS
/* Cross-axis alignment (in row direction = vertical alignment) */

align-items: stretch;    /* items fill container height (default) */
align-items: flex-start; /* items sit at the top */
align-items: flex-end;   /* items sit at the bottom */
align-items: center;     /* items vertically centered */
align-items: baseline;   /* text baselines line up — great for
                            buttons next to inputs of different sizes */
Item Properties (set on the children)

Property

Default

What it does

flex-grow

0

How much of the leftover space this item absorbs, proportional to all grow values

flex-shrink

1

How much this item shrinks when space is short (0 = never shrink)

flex-basis

auto

The item's starting size on the main axis before growing/shrinking

flex

0 1 auto

Shorthand for grow shrink basis — always prefer the shorthand

align-self

auto

Overrides the container's align-items for this one item

order

0

Visual reordering — lower numbers appear first

The flex Shorthand Decoded

Shorthand

Expands to

Meaning

flex: 1

1 1 0

Grow and shrink equally, ignore content size — equal columns

flex: auto

1 1 auto

Grow and shrink, but start from content size — bigger content gets more room

flex: none

0 0 auto

Rigid: never grow, never shrink

flex: initial

0 1 auto

The default: don't grow, shrink if needed

flex: 2 1 200px

Start at 200px, take double share of free space, shrink normally

Warning
flex: 1 and flex: auto look similar but behave differently. flex: 1 sets flex-basis: 0, so all items end up the same size regardless of content. flex: auto starts from the content size, so items with more content end up wider.
Recipe: Navbar

CSS
/* Logo left, links right, everything vertically centered */
.navbar {
  display: flex;
  align-items: center;
  gap: 2rem;
  padding: 0.75rem 1.5rem;
}

.navbar .links {
  display: flex;
  gap: 1rem;
  margin-left: auto; /* pushes links to the far right */
}

/* margin-left: auto on a flex item eats all free space —
   the classic way to split a flex row into two groups */
Recipe: Perfect Centering

CSS
/* Center anything on both axes */
.center {
  display: flex;
  justify-content: center; /* main axis */
  align-items: center;     /* cross axis */
  min-height: 100dvh;
}

/* Center a single child even more simply */
.center-child {
  display: flex;
}
.center-child > * {
  margin: auto; /* auto margins center on BOTH axes in flex */
}
Recipe: Equal-Width Columns

CSS
.columns {
  display: flex;
  gap: 1rem;
}

.columns > * {
  flex: 1;      /* flex: 1 1 0 — every column identical width */
  min-width: 0; /* allow content (long words, tables) to shrink */
}
Recipe: Sticky Footer

CSS
body {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
}

main {
  flex: 1; /* main grows, footer stays at the bottom
              even on short pages */
}
Recipe: Responsive Card Row (no media queries)

CSS
.cards {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
}

.cards > .card {
  flex: 1 1 250px; /* aim for 250px, grow to fill the row,
                      wrap when there's no room */
}
Gotchas Checklist
  • min-width: auto — flex items refuse to shrink below their content size by default, so long words or wide images overflow. Fix: min-width: 0 (or overflow: hidden) on the item.

  • justify-content: space-between with wrapped rows leaves an ugly gap when the last row has fewer items — use gap plus flex-grow, or switch to Grid.

  • align-content does nothing on a single line — it only distributes wrapped lines.

  • order changes visual order only, not tab or screen-reader order — use it sparingly for accessibility reasons.

  • Margins do not collapse between flex items, and float, clear, and vertical-align have no effect on them.

  • Percentage heights inside flex items can misbehave — prefer align-items: stretch (the default) or explicit heights.

  • In flex-direction: column, justify-content becomes vertical and align-items horizontal — the axes rotate with the direction.

Quick Decision Guide

You want…

Use

Items in one row/column with smart spacing

Flexbox

Content-driven sizing (nav, toolbar, tag list)

Flexbox

Rows and columns aligned to a shared grid

CSS Grid

Precise placement into a 2D template

CSS Grid

Split one flex row into left/right groups

margin-left: auto on the first right-side item

One item aligned differently from siblings

align-self on that item

Tip
Every major browser DevTools has a flexbox inspector. Click the flex badge next to the element in the Elements panel to overlay the axes, free space, and gaps visually.

Related pages: see the Grid cheatsheet for two-dimensional layouts, and Flexbox vs Grid for choosing between them.