Developer Tools

CSS Flexbox: The Complete Layout Guide for Beginners

Flexbox, solved: the two axes, container and item properties, the flex shorthand, and the five layouts you will build every week — with working examples.

4 min read·Updated 2026-04-18

Flexbox — officially the Flexible Box Layout Module — is the CSS model built for distributing space along a single dimension. Where floats and margins made layout a fight against the document flow, flexbox lets you center anything, push items to the edges, and reorder elements without touching the HTML. This is the guide to read before you write another line of alignment CSS.

The two axes: everything hangs off these

Every flex container has a main axis and a cross axis. The main axis runs in the direction of flex-direction, which defaults to row (left to right). The cross axis runs across it. Two properties then position items: justify-content distributes items along the main axis, and align-items (or align-content) positions them on the cross axis. Get those four names straight and flexbox stops being magic:

.flex {
display: flex;
flex-direction: row; /* row | row-reverse | column | column-reverse */
justify-content: space-between;
align-items: center;
}

Container properties at a glance

PropertyWhat it controlsCommon values
flex-directionDirection of the main axisrow, row-reverse, column, column-reverse
flex-wrapWhether items wrap to a new linenowrap (default), wrap
justify-contentMain-axis distributionflex-start, center, space-between, space-around, space-evenly
align-itemsCross-axis alignment of each linestretch, flex-start, center, baseline
align-contentCross-axis distribution of wrapped linesflex-start, center, space-between (only matters when wrapping)
gapGutters between itemsgap: 1rem; also row-gap and column-gap

The flex shorthand on items

Three item properties are usually written as one declaration. flex-grow decides how a leftover space is shared, flex-shrink how items surrender space when the container is tight, and flex-basis the starting size before any sharing happens. The shorthand is what you will see in almost every real stylesheet:

  • flex: 1 — grow 1, shrink 1, basis 0%. Three items in a 600px row each take 200px; free space is split evenly.
  • flex: 2 on one item — it absorbs twice the free space of its flex: 1 siblings.
  • flex: 0 1 auto — the default: items keep natural size but shrink when needed.
  • flex: 0 0 240px — fixed 240px columns that never grow or shrink.

With flex-basis: 0%, grow distributes the full container width between items; with a positive basis it distributes only the leftover after the bases are reserved. That one sentence explains most "why did my nav wrap" mysteries.

The centering trick you will use forever

One recipe centers any child both ways on any container: display: flex; justify-content: center; align-items: center. No fixed heights, no negative margins, no table-cell hacks. It is the single most copied flex pattern in existence, and it is the reason flexbox ended a decade of float-based vertical-centering pain.

Five layouts you will build every week

  1. Navbar: display: flex on the nav; flex: 1 on each link grows them to equal widths; add gap for spacing.
  2. Sticky footer: body as display: flex, flex-direction: column, min-height: 100vh; main gets flex: 1 and the footer stays pinned to the bottom when content is short.
  3. Input + button pair: display: flex; gap: 0.5rem; the input takes flex: 1 and the button keeps its natural width.
  4. Badge rows: align-items: center on a row so avatars and names line up regardless of text height.
  5. Card grids without grid: flex-wrap: wrap on a container with each card flex: 1 1 260px.

Flexbox vs Grid: pick the right tool

Flexbox is one-dimensional: it lays out a row or a column. CSS Grid is two-dimensional: it defines rows and columns together and places items into that grid. Navbars, toolbars, groups and small components in a row belong in flexbox. Page-level skeletons with header, sidebar, content and footer regions are grid territory. The moment you find yourself fighting flex wrap to build a two-axis layout, it is time for Grid.

FlexboxGrid
AxesOne dimension per layoutRows and columns together
StrengthsSpace distribution, alignment, orderingExplicit tracks, areas, dense packing
Good startInspect one row or columnSketch the full page skeleton

Prototype before you commit

The Flexbox Generator turns direction, wrap, justification, alignment and gap into a live preview plus copy-paste CSS, so you can confirm the exact arrangement before embedding it. Give the finished rows depth with the Box Shadow Generator, and add a gradient surface behind the whole layout with the Gradient Generator — a polished-looking prototype is a much better starting point for review than a bare wireframe.

Frequently asked questions

What is the difference between align-items and align-content? align-items aligns each item within its own line on the cross axis. align-content distributes whole lines of wrapped items along the cross axis — it does nothing until flex-wrap creates more than one line.

Why is justify-content: space-between not spreading my items? Because the container is only as wide as its content, so there is no free space to distribute. Give the container a width (or let it stretch), then space-between has room to work. The same symptom appears when a single item fills the entire row.

Does flex-direction: column change which axis is which? Yes, and nothing else does so much. With column, the main axis becomes vertical, so justify-content now stacks items top to bottom and align-items handles the horizontal cross axis. Picture the axes rotating 90 degrees and every property swaps meaning accordingly.

Tools used in this guide

Related guides