Interactive CSSFlexboxTutorial

Hands-on demos, sliders, and presets for every flex property—learn, experiment, and master layouts with live feedback.

Sections

Main Axis & Cross Axis

Every flex container has two axes. The main axis is set by flex-direction and the cross axis runs perpendicular.

Main Axis
Cross Axis
flex-direction:
main axis →
cross axis ↓
A
B
C
.container {
  display: flex;
  flex-direction: row;
}
💡When flex-direction: row, main axis is horizontal. When column, it flips vertical. -reverse variants flip start/end.

justify-content

Distributes items along the main axis.

justify-content:
1
2
3
4
.container {
  display: flex;
  justify-content: flex-start;
}

align-items

Aligns items along the cross axis. Items have different heights to show the effect.

align-items:
Short
Tall
Item
Mid
Tiny
.container {
  display: flex;
  align-items: stretch;
}
💡stretch is the default — items expand to fill the cross axis. baseline aligns by text baseline.

flex-wrap & align-content

flex-wrap lets items wrap to new lines. align-content controls spacing of wrapped lines.

flex-wrap:
align-content:
1
2
3
4
5
.container {
  display: flex;
  flex-wrap: nowrap;
  align-content: flex-start;
}

flex-grow

Controls how items grow to fill available space. A higher value = proportionally larger share.

Preset:
flex: 1
flex: 1
flex: 1
.a { flex: 1; }
.b { flex: 1; }
.c { flex: 1; }
💡flex: 1 is shorthand for flex-grow: 1; flex-shrink: 1; flex-basis: 0%.

flex-shrink

Controls how items shrink when the container is too small. Drag the slider to squeeze.

Container width: 600px
Preset:
A · shrink:1
B · shrink:1
C · shrink:1
/* Each item wants 200px, but container can be smaller */
.item { flex: 0 1 200px; }
💡flex-shrink: 0 = refuses to shrink. flex-shrink: 3 = shrinks 3× faster than flex-shrink: 1.

flex-basis vs width

flex-basis sets the ideal size along the main axis. width always sets horizontal size regardless of direction.

flex-basis: 150px
150px
auto
.item { flex-basis: 150px; }
/* respects flex-direction */
width: 150px
150px
auto
.item { width: 150px; }
/* always horizontal */
Switch to column to see the difference →
💡In column mode, flex-basis controls height (the main axis), while width still controls width. That's the key difference.

gap

Adds space between items without affecting outer edges. Drag to adjust.

gap:12px
A
B
C
D
E
.container { display: flex; gap: 12px; }

align-self

Override the container's align-items for a single item.

align-self on B:
A
B
C
.container { align-items: flex-start; }
.item-b { align-self: auto; }

order

Change visual order without touching HTML. Lower values come first.

Preset:
A0
B0
C0
D0
.a{order:0} .b{order:0} .c{order:0} .d{order:0}

The min-width: auto Problem

Flex items have implicit min-width: auto preventing shrink below content size. This causes overflow.

🐛 Bug — overflow!
This-very-long-text-overflows-the-container
Fixed
.item { flex: 1; }
/* min-width: auto prevents shrinking */
✅ Fix — min-width: 0
This-very-long-text-overflows-the-container
Fixed
.item { flex: 1; min-width: 0; }
/* now it can shrink + ellipsis works */
⚠️This is the #1 flexbox bug. When text overflows, add min-width: 0 or overflow: hidden. Same goes for min-height: 0 in column layouts.

Nested Flex Containers

A flex item can be a flex container itself. This is how complex layouts are composed.

Outer:
Inner:
Nav 1
Nav 2
Nav 3
Main Content
Widget A
Widget B
.outer { display: flex; flex-direction: row; }
.inner { display: flex; flex-direction: column; flex: 1; }

Add & Remove Items

See how flex reacts when items appear or disappear. The container redistributes space automatically.

3 items
justify:
wrap:
1
2
3

Common Layouts

Click any card to see the live demo + CSS. Patterns you'll use daily.

🧭 Navbar
⚜️ Holy Grail
🃏 Card Row
🎯 Perfect Center
📌 Sticky Footer
📐 Sidebar
🧭 Navbar

Flexbox vs CSS Grid

Both powerful — but for different jobs.

AspectFlexboxGrid
DimensionOne-dimensional (row or column)Two-dimensional (rows and columns)
ApproachContent-first — items dictate layoutLayout-first — grid defines structure
Best forNavbars, toolbars, card rows, centeringPage layouts, dashboards, galleries
Item sizingGrow/shrink dynamically from contentExplicit tracks (fr, px, auto)
AlignmentMain axis + cross axisRow axis + column axis + areas
OverlapNot naturallyEasy with grid-area + z-index
💡Common pattern: Grid for page structure, Flexbox inside each area for component layout. They complement each other.

Playground

Mix all properties together. This is where it clicks.

dir:
justify:
align:
wrap:
gap: 12px
Hello
Flex
Box
🚀
CSS

Cheatsheet

Quick reference for every flexbox property.

Container Properties

displayflex | inline-flex
flex-directionrow | row-reverse | column | column-reverse
flex-wrapnowrap | wrap | wrap-reverse
flex-flowshorthand for direction + wrap
justify-contentflex-start | center | flex-end | space-between | space-around | space-evenly
align-itemsstretch | flex-start | center | flex-end | baseline
align-contentflex-start | center | flex-end | space-between | space-around | stretch
gaprow-gap column-gap

Item Properties

orderinteger (default: 0)
flex-grownumber (default: 0)
flex-shrinknumber (default: 1)
flex-basislength | auto | content
flexshorthand: grow shrink basis
align-selfauto | flex-start | center | flex-end | stretch | baseline

Common Shorthand

flex: 1→ 1 1 0% (grow equally)
flex: auto→ 1 1 auto (grow from content)
flex: none→ 0 0 auto (rigid)
flex: 2→ 2 1 0% (2× share)
flex: 0 0 200px→ fixed 200px, no flex