{ ILoveJS }

CSS Flexbox Cheatsheet

css

Complete CSS Flexbox reference with visual descriptions.

8 sections · 50 items

Display & Direction

display
display: flex | inline-flex;

Defines a flex container and enables flex context for direct children

css
.container {
  display: flex;
}
flex-direction
flex-direction: row | row-reverse | column | column-reverse;

Establishes the main axis direction for flex items

css
.container {
  flex-direction: column;
}
flex-direction: row
flex-direction: row;

Items are placed left to right in ltr (default)

css
.container { flex-direction: row; }
flex-direction: row-reverse
flex-direction: row-reverse;

Items are placed right to left in ltr

css
.container { flex-direction: row-reverse; }
flex-direction: column
flex-direction: column;

Items are placed top to bottom

css
.container { flex-direction: column; }
flex-direction: column-reverse
flex-direction: column-reverse;

Items are placed bottom to top

css
.container { flex-direction: column-reverse; }

Wrapping & Flow

flex-wrap
flex-wrap: nowrap | wrap | wrap-reverse;

Controls whether flex items wrap onto multiple lines

css
.container {
  flex-wrap: wrap;
}
flex-wrap: nowrap
flex-wrap: nowrap;

All items stay on one line (default)

css
.container { flex-wrap: nowrap; }
flex-wrap: wrap
flex-wrap: wrap;

Items wrap onto multiple lines from top to bottom

css
.container { flex-wrap: wrap; }
flex-wrap: wrap-reverse
flex-wrap: wrap-reverse;

Items wrap onto multiple lines from bottom to top

css
.container { flex-wrap: wrap-reverse; }
flex-flow
flex-flow: <flex-direction> <flex-wrap>;

Shorthand for flex-direction and flex-wrap combined

css
.container {
  flex-flow: row wrap;
}

Main Axis Alignment (justify-content)

justify-content
justify-content: flex-start | flex-end | center | space-between | space-around | space-evenly;

Aligns items along the main axis

css
.container {
  justify-content: center;
}
justify-content: flex-start
justify-content: flex-start;

Items packed toward the start of main axis (default)

css
.container { justify-content: flex-start; }
justify-content: flex-end
justify-content: flex-end;

Items packed toward the end of main axis

css
.container { justify-content: flex-end; }
justify-content: center
justify-content: center;

Items centered along the main axis

css
.container { justify-content: center; }
justify-content: space-between
justify-content: space-between;

Items evenly distributed with first item at start and last at end

css
.container { justify-content: space-between; }
justify-content: space-around
justify-content: space-around;

Items evenly distributed with equal space around each

css
.container { justify-content: space-around; }
justify-content: space-evenly
justify-content: space-evenly;

Items distributed with equal space between all items and edges

css
.container { justify-content: space-evenly; }

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

align-items
align-items: stretch | flex-start | flex-end | center | baseline;

Aligns items along the cross axis on the current line

css
.container {
  align-items: center;
}
align-items: stretch
align-items: stretch;

Items stretch to fill the container cross axis (default)

css
.container { align-items: stretch; }
align-items: flex-start
align-items: flex-start;

Items aligned to the start of the cross axis

css
.container { align-items: flex-start; }
align-items: flex-end
align-items: flex-end;

Items aligned to the end of the cross axis

css
.container { align-items: flex-end; }
align-items: center
align-items: center;

Items centered on the cross axis

css
.container { align-items: center; }
align-items: baseline
align-items: baseline;

Items aligned by their text baselines

css
.container { align-items: baseline; }
align-content
align-content: flex-start | flex-end | center | space-between | space-around | stretch;

Aligns flex lines when there is extra space on the cross axis

css
.container {
  flex-wrap: wrap;
  align-content: space-between;
}
align-content: stretch
align-content: stretch;

Lines stretch to fill remaining space (default)

css
.container { align-content: stretch; }
align-content: space-between
align-content: space-between;

Lines evenly distributed with first line at start and last at end

css
.container { align-content: space-between; }

Gap Properties

gap
gap: <row-gap> <column-gap>;

Sets spacing between flex items in both directions

css
.container {
  display: flex;
  gap: 20px;
}
row-gap
row-gap: <length>;

Sets spacing between rows of flex items

css
.container {
  row-gap: 15px;
}
column-gap
column-gap: <length>;

Sets spacing between columns of flex items

css
.container {
  column-gap: 10px;
}
gap with two values
gap: <row-gap> <column-gap>;

First value sets row gap and second sets column gap

css
.container {
  gap: 20px 10px;
}

Flex Item Sizing

flex-grow
flex-grow: <number>;

Defines how much an item should grow relative to siblings

css
.item {
  flex-grow: 1;
}
flex-shrink
flex-shrink: <number>;

Defines how much an item should shrink relative to siblings

css
.item {
  flex-shrink: 0;
}
flex-basis
flex-basis: <length> | auto | content;

Defines the initial main size of a flex item before growing or shrinking

css
.item {
  flex-basis: 200px;
}
flex shorthand
flex: <grow> <shrink> <basis>;

Shorthand for flex-grow flex-shrink and flex-basis combined

css
.item {
  flex: 1 0 auto;
}
flex: auto
flex: auto;

Equivalent to flex: 1 1 auto - item can grow and shrink

css
.item { flex: auto; }
flex: none
flex: none;

Equivalent to flex: 0 0 auto - item is fully inflexible

css
.item { flex: none; }
flex: 1
flex: 1;

Equivalent to flex: 1 1 0% - items share space equally

css
.item { flex: 1; }

Flex Item Ordering & Alignment

order
order: <integer>;

Controls the order in which flex items appear in the container

css
.item {
  order: 2;
}
order: negative
order: -1;

Negative values place items before items with default order of 0

css
.item-first { order: -1; }
align-self
align-self: auto | flex-start | flex-end | center | baseline | stretch;

Overrides container align-items for individual flex items

css
.item {
  align-self: flex-end;
}
align-self: auto
align-self: auto;

Item uses the parent containers align-items value (default)

css
.item { align-self: auto; }
align-self: center
align-self: center;

Individual item is centered on the cross axis

css
.item { align-self: center; }
align-self: stretch
align-self: stretch;

Individual item stretches to fill the container

css
.item { align-self: stretch; }

Common Flexbox Patterns

Center both axes
display: flex; justify-content: center; align-items: center;

Perfect centering of content horizontally and vertically

css
.container {
  display: flex;
  justify-content: center;
  align-items: center;
}
Equal columns
display: flex; .item { flex: 1; }

Creates equal width columns that share available space

css
.container { display: flex; }
.item { flex: 1; }
Sticky footer
display: flex; flex-direction: column; .main { flex: 1; }

Footer sticks to bottom when content is short

css
.page {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}
.main { flex: 1; }
Navigation bar
display: flex; justify-content: space-between; align-items: center;

Common navbar layout with logo left and nav items right

css
.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
Card grid
display: flex; flex-wrap: wrap; gap: 20px;

Responsive card layout that wraps with consistent spacing

css
.cards {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
}
.card { flex: 1 1 300px; }
Push item to end
margin-left: auto;

Pushes a single item to the far end using auto margins

css
.nav { display: flex; }
.logout { margin-left: auto; }

Related Content