← Back to Course

Module -08

CSS Flexbox

The one-dimensional layout system that changed everything. Learn to align, distribute, and reorder elements with just a few properties.

Flexbox arrived in 2012 and became the default for component layouts. Every modern framework uses it. Browser DevTools now have dedicated Flexbox inspectors.

Before Flexbox: floats, clearfixes, table hacks. After Flexbox: clean, predictable layouts. Container queries are adding new flexibility, but Flexbox remains essential.

One axis at a time. Main axis for distribution, cross axis for alignment. These two concepts unlock every Flexbox layout pattern you'll ever need.

Part A

Core Concepts

01

The Flexbox Mental Model

Flexbox is about axes. Once you understand main axis vs cross axis, everything else falls into place.

Row Direction (Default)
Main Axis
→
↓
Cross
Column Direction
Main
↓
Cross
→
Main Axis & Cross Axis

Flexbox has TWO axes. The main axis is determined by flex-direction (row = horizontal, column = vertical). The cross axis is perpendicular. Every Flexbox property works on one of these axes.

Flex Container (parent)
Flex Item 1
Flex Item 2
Flex Item 3
Container Properties
display: flex;
flex-direction
justify-content
align-items
flex-wrap
gap
Item Properties
flex-grow
flex-shrink
flex-basis
align-self
order
Flex Container & Flex Items

Add display: flex to a parent element — it becomes the flex container. Its direct children automatically become flex items. Container properties control layout; item properties control individual behavior.

Before Flexbox (Float Hell)
.container {
  overflow: hidden; /* clearfix */
}
.item {
  float: left;
  width: 33.333%;
  box-sizing: border-box;
}
.item:nth-child(3n+1) {
  clear: left;
}
Complex, fragile, hard to center vertically
With Flexbox
.container {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
}

/* That's it! */
Clean, predictable, easy vertical centering
Life Before Flexbox

Before Flexbox, we used floats, clearfixes, and table-cell hacks. Vertical centering was notoriously difficult. Flexbox solved these problems elegantly.

02

justify-content & align-items

The two most important Flexbox properties. justify-content works on the main axis, align-items works on the cross axis.

justify-content: flex-start;
justify-content: center;
justify-content: space-between;
justify-content: space-around;
justify-content: space-evenly;
Distribute Along Main Axis

justify-content distributes items along the main axis. Think: "How should extra space be distributed?" space-between puts space between items. space-evenly puts equal space everywhere.

align-items: flex-start;
align-items: center;
align-items: flex-end;
align-items: stretch;
Align on Cross Axis

align-items positions items on the cross axis. stretch is the default — items fill available height. center is the most common override for vertical centering.

.container {
  display: flex;
  justify-content: center; /* main axis */
  align-items: center; /* cross axis */
}
Perfectly Centered
Before Flexbox, this required hacks. Now it's 3 lines.
The Holy Grail: Centering

Centering both horizontally and vertically was notoriously hard before Flexbox. Now: display: flex + justify-content: center + align-items: center. Done.

Navigation: space-between
Logo
HomeAboutContact
Card Footer: space-between + center
Posted 2 hours ago
Read More
Icon + Text: gap + center
Success! Your file was uploaded.
Layouts You'll Use Daily

These patterns appear everywhere: nav bars, card footers, alerts, buttons with icons. Master space-between, center, and gap — you'll use them daily.

03

flex-wrap & gap

Make your layouts responsive and well-spaced with these two essential properties.

flex-wrap: nowrap (default)
Items shrink or overflow
flex-wrap: wrap
Items wrap to next line
Wrapping to New Lines

By default, flex items try to fit on one line, shrinking if needed. flex-wrap: wrap allows items to flow to new lines when they don't fit — essential for responsive card grids.

Without gap (using margins - inconsistent)
With gap: 8px (clean, consistent)
row-gap: 16px; column-gap: 8px; (different axes)
Spacing Between Items

gap creates consistent spacing between flex items — no need for margin hacks. Use row-gap and column-gap for different spacing on each axis.

.card-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
}

.card {
  flex: 1 1 250px; /* grow, shrink, basis */
}
Cards grow to fill space, wrap when needed, minimum 250px each
Responsive Card Grid Pattern

The classic responsive card grid: flex-wrap: wrap allows wrapping, gap handles spacing, and flex: 1 1 250px means cards grow/shrink but never go below 250px.

04

flex-grow, flex-shrink, flex-basis

Control how flex items grow and shrink to fill available space.

All items: flex-grow: 0 (default) — don't grow
0
0
0
All items: flex-grow: 1 — grow equally
1
1
1
Middle item: flex-grow: 2 — grows twice as much
1
2
1
Growing to Fill Space

flex-grow controls how items expand to fill extra space. Value of 0 = don't grow. Value of 1 = grow equally. Value of 2 = grow twice as much as items with 1.

flex-shrink: 1 (default) — items shrink to fit
flex-shrink: 0 — item won't shrink (fixed width)
Fixed
Sidebar pattern: fixed sidebar + flexible content
Shrinking When Space is Tight

flex-shrink controls whether items can shrink below their basis. flex-shrink: 0 means "never shrink" — useful for fixed-width elements like logos or sidebars.

flex-basis: auto (default) — uses content/width
Short
Medium text
Longer content here
flex-basis: 0 — ignore content, distribute equally
Short
Medium text
Longer content here
flex-basis: 200px — start at 200px, then grow/shrink
Starting Size Before Grow/Shrink

flex-basis sets the initial size before growing or shrinking. auto = use width/content. 0 = ignore content size. A pixel value = start at that size.

/* flex: grow shrink basis */
flex: 0 1 auto; /* default */
flex: 1; /* same as: 1 1 0 */
flex: auto; /* same as: 1 1 auto */
flex: none; /* same as: 0 0 auto */
flex: 1
Grow equally, shrink equally, ignore content size
flex: none
Don't grow, don't shrink, use auto size
The flex Shorthand

The flex shorthand combines grow, shrink, and basis. flex: 1 is the most common — it means "grow and shrink equally, distributing space evenly."

05

flex-direction & order

Change the flow direction and reorder items without changing HTML.

flex-direction: row;
1
2
3
flex-direction: row-reverse;
1
2
3
flex-direction: column;
1
2
3
flex-direction: column-reverse;
1
2
3
Changing the Main Axis

flex-direction sets the main axis direction. row = horizontal, column = vertical. Add -reverse to flip the order. This changes which axis justify-content and align-items affect!

HTML order: 1, 2, 3, 4
1
2
3
4
Visual order with CSS: item 3 has order: -1
3 (order: -1)
1
2
4
Visual Reordering

The order property changes visual order without changing HTML. Default is 0. Lower numbers appear first. Useful for responsive layouts where you want different order on mobile vs desktop.

Mobile: Image first (order: -1)
Image
Text Content
Desktop: Text first (natural order)
Text Content
Image
Responsive Reordering

Common pattern: on mobile, images come first for visual impact. On desktop, text comes first for F-pattern reading. Use order with media queries to achieve this without duplicating HTML.

Part H

Self-Assessment

15

Self-Assessment

Test your understanding of CSS Flexbox. Click each question to reveal the answer.

Q1

What does display: flex do?

Click to reveal
Q2

Which property controls distribution along the main axis?

Click to reveal
Q3

How do you center an item both horizontally and vertically?

Click to reveal
Q4

What does flex: 1 mean?

Click to reveal
Q5

What happens when you set flex-direction: column?

Click to reveal
Q6

Which property creates consistent spacing between flex items?

Click to reveal

Key Takeaways

Think in two axes
Main →
justify-content
Cross ↓
align-items

Flexbox has a main axis (direction of flow) and cross axis (perpendicular). justify-content works on main, align-items works on cross. This is the core mental model.

Perfect centering in 3 lines
display: flex;
justify-content: center;
align-items: center;

The holy grail of CSS layout. Before Flexbox, centering was notoriously difficult. Now it's three properties.

Distribution with space-between

space-between pushes items to edges with equal space between. Perfect for nav bars, card footers, and any "logo left, menu right" pattern.

flex: 1 for equal distribution

flex: 1 on all items means they grow equally to fill space. Most common flex shorthand you'll use.

flex-wrap + gap for grids

flex-wrap: wrap + gap creates responsive grids. Items flow to new lines when they don't fit, with consistent spacing.

Sidebar pattern: fixed + flex: 1

Classic sidebar layout: sidebar has fixed width (flex: 0 0 250px), content has flex: 1 to fill remaining space. Works for any fixed + fluid combo.