Tutorials CSS Flexbox Introduction
Ch 25 / 60
Chapter 25 Beginner 8 min read CSS

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.

What you'll learn
What Flexbox is and how it works
Flex container vs flex items
Main axis and cross axis
flex-direction — row and column
flex-wrap — single vs multi-line
justify-content and align-items

What 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.

One single property enables it
display: flex on a parent turns all direct children into flex items. That's it — the browser does the rest with sensible defaults.
One-dimensional
Flexbox works along one axis at a time — either a row or a column. For two-dimensional grid layouts, use CSS Grid (Chapter 29).
Before and after Flexbox
Before Flexbox: centering vertically required absolute positioning tricks. Equal-height columns required table hacks. Now these are trivial. Flexbox solved the layout problems that had plagued CSS for over a decade.
Without display:flex
Item 1
Item 2
Item 3

block children stack vertically

With display:flex
Item 1
Item 2
Item 3

children become a row instantly

CSS — Enabling Flexbox
/* 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
.container { display: flex; } ← flex properties go here
Item 1
flex item
Item 2
Item 3
Item 4

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
main axis →
1
2
3
4
flex-direction: row (default)
Main axis → horizontal (left to right)
Cross axis → vertical (top to bottom)
justify-content aligns horizontally
align-items aligns vertically
flex-direction: column
Main axis → vertical (top to bottom)
Cross axis → horizontal (left to right)
justify-content aligns vertically
align-items aligns horizontally
Key mental model
The axes rotate with flex-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:

row (default)
1
2
3

left → right

row-reverse
1
2
3

right → left (1 is at right)

column
Item 1
Item 2
Item 3

top → bottom

column-reverse
Item 1
Item 2
Item 3

bottom → top (1 is at bottom)

CSS — flex-direction
.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:

flex-wrap: nowrap (default)
Item 1
Item 2
Item 3
Item 4
Item 5

items overflow/shrink on one line

flex-wrap: wrap
Item 1
Item 2
Item 3
Item 4
Item 5

items wrap to next line ✓

CSS — flex-wrap
.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.

flex-start (default)
A
B
C
flex-end
A
B
C
center
A
B
C
space-between
A
B
C
space-around
A
B
C
space-evenly
A
B
C
CSS — justify-content
.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:

stretch (default)
A
B
tall
C
flex-start
A
B
tall
C
flex-end
A
B
tall
C
center
A
B
tall
C
baseline
A
B small
C
CSS — align-items
.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 */
}
display:flex + justify-content:center + align-items:center
Perfectly centred ✓

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:

Without gap (using margin)
Item 1
Item 2
Item 3

margin on items — edge items need special treatment

With gap: 8px
Item 1
Item 2
Item 3

gap — equal spacing, no edge space ✓

CSS — gap
.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 */
Free Resource
CSS3 Cheat Sheet — All Properties & Selectors
Complete reference covering all Flexbox container and item properties.
Download PDF

Try It Yourself

Experiment with all the Flexbox container properties. Try changing flex-direction, justify-content, and align-items values:

Live Editor
✎ Editor
👁 Preview
Featured Course — Enroll Now
Modern Web Development With AI
Master HTML, CSS, JavaScript, React and Node.js with real projects and placement support.
120+
Chapters
40 hrs
Content
10 Real
Projects
4.9 ⭐
Rating
HTML5CSS3JavaScriptReact.jsNode.jsTailwind CSSAI Tools

Quick Check — Chapter 25

Questions & Answers
The main axis is the primary direction in which flex items are laid out. It is determined by 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 axis

When you switch flex-direction from row to column, the axes rotate — so what was horizontal alignment (justify-content) becomes vertical alignment, and vice versa.
All three distribute free space between/around flex items, but they differ in how they treat the edges:

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.
Three properties on the container:

display: flex; — enables flexbox
justify-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.
Multiple Choice Questions
Question 1 of 5
Which CSS property aligns flex items along the main axis?
Question 2 of 5
What is the default value of flex-direction?
Question 3 of 5
Which justify-content value places the first item at the start edge and last item at the end edge with no space at the edges?
Question 4 of 5
What is the default value of align-items?
Question 5 of 5
Which property do you add to a flex container to allow items to wrap to the next line?
Chapter Summary
  • Flexbox is a one-dimensional layout model activated by display: flex on 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). Supports row-gap and column-gap separately.
~8 min read
Beginner level
Chapter 25 of 60

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel