The essentials

Quick reference

One focused task per row. Jump to the related section for complete, working examples.

UseSyntaxExamples
Create a flex containerdisplay: flex;View examples
Flow in a rowflex-direction: row;View examples
Flow in a columnflex-direction: column;View examples
Allow wrappingflex-wrap: wrap;View examples
Space items evenlygap: 1rem;View examples
Set row spacingrow-gap: 2rem;View examples
Set column spacingcolumn-gap: 1rem;View examples
Align at main-axis startjustify-content: flex-start;View examples
Center on the main axisjustify-content: center;View examples
Separate items evenlyjustify-content: space-between;View examples
Align at cross-axis startalign-items: flex-start;View examples
Center on the cross axisalign-items: center;View examples
Stretch on the cross axisalign-items: stretch;View examples
Set flexible item sizingflex: 1 1 16rem;View examples
Push an item to the endmargin-inline-start: auto;View examples

Flexbox arranges items along one main axis and one cross axis. Start on the parent with display: flex, then choose direction, wrapping, spacing, and item behavior.

Step by step

Detailed examples

01

Create a flex formatting context

Apply display: flex to the parent. Only its direct children become flex items; deeper descendants keep their existing layout unless they also become flex containers.

Navigation row
.navigation {
  display: flex;
}

.navigation > a {
  padding: 0.75rem 1rem;
}
Back to quick reference ↑
02

Choose the main-axis direction

Row follows the document's inline direction, while column follows the block direction. Prefer logical layout behavior instead of assuming left-to-right content.

Responsive direction
.profile {
  display: flex;
  flex-direction: column;
}

@media (min-width: 48rem) {
  .profile {
    flex-direction: row;
  }
}
Back to quick reference ↑
03

Wrap items onto new lines

Flex containers do not wrap by default. Enable wrapping for rows of cards, tags, or controls that should remain usable at narrow widths.

Wrapping tag list
.tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}
Back to quick reference ↑
04

Add spacing with gap

Gap inserts space only between flex items. It avoids special rules for first and last child margins and works in both row and column directions.

Different row and column gaps
.card-grid {
  display: flex;
  flex-wrap: wrap;
  row-gap: 2rem;
  column-gap: 1rem;
}
Back to quick reference ↑
05

Align on both axes

Justify-content works on the main axis; align-items works on the cross axis. The physical direction changes when flex-direction changes.

Centered empty state
.empty-state {
  min-height: 20rem;
  display: flex;
  justify-content: center;
  align-items: center;
}
Toolbar with separated groups
.toolbar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.toolbar__actions {
  margin-inline-start: auto;
}
Back to quick reference ↑
06

Control how individual items grow and shrink

The flex shorthand sets growth, shrinking, and starting size. A useful card pattern allows growth and shrinking from a readable minimum basis.

Responsive cards without a media query
.cards {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
}

.card {
  flex: 1 1 16rem;
}
Back to quick reference ↑

Local code tester

Experiment with Flexbox

Change the direction, wrapping, alignment, gap, or item basis, then update the sandboxed preview.

Runs in your browser
Preview

Sources and further reading

References

Authoritative documentation used to verify and expand this cheat sheet.

  1. MDN Web DocsCSS flexible box layoutdeveloper.mozilla.org
  2. MDN Web DocsCSS box alignmentdeveloper.mozilla.org
  3. W3CCSS Flexible Box Layout Module Level 1w3.org

Help us improve

Found a typo or missing example?

Tell us what would make this cheat sheet clearer, more complete, or more useful.

Share feedback