CSS Flexbox

Beginner
8 min read
Updated: Nov 2026
3 examples
One axis at a time

Definition and Usage

CSS Flexbox lays items out in one direction: a row or a column. Set display: flex on the parent. That parent is the flex container. Its direct children are the flex items.

The main axis is the line they follow. flex-direction: row is the default, so the line runs across the page. The cross axis runs the other way. Items can grow to share leftover space, or stay at the size of their content.

Flexbox is one axis. Grid is both.

A flex container handles a row, or a column. flex-wrap: wrap can start another line, and each line is laid out on its own. Rows and columns together are a job for CSS Grid.

CSS Syntax

The container sets the axis. flex is written on the item:

CSS
.row {
  display: flex;          /* container; children are items */
  flex-direction: row;    /* main axis: across. This is the default */
  gap: 10px;
}

.item {
  flex: 1;                /* flex-grow 1, flex-shrink 1, flex-basis 0 */
}

.stack {
  display: flex;
  flex-direction: column; /* main axis: down the page */
  align-items: center;    /* cross axis: center each item */
}

Core Ideas

The defaults a CSS Flexbox tutorial starts from:

IdeaExampleRemember
Containerdisplay: flexDirect children become flex items
Main axisflex-direction: rowDefault. Items start in one line
Cross axisalign-items: stretchDefault. Short items match the tallest
Free spaceflex: 1Equal share. Same as 1 1 0
Pack the rowjustify-content: centerMoves items along the main axis
A columnflex-direction: columnThe main axis now runs downward

CSS Examples

Three flex containers. View Output shows the axis the browser uses.

Example 1 — Lay items in a row

display: flex puts the children on one row. The default align-items: stretch makes every item as tall as the tallest one.

CSS
.row {
  display: flex;
  gap: 10px;
}

/* items: One, Two, and a taller Three */
Try It Yourself

How It Works

  1. display: flex makes .row the container. One, Two, and Three are the items.
  2. flex-direction: row is already the default, so the main axis runs across.
  3. align-items: stretch is the default on the cross axis. One and Two grow to the height of Three.

Example 2 — Share the free space with flex: 1

Without flex, spare space sits after the last item. flex: 1 gives each item an equal share of that space.

CSS
.row {
  display: flex;
  gap: 10px;
}

.item {
  flex: 1;
}
Try It Yourself

How It Works

  1. flex: 1 sets grow to 1, shrink to 1, and basis to 0.
  2. Basis 0 means the free space is the whole row, after the gap.
  3. The same grow value on each item splits that space into three equal widths.

Example 3 — Switch the main axis to a column

flex-direction: column stacks the items. align-items: center centers each one on the cross axis, which is now horizontal.

CSS
.stack {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}
Try It Yourself

How It Works

  1. column makes the main axis run downward. One, then the wider item, then Three.
  2. align-items now works left to right, because that is the cross axis.
  3. center places each item in the middle of the row, at its own width.

Frequently Asked Questions

Flexbox lays out a parent’s direct children along one axis at a time: a row or a column. Set display: flex on the parent. That parent is the flex container. Each direct child is a flex item. The main axis is the direction they line up. The cross axis runs across that direction.
flex-direction is row, so items sit in one line from the start edge. flex-wrap is nowrap, so they stay on that line. flex-grow is 0, so they stay at their content size and the spare space sits after the last item. align-items is stretch, so a short item grows to the height of the tallest item on the cross axis.
flex: 1 is the shorthand for flex-grow: 1, flex-shrink: 1, and flex-basis: 0. Each item starts from zero and takes an equal share of the free space on the main axis. Three items with flex: 1 are the same width in a row.
justify-content packs items along the main axis. In a row, space-between puts the spare space between the items. align-items aligns them on the cross axis. stretch is the default. center lines them up in the middle of that axis.
No. Only the direct children of the flex container are flex items. A grandchild is laid out by its own parent. To lay it out with Flexbox, set display: flex on that parent too.

Next: Length Units

Choose px, em, rem, and the other sizes.

Length units tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful