CSS Flexbox Cheat Sheet

css flexbox layout frontend cheatsheet responsive-design

CSS Flexbox is a one-dimensional layout model that lets you distribute space and align items in a container along a single axis. This reference covers every property — for both the flex container and flex items — with values, defaults, and usage examples.

Enabling Flexbox

.container {
  display: flex;        /* block-level flex container */
  display: inline-flex; /* inline-level flex container */
}

All direct children of a flex container automatically become flex items.

Flex Container Properties

These properties are applied to the parent element (display: flex).

Flex Direction & Wrapping

PropertyValuesDefaultDescription
flex-directionrow | row-reverse | column | column-reverserowSets the main axis direction
flex-wrapnowrap | wrap | wrap-reversenowrapControls whether items wrap to new lines
flex-flow<direction> <wrap>row nowrapShorthand for flex-direction + flex-wrap
.container {
  flex-direction: row;         /* left → right */
  flex-direction: column;      /* top → bottom */
  flex-direction: row-reverse; /* right → left */

  flex-wrap: wrap;             /* items wrap onto multiple lines */
  flex-flow: row wrap;         /* shorthand equivalent */
}

Alignment: Main Axis (justify-content)

justify-content aligns items along the main axis (set by flex-direction).

ValueDescription
flex-startPack items at the start (default)
flex-endPack items at the end
centerCenter items along the main axis
space-betweenEqual gaps between items; no gap at edges
space-aroundEqual spacing around each item
space-evenlyEqual spacing between items and edges
.container {
  justify-content: space-between; /* nav links spread across full width */
  justify-content: center;        /* centered button group */
}

Alignment: Cross Axis (align-items & align-content)

PropertyValuesDefaultDescription
align-itemsstretch | flex-start | flex-end | center | baselinestretchAligns items along the cross axis for a single line
align-contentstretch | flex-start | flex-end | center | space-between | space-around | space-evenlystretchAligns multiple lines of items (only applies when flex-wrap: wrap)
.container {
  align-items: center;          /* vertically center items in a row */
  align-items: flex-start;      /* align to top of cross axis */

  /* Multi-line layout */
  flex-wrap: wrap;
  align-content: space-between; /* distribute rows with space between */
}

Gap

PropertyDescription
gap: <value>Space between flex items (row and column)
row-gap: <value>Space between rows (wrapped layouts)
column-gap: <value>Space between columns
.container {
  gap: 16px;            /* 16px between all items */
  gap: 16px 24px;       /* 16px row gap, 24px column gap */
}

Flex Item Properties

These properties are applied to the children of a flex container.

Sizing: flex-grow, flex-shrink, flex-basis

PropertyDefaultDescription
flex-grow0How much an item grows relative to siblings when space is available
flex-shrink1How much an item shrinks relative to siblings when space is tight
flex-basisautoThe initial main-size of the item before growing/shrinking
flex0 1 autoShorthand for flex-grow flex-shrink flex-basis
/* Common flex shorthand patterns */
.item { flex: 1; }       /* flex: 1 1 0% — grow equally, shrink equally */
.item { flex: auto; }    /* flex: 1 1 auto — grow and shrink from natural size */
.item { flex: none; }    /* flex: 0 0 auto — don't grow or shrink */

/* Three-column equal layout */
.col { flex: 1; }

/* Sidebar + main content */
.sidebar { flex: 0 0 250px; }  /* fixed 250px, never flex */
.main    { flex: 1; }          /* takes remaining space */

Individual Alignment (align-self)

align-self overrides align-items for a single item.

ValueDescription
autoInherit from align-items (default)
flex-startAlign to start of cross axis
flex-endAlign to end of cross axis
centerCenter on cross axis
stretchStretch to fill cross axis
baselineAlign to text baseline
.container {
  align-items: flex-start; /* all items top-aligned */
}

.special-item {
  align-self: center; /* this item centered, overrides parent */
}

Order

.item { order: 0; }   /* default — source order */
.item { order: -1; }  /* move item before all order:0 siblings */
.item { order: 1; }   /* move item after all order:0 siblings */

order only affects visual order; DOM order (and accessibility reading order) is unchanged.

Common Flexbox Layout Patterns

Centered Content (horizontal + vertical)

.centered {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
}
.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 24px;
}

.navbar .logo { flex: 0 0 auto; }
.navbar .nav-links { display: flex; gap: 16px; }
.navbar .cta { margin-left: auto; }

Responsive Card Grid

.cards {
  display: flex;
  flex-wrap: wrap;
  gap: 24px;
}

.card {
  flex: 1 1 280px;  /* grow/shrink but minimum 280px wide */
  max-width: 400px;
}
body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

main {
  flex: 1; /* pushes footer to bottom */
}

Quick-Reference Summary

NeedProperty + Value
Horizontal centerjustify-content: center
Vertical centeralign-items: center
Full centerboth of the above
Equal columnsflex: 1 on each child
Fixed sidebarflex: 0 0 <width> on sidebar, flex: 1 on main
Wrap itemsflex-wrap: wrap
Reverse orderflex-direction: row-reverse
Push one item to endmargin-left: auto on that item
Space links evenlyjustify-content: space-between
Stack verticallyflex-direction: column

Flexbox handles the vast majority of one-dimensional layout needs. For grid-based two-dimensional layouts, pair this reference with a CSS Grid cheatsheet. Explore the full DevNook cheatsheets hub for more quick references, including the JavaScript Array Methods Cheat Sheet for front-end development. Check the guides hub for deeper walkthroughs, and visit DevNook tools for utilities to speed up your development workflow.