Skip to content

CSS Flexbox

Learn CSS Flexbox for one-dimensional layouts — align, distribute, and order items.

What you'll learn

  • What Flexbox is and when to use it over other layout methods
  • Container properties: flex-direction, justify-content, align-items, gap
  • Item properties: flex-grow, flex-shrink, flex-basis, align-self
  • How to center anything with three lines of Flexbox

Concept

CSS Flexbox

Flexbox (Flexible Box Layout) is a CSS layout system designed for arranging elements in one dimension — either a row or a column. It solves the problems that hacks like floats and inline-block were pressed into solving for decades: equal-height columns, vertical centering, distributing space between items, and reordering elements. If you learn one layout tool in CSS, make it Flexbox.

The Container and Its Items

Apply display: flex to a parent element (the flex container), and its direct children become flex items. The container controls the overall arrangement; the items can override their own behavior. This parent-child relationship is the core of how Flexbox works.

Container Properties

  • flex-direction — row (default), column, row-reverse, column-reverse. Sets the main axis.
  • justify-content — aligns items along the main axis: flex-start, center, space-between, space-around, space-evenly.
  • align-items — aligns items along the cross axis: stretch (default), flex-start, center, baseline.
  • flex-wrap — nowrap (default) or wrap. Lets items wrap to the next line instead of shrinking.
  • gap — space between items, without needing margins. gap: 16px is cleaner than margin tricks.

Item Properties

  • flex-grow — how much the item grows to fill free space (0 = don't grow).
  • flex-shrink — how much it shrinks when space is tight (1 = shrink equally).
  • flex-basis — the ideal size before growing or shrinking (like a starting width).
  • align-self — overrides the container's align-items for this one item.

The shorthand flex: 1 sets grow=1, shrink=1, basis=0 — the most common pattern for equal-width items that fill the container.

Centering Anything

The classic CSS problem — "how do I center something?" — has a three-line Flexbox answer:

.center {
  display: flex;
  justify-content: center;
  align-items: center;
}

This centers a child both horizontally and vertically, regardless of its size. Before Flexbox, this required negative margins, table cells, or absolute positioning hacks.

Example

              /* A responsive card row that wraps and centers */
.card-row {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: stretch;
  gap: 20px;
}

.card-row .card {
  flex: 1 1 250px;  /* grow, shrink, basis */
  min-width: 0;     /* prevent overflow */
}

/* A navbar: logo on the left, links on the right */
.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 24px;
}

.navbar .links {
  display: flex;
  gap: 16px;
}

/* A sidebar + main content layout */
.layout {
  display: flex;
  gap: 24px;
}
.layout .sidebar { flex: 0 0 240px; }
.layout .main { flex: 1; }
            

card-row wraps cards and centers them with gap for spacing. flex: 1 1 250px lets cards grow and shrink with a 250px starting size. The navbar uses space-between to push logo and links apart. The layout gives the sidebar a fixed 240px and lets main fill the rest.

Try it

  • SVG Optimizer

    SVG icons are often arranged with Flexbox — optimize and inspect them.

  • Image Color Picker

    Pick colors for your flex container backgrounds and item borders.

Common mistakes

The mistake

Using justify-content for vertical centering when flex-direction is column.

The fix

justify-content works on the main axis. If flex-direction is column, the main axis is vertical, so justify-content controls vertical alignment and align-items controls horizontal. Always know which axis is which.

The mistake

Forgetting min-width: 0 on flex items, causing text to overflow.

The fix

Flex items have a default min-width of auto, which prevents them from shrinking below their content size. Set min-width: 0 (or min-height: 0 for columns) to allow shrinking and prevent overflow.

Related Snippets

Related Cheatsheets

Practice CSS Flexbox

7 exercises

Practice CSS Flexbox

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors