CSS Flexbox Introduction
Flexbox is the layout system that changed CSS forever. Before it, centering something vertically was a notorious challenge. With Flexbox, it's one line. It makes rows, columns, navigation bars, card grids, and responsive layouts intuitive and powerful — all without floats or hacks.
flex-direction — row and column
flex-wrap — single vs
multi-linejustify-content and align-itemsWhat Is Flexbox?
Flexbox (Flexible Box Layout) is a one-dimensional CSS layout model. You apply it to a container and its direct children become flex items that you can arrange, align, size and distribute along a single axis — either horizontal or vertical.
display: flex on a parent turns all
direct children into flex items. That's it — the browser does the rest with sensible defaults.block children stack vertically
children become a row instantly
/* One property — that's all it takes */
.container {
display: flex;
}
/* children automatically become flex items:
- arranged in a row (left to right) by default
- shrink to fit if needed
- same height as the tallest item */
/* inline-flex: flex container that flows inline */
.inline-container {
display: inline-flex;
}
Flex Container vs Flex Items
Flexbox has two types of participants. Properties are split between the container (the parent) and the items (the children). This is important — putting an item property on the container (or vice versa) does nothing.
| Applied to | Properties | Controls |
|---|---|---|
| Container | flex-direction, flex-wrap,
justify-content, align-items, align-content,
gap
|
How all children are arranged, aligned and spaced |
| Items | flex-grow, flex-shrink,
flex-basis, flex, align-self, order
|
How each individual item sizes itself and overrides container alignment |
Direct children are automatically flex items. Grandchildren are NOT flex items.
Main Axis & Cross Axis
Every flex container has two axes. The main axis is the primary
direction items flow — set by flex-direction. The cross
axis is always perpendicular to the main axis. Understanding this is the key to understanding all
the alignment properties.
Cross axis → vertical (top to bottom)
justify-content aligns horizontallyalign-items aligns verticallyCross axis → horizontal (left to right)
justify-content aligns verticallyalign-items aligns horizontallyflex-direction. justify-content always controls the main axis and align-items always controls the cross axis. Remember these two
rules and Flexbox alignment becomes predictable in any direction.
flex-direction — Setting the Main Axis
flex-direction sets which direction items flow —
horizontally in a row or vertically in a column. It also has reverse variants that reverse the order:
left → right
right → left (1 is at right)
top → bottom
bottom → top (1 is at bottom)
.container { display: flex; }
.row { flex-direction: row; } /* ← default */
.row-reverse { flex-direction: row-reverse; }
.column { flex-direction: column; }
.column-reverse { flex-direction: column-reverse; }
/* Real-world: responsive nav → vertical on mobile */
.nav {
display: flex;
flex-direction: row;
}
@media (max-width: 640px) {
.nav { flex-direction: column; }
}
flex-wrap — Single vs Multi-Line
By default flex items try to fit on one line — they shrink if needed.
flex-wrap: wrap lets items break onto the next line when they don't fit,
creating a responsive multi-line layout:
items wrap to next line ✓
.container {
display: flex;
flex-wrap: wrap; /* items wrap to next line */
}
.container { flex-wrap: nowrap; } /* default — single line */
.container { flex-wrap: wrap-reverse; } /* wraps upward */
/* flex-flow: shorthand for direction + wrap */
.container { flex-flow: row wrap; }
.container { flex-flow: column nowrap; }
/* Common responsive card pattern */
.card-row {
display: flex;
flex-wrap: wrap;
gap: 16px;
}
.card { flex: 1 1 250px; } /* grow/shrink, min 250px */
justify-content — Main Axis Alignment
justify-content distributes items and space
along the main axis. With flex-direction: row
(default), this means horizontal alignment. With column, it means
vertical.
.container { display: flex; }
.jc-start { justify-content: flex-start; } /* default */
.jc-end { justify-content: flex-end; }
.jc-center { justify-content: center; } /* centred */
.jc-between { justify-content: space-between; } /* first/last at edges */
.jc-around { justify-content: space-around; } /* half-space at edges */
.jc-evenly { justify-content: space-evenly; } /* equal space everywhere */
/* Memory rule:
space-between → gap between items, nothing at edges
space-around → half-gap at edges, full gap between
space-evenly → equal gaps everywhere including edges */
align-items — Cross Axis Alignment
align-items aligns items along the cross
axis. With default flex-direction: row, this controls
vertical alignment. Each item can have a different height — this controls where each sits
vertically within the row:
tall
tall
tall
tall
.container { display: flex; }
.ai-stretch { align-items: stretch; } /* default — fill cross axis */
.ai-start { align-items: flex-start; } /* top (in row direction) */
.ai-end { align-items: flex-end; } /* bottom */
.ai-center { align-items: center; } /* vertically centred */
.ai-baseline { align-items: baseline; } /* aligns text baselines */
/* ── The classic perfect centring trick ── */
.centred {
display: flex;
justify-content: center; /* horizontal */
align-items: center; /* vertical */
height: 100vh; /* needs a height */
}
gap — Spacing Between Items
gap adds consistent spacing between flex items
without adding space at the outer edges. It's cleaner than margins on items because it applies
between items only:
margin on items — edge items need special treatment
gap — equal spacing, no edge space ✓
.container {
display: flex;
gap: 16px; /* row gap AND column gap */
}
/* Row and column gap separately */
.container {
row-gap: 12px; /* between rows (with flex-wrap) */
column-gap: 20px; /* between columns */
}
/* Shorthand: gap: row column */
.container { gap: 12px 20px; } /* 12px rows, 20px cols */
/* gap works in Grid too — the same syntax */
Try It Yourself
Experiment with all the Flexbox container properties. Try changing flex-direction, justify-content, and
align-items values:
Quick Check — Chapter 25
flex-direction. With the default flex-direction: row, the main axis is horizontal (left to right). With
column, the main axis is vertical (top to bottom).The cross axis is always perpendicular to the main axis. With
row, the cross axis is vertical. With column, the cross axis is horizontal.This distinction matters because alignment properties are axis-specific:
—
justify-content always aligns along the main axis—
align-items always aligns along the cross axisWhen you switch
flex-direction from row to column, the axes rotate —
so what was horizontal alignment (justify-content) becomes vertical alignment, and vice versa.
space-between — the first item is at the start
edge, the last item is at the end edge. Space is placed only between items. No space at the
outer edges.space-around — each item gets an equal amount of
space on both sides. Items at the edges get half the space of items in the middle (half-unit at edge,
full-unit between items).space-evenly — the space between any two adjacent
items (and from the edges to the first/last item) is exactly equal. This is the most visually even
distribution.Memory: between = no edge space, around = half edge space, evenly = equal everywhere.
align-items: stretch is the default value. It makes
flex items stretch to fill the cross axis of the container. In a row container, this means
items stretch vertically to match the height of the tallest item.This solves a problem that was very hard before Flexbox: equal-height columns. In a card grid where each card has different amounts of content,
align-items: stretch (default) makes all cards the same height — the
height of the tallest card.Practical example: a row of three cards — one with a short title, one with a long description. With stretch (default), all three cards fill to the same height, creating a uniform grid. Without it, each card would be only as tall as its content.
The
stretch effect only applies when items don't have an explicit
height set. If you set height: 100px on an item, stretch is
overridden for that item.
display: flex; — enables flexboxjustify-content: center; — centres along the main axis (horizontally
in row)align-items: center; — centres along the cross axis (vertically in
row)The container also needs a defined height (or a height constraint) — otherwise there's no vertical space to centre within. Common values:
height: 100vh for full screen,
min-height: 400px for a section, or height: 100% relative to a defined parent.This is widely considered one of Flexbox's most impactful contributions to CSS — before it, vertically centring content required complex absolute positioning or table-cell hacks.
flex-wrap: nowrap (default) — all flex items are
forced onto a single line. If there isn't enough space, items shrink or overflow the container. This can
cause horizontal overflow or very narrow items.flex-wrap: wrap — when items would overflow the
container's width, they wrap to the next line. This enables responsive multi-line flex layouts. When
used with flex: 1 1 250px on items, the container responds like a
grid — filling the available space with as many items as fit per row, and wrapping the rest.flex-wrap: wrap is the foundation of the common "responsive flex
grid" pattern, which is simpler than CSS Grid for situations where you don't need precise column counts
— you just want items to fill the available space and wrap naturally.
flex-direction?
justify-content value places the first
item at the start edge and last item at the end edge with no space at the edges?align-items?
- Flexbox is a one-dimensional layout model activated by
display: flexon a container. Direct children become flex items instantly. - Container vs Item properties are separate. Container:
flex-direction,flex-wrap,justify-content,align-items,gap. Item:flex,align-self,order. - Main axis — direction of item flow (set by
flex-direction). Cross axis — perpendicular to main axis. Both axes rotate when direction changes. flex-direction—row(default, left→right),row-reverse,column(top→bottom),column-reverse.flex-wrap—nowrap(default, one line),wrap(multi-line),wrap-reverse. Shorthand:flex-flow: row wrap.justify-content— main axis:flex-start,flex-end,center,space-between,space-around,space-evenly.align-items— cross axis:stretch(default),flex-start,flex-end,center,baseline.- Perfect centring:
display:flex; justify-content:center; align-items:center+ a defined height. gap— spacing between items (not at edges). Supportsrow-gapandcolumn-gapseparately.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.