CSS ยท Lesson 69

CSS Flexbox ยท Flexible Box Layout Complete Guide

10 phases  ยท  Build: Stage 1 ยท Navigation Bar

๐Ÿ‘‹ Welcome to Lesson 69

Have you ever tried to centre something perfectly on a webpage and ended up fighting with margin: auto, float, and endless guessing? Or built a row of cards that looked fine on a big screen but collapsed into a mess on a phone? For years, web developers struggled with these exact problems using old layout tools that were never really designed for the job.

CSS Flexbox (short for Flexible Box Layout) was built specifically to solve those problems. It gives you a powerful, intuitive system for arranging elements in rows or columns ยท aligning them, distributing space between them, and making them adapt to different screen sizes ยท all with just a handful of well-named CSS properties.

In this lesson you will learn:

  • What Flexbox is, why it exists, and how it thinks about layout
  • How to turn any element into a flex container
  • How to control direction, wrapping, and the main axis with container properties
  • How to align items along both axes (justify-content, align-items, align-content)
  • How to control individual flex items ยท their size, order, growth, and shrinkage
  • How to build fully responsive layouts using Flexbox and media queries
  • How to apply everything in a real mini-project

No prior Flexbox knowledge is required. A basic understanding of HTML and CSS (selectors, properties, values) is enough to follow along.


๐Ÿ“š 10 phases๐Ÿ—๏ธ Stage 1 ยท Navigation Bar๐ŸŒ GitHub Pages
Phase 1 of 10
Lesson Introduction

Have you ever tried to centre something perfectly on a webpage and ended up fighting with margin: auto, float, and endless guessing? Or built a row of cards that looked fine on a big screen but collapsed into a mess on a phone? For years, web developers struggled with these exact problems using old layout tools that were never really designed for the job.

CSS Flexbox (short for Flexible Box Layout) was built specifically to solve those problems. It gives you a powerful, intuitive system for arranging elements in rows or columns ยท aligning them, distributing space between them, and making them adapt to different screen sizes ยท all with just a handful of well-named CSS properties.

In this lesson you will learn:

  • What Flexbox is, why it exists, and how it thinks about layout
  • How to turn any element into a flex container
  • How to control direction, wrapping, and the main axis with container properties
  • How to align items along both axes (justify-content, align-items, align-content)
  • How to control individual flex items ยท their size, order, growth, and shrinkage
  • How to build fully responsive layouts using Flexbox and media queries
  • How to apply everything in a real mini-project

No prior Flexbox knowledge is required. A basic understanding of HTML and CSS (selectors, properties, values) is enough to follow along.


โœ๏ธ Your Task
Practise what you just learned about Lesson Introduction. Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 2 of 10
Prerequisite Concepts

What is "layout" in CSS?

Layout means deciding how elements are positioned and sized on the page ยท how they sit next to each other, how much space they take, and where they appear when the screen is resized.

The Problem with Old Layout Methods

Before Flexbox, layout was done with:

  • float ยท originally for wrapping text around images, awkwardly repurposed for columns
  • position: absolute/relative ยท powerful but fragile; elements fall out of the normal flow
  • display: inline-block ยท creates strange gaps between elements

These tools were unpredictable, required hacks, and did not handle varying content sizes or different screen sizes gracefully.

What Flexbox Offers

Flexbox introduces a parent-child relationship for layout:

  • The parent element is called the flex container. You set rules on it that control how its children are arranged.
  • The children inside it are called flex items. They automatically respond to the container's rules, and you can also give them individual rules.

Think of a flex container like a tray holding items. The tray decides whether items line up left-to-right or top-to-bottom, how much space sits between them, and whether they wrap onto new lines. Each item on the tray can also have its own rules about how big it should be.


โœ๏ธ Your Task
Practise what you just learned about Prerequisite Concepts. Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 3 of 10
Part 1 ยท What is Flexbox and How Do You Activate It?

Activating Flexbox

You turn any HTML element into a flex container by writing one line of CSS:

css
display: flex;

That is it. Once you do this, all direct children of that element automatically become flex items and are arranged according to flexbox rules.

Before Flexbox (block layout):

html
<div class="container">
  <div class="box">Box 1</div>
  <div class="box">Box 2</div>
  <div class="box">Box 3</div>
</div>
css
.container {
  /* No display: flex โ€” default block layout */
}
.box {
  background-color: #4CAF50;
  color: white;
  padding: 20px;
  margin: 5px;
}

Expected result without Flexbox: Each .box is a block element and stacks vertically ยท one box per line.


After adding Flexbox:

css
.container {
  display: flex;   /* โ† This one line changes everything */
}
.box {
  background-color: #4CAF50;
  color: white;
  padding: 20px;
  margin: 5px;
}

Expected result WITH Flexbox: All three boxes appear in a row side by side, left to right.

๐Ÿค” Think About This: You only changed the container, not the boxes themselves. Yet the boxes changed how they look. This is the parent-child power of Flexbox ยท the container decides how its children behave.


The Two Axes of Flexbox

Flexbox works along two axes. Understanding these axes is the key to understanding every property that follows.

code
MAIN AXIS (default: horizontal โ†’ left to right)
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ–บ

  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  โ”‚  Item 1 โ”‚  โ”‚  Item 2 โ”‚  โ”‚  Item 3 โ”‚      โ–ฒ
  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜      โ”‚
                                              CROSS AXIS
                                          (default: vertical โ†“)
  • The main axis runs in the direction items are placed (default: left to right)
  • The cross axis runs perpendicular to the main axis (default: top to bottom)

When you change the direction, the axes rotate. This is critical ยท keep it in mind throughout the lesson.


display: inline-flex

There is also display: inline-flex. The difference:

  • display: flex ยท the container itself becomes a block-level element (takes full row width)
  • display: inline-flex ยท the container becomes inline (sits alongside other inline content)
css
/* The container takes full width */
.container { display: flex; }

/* The container is only as wide as its content */
.container { display: inline-flex; }

Most of the time you will use display: flex.


โœ๏ธ Your Task
Practise what you just learned about What is Flexbox and How Do You Activate It?. Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 4 of 10
Part 2 ยท Flex Container Properties

These are properties you set on the parent/container to control how all its children are arranged.


flex-direction ยท Which Way Do Items Go?

flex-direction controls the direction of the main axis ยท the direction items are placed.

Syntax:

css
.container {
  flex-direction: row | row-reverse | column | column-reverse;
}
ValueWhat it does
rowItems placed left to right (default)
row-reverseItems placed right to left
columnItems placed top to bottom
column-reverseItems placed bottom to top

Example 1 ยท flex-direction: row (default):

css
.container {
  display: flex;
  flex-direction: row;
}
code
[ Item 1 ] [ Item 2 ] [ Item 3 ]
โ†’ left to right

Example 2 ยท flex-direction: column:

css
.container {
  display: flex;
  flex-direction: column;
}
code
[ Item 1 ]
[ Item 2 ]   โ†“ top to bottom
[ Item 3 ]

๐Ÿ’ก Important: When you switch to column, the main axis is now vertical and the cross axis is now horizontal. This affects how justify-content and align-items behave ยท something beginners often find confusing!


Example 3 ยท flex-direction: row-reverse:

css
.container {
  display: flex;
  flex-direction: row-reverse;
}
code
[ Item 3 ] [ Item 2 ] [ Item 1 ]
โ† right to left

flex-wrap ยท What Happens When There Is No Room?

By default, flex items all try to fit on one line. If there are too many items to fit, they shrink rather than wrap. flex-wrap changes this behaviour.

Syntax:

css
.container {
  flex-wrap: nowrap | wrap | wrap-reverse;
}
ValueWhat it does
nowrapAll items on one line, shrink to fit (default)
wrapItems wrap onto new lines as needed
wrap-reverseItems wrap in reverse ยท new lines appear above

Example ยท flex-wrap: nowrap (default):

css
.container {
  display: flex;
  flex-wrap: nowrap;
  width: 300px;   /* too narrow for 6 items */
}
code
[I1][I2][I3][I4][I5][I6]   โ† all squashed on one line

Example ยท flex-wrap: wrap:

css
.container {
  display: flex;
  flex-wrap: wrap;
  width: 300px;
}
code
[Item 1][Item 2][Item 3]
[Item 4][Item 5][Item 6]   โ† overflow wraps to next line

๐Ÿ’ก Real-world use: flex-wrap: wrap is essential for responsive card grids. When the screen gets narrow, cards naturally wrap to new rows instead of becoming tiny.


Example ยท flex-wrap: wrap-reverse:

css
.container {
  display: flex;
  flex-wrap: wrap-reverse;
}
code
[Item 4][Item 5][Item 6]   โ† last row appears at TOP
[Item 1][Item 2][Item 3]

flex-flow ยท Shorthand for direction + wrap

flex-flow combines flex-direction and flex-wrap in one line:

css
/* Long form */
.container {
  flex-direction: row;
  flex-wrap: wrap;
}

/* Shorthand โ€” exactly equivalent */
.container {
  flex-flow: row wrap;
}

More examples:

css
flex-flow: column nowrap;      /* column direction, no wrapping */
flex-flow: row-reverse wrap;   /* right-to-left with wrapping */

โœ๏ธ Your Task
Practise what you just learned about Flex Container Properties. Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 5 of 10
Part 3 ยท Justifying Content Along the Main Axis

justify-content ยท Space Distribution on the Main Axis

justify-content controls how flex items are positioned and spaced along the main axis (the direction they are flowing in).

Syntax:

css
.container {
  justify-content: flex-start | flex-end | center | space-between | space-around | space-evenly;
}

To understand each value, imagine 3 boxes inside a wide container. The container has leftover space on the right.


justify-content: flex-start (default):

code
[Box1][Box2][Box3]                    (empty space)
โ† items packed to the START of the main axis
css
.container {
  display: flex;
  justify-content: flex-start;
}

justify-content: flex-end:

code
                    (empty space)[Box1][Box2][Box3]
                                 items packed to the END โ†’
css
.container {
  display: flex;
  justify-content: flex-end;
}

justify-content: center:

code
         (space)[Box1][Box2][Box3](space)
                  items centred
css
.container {
  display: flex;
  justify-content: center;
}

๐Ÿ’ก This is how you centre things horizontally in a row container with one line of CSS!


justify-content: space-between:

code
[Box1]         [Box2]         [Box3]
  no gap at edges; equal gap BETWEEN items
css
.container {
  display: flex;
  justify-content: space-between;
}

This is extremely common for navigation bars ยท logo on the left, links on the right.


justify-content: space-around:

code
  (half)[Box1](full)(Box2)(full)[Box3](half)
  equal space on EACH SIDE of every item
  (edge gaps are half the size of middle gaps)
css
.container {
  display: flex;
  justify-content: space-around;
}

justify-content: space-evenly:

code
  (gap)[Box1](gap)[Box2](gap)[Box3](gap)
  perfectly EQUAL gaps everywhere including edges
css
.container {
  display: flex;
  justify-content: space-evenly;
}

๐Ÿค” Think About This: What is the difference between space-around and space-evenly? In space-around, the edge gaps are half the size of middle gaps. In space-evenly, ALL gaps are identical.


Remembering justify-content

A simple memory trick: "justify" = along the main axis. Just like text justification in a word processor adjusts text along the horizontal line, justify-content adjusts items along the flex direction.


โœ๏ธ Your Task
Practise what you just learned about Justifying Content Along the Main Axis. Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 6 of 10
Part 4 ยท Aligning Items Along the Cross Axis

align-items ยท Alignment on the Cross Axis (Single Line)

align-items controls how flex items are positioned along the cross axis (perpendicular to the direction they flow). For a row container, this means vertical alignment.

Syntax:

css
.container {
  align-items: stretch | flex-start | flex-end | center | baseline;
}

Imagine 3 boxes in a row inside a tall container. The cross axis runs vertically.


align-items: stretch (default):

code
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ [Box1] [Box2] [Box3]           โ”‚
โ”‚                                โ”‚
โ”‚  items stretch to fill the     โ”‚
โ”‚  full height of the container  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
css
.container {
  display: flex;
  align-items: stretch;
  height: 200px;
}

Expected result: All items grow to the container's full height, even if their content is short.


align-items: flex-start:

code
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ [Box1] [Box2] [Box3]           โ”‚  โ† items at TOP (cross-axis start)
โ”‚                                โ”‚
โ”‚                                โ”‚
โ”‚                                โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

align-items: flex-end:

code
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                                โ”‚
โ”‚                                โ”‚
โ”‚                                โ”‚
โ”‚ [Box1] [Box2] [Box3]           โ”‚  โ† items at BOTTOM (cross-axis end)
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

align-items: center:

code
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                                โ”‚
โ”‚ [Box1] [Box2] [Box3]           โ”‚  โ† items CENTRED vertically
โ”‚                                โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
css
.container {
  display: flex;
  align-items: center;
  height: 200px;
}

๐Ÿ’ก The famous centring trick: Combine justify-content: center + align-items: center to perfectly centre one or more items both horizontally AND vertically inside their container ยท something that was notoriously difficult before Flexbox!

css
.container {
  display: flex;
  justify-content: center;   /* centre along main axis (horizontal) */
  align-items: center;       /* centre along cross axis (vertical) */
  height: 300px;
}

align-items: baseline:

Items are aligned so their text baselines line up ยท useful when items have different font sizes and you want the text to sit on the same invisible line.

css
.container {
  display: flex;
  align-items: baseline;
}
code
[small text] [BIG TEXT] [medium]
      โ†‘           โ†‘        โ†‘
      All text baselines aligned on same horizontal line

align-content ยท Alignment When There Are Multiple Lines

align-content only takes effect when items wrap onto multiple lines (i.e., when flex-wrap: wrap is set and there actually IS wrapping). It controls how the lines themselves are distributed along the cross axis.

Think of it as justify-content but for the lines/rows rather than for individual items.

Syntax:

css
.container {
  align-content: flex-start | flex-end | center | space-between | space-around | space-evenly | stretch;
}
ValueWhat it does
flex-startLines packed at the start of the cross axis
flex-endLines packed at the end
centerLines centred on the cross axis
space-betweenEqual space between lines, no space at edges
space-aroundEqual space around each line
space-evenlyEqual space between all lines and edges
stretchLines stretch to fill available space (default)

Example:

css
.container {
  display: flex;
  flex-wrap: wrap;
  align-content: space-between;
  height: 400px;
}
code
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ [Item1][Item2][Item3]          โ”‚  โ† Row 1 at top
โ”‚                                โ”‚
โ”‚                                โ”‚  โ† space between rows
โ”‚                                โ”‚
โ”‚ [Item4][Item5][Item6]          โ”‚  โ† Row 2 at bottom
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

โš ๏ธ Common confusion: align-items vs align-content

  • align-items ยท aligns items within a single line (works even without wrapping)
  • align-content ยท aligns the lines themselves (only works when there are multiple lines)

Quick Reference: Container Properties Summary

PropertyControlsAxis
flex-directionDirection items flowSets main axis
flex-wrapWhether items wrap to new linesCross axis
flex-flowShorthand: direction + wrapBoth
justify-contentSpace/position along main axisMain
align-itemsPosition along cross axis (single line)Cross
align-contentPosition of multiple linesCross

โœ๏ธ Your Task
Practise what you just learned about Aligning Items Along the Cross Axis. Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 7 of 10
Part 5 ยท Flex Item Properties

So far we have only set properties on the container. Now we look at properties you set on individual items to control their own size and behaviour.


order ยท Change the Visual Order of Items

By default, flex items appear in the same order as they appear in the HTML. order lets you change the visual order without changing the HTML.

Syntax:

css
.item {
  order: integer;   /* default is 0 */
}

Items are sorted from lowest to highest order value. Items with the same order appear in HTML order.

Example:

html
<div class="container">
  <div class="item" id="a">A</div>   <!-- HTML order: 1st -->
  <div class="item" id="b">B</div>   <!-- HTML order: 2nd -->
  <div class="item" id="c">C</div>   <!-- HTML order: 3rd -->
</div>
css
.container { display: flex; }

#a { order: 3; }   /* A displayed last */
#b { order: 1; }   /* B displayed first */
#c { order: 2; }   /* C displayed second */

Expected result (visual display): B โ†’ C โ†’ A

Even though in HTML the order is A, B, C ยท visually they appear as B, C, A.

๐Ÿ’ก Real-world use: You can reorder elements for mobile vs desktop using media queries + order, without moving HTML around. For example, show a sidebar before the main content on desktop, but after it on mobile.


flex-grow ยท How Much Should an Item Grow?

flex-grow controls how much extra space an item takes up relative to other items, when the container has leftover space.

Syntax:

css
.item {
  flex-grow: number;   /* default is 0 โ€” do not grow */
}

The value is a ratio (not a percentage or pixel amount). The browser calculates how much free space exists, then divides it among items proportionally to their flex-grow values.

Example 1 ยท No growing (default):

css
.item { flex-grow: 0; }   /* items stay their natural size */
code
[Item1][Item2][Item3]                  (leftover space unused)

Example 2 ยท All items grow equally:

css
.item { flex-grow: 1; }   /* each item gets 1 share of free space */
code
[โ”€โ”€โ”€โ”€ Item1 โ”€โ”€โ”€โ”€][โ”€โ”€โ”€โ”€ Item2 โ”€โ”€โ”€โ”€][โ”€โ”€โ”€โ”€ Item3 โ”€โ”€โ”€โ”€]
All three items grow equally to fill the container

Example 3 ยท One item grows more:

css
.item-a { flex-grow: 1; }
.item-b { flex-grow: 2; }   /* gets TWICE as much free space */
.item-c { flex-grow: 1; }

If there is 60px of free space, item-a gets 15px, item-b gets 30px, item-c gets 15px (ratios 1:2:1).

code
[โ”€ Item A โ”€][โ”€โ”€โ”€โ”€ Item B โ”€โ”€โ”€โ”€][โ”€ Item C โ”€]

๐Ÿค” Think About This: If every item has flex-grow: 1, do they all end up the same size? Not necessarily ยท they start at their natural size and then share the FREE space. If they started at different sizes they will end at different sizes but each absorbed the same amount of extra space.


flex-shrink ยท How Much Should an Item Shrink?

flex-shrink is the opposite of flex-grow. It controls how much an item shrinks relative to others when the container is too small to fit everything at natural size.

Syntax:

css
.item {
  flex-shrink: number;   /* default is 1 โ€” shrink proportionally */
}

Example:

css
.item-a { flex-shrink: 1; }   /* shrinks normally */
.item-b { flex-shrink: 3; }   /* shrinks 3x as much as item-a */
.item-c { flex-shrink: 1; }

When the container runs out of space, item-b gives up three times more width than item-a or item-c.

To prevent shrinking:

css
.item { flex-shrink: 0; }   /* item will never shrink below its natural size */

๐Ÿ’ก Real-world use: A navigation logo should never shrink while links can. Set flex-shrink: 0 on the logo item.


flex-basis ยท The Starting Size of an Item

flex-basis sets the initial/base size of a flex item before any growing or shrinking happens. Think of it as setting the "starting point" size.

Syntax:

css
.item {
  flex-basis: auto | length | percentage;
}
ValueMeaning
autoUse the item's content size (default)
200pxStart at exactly 200px
30%Start at 30% of the container's size
0Start with no assumed size (grow from scratch)

Example:

css
.item-a { flex-basis: 200px; }   /* starts at 200px, then grows/shrinks from there */
.item-b { flex-basis: 100px; }   /* starts at 100px */
.item-c { flex-basis: auto; }    /* starts at natural content size */

flex ยท The Powerful Shorthand

flex is a shorthand that combines flex-grow, flex-shrink, and flex-basis in one declaration:

css
.item {
  flex: flex-grow  flex-shrink  flex-basis;
}

Common patterns:

css
flex: 1;            /* grow: 1, shrink: 1, basis: 0  โ†’ items share space equally */
flex: 1 1 auto;     /* same as above but starts from natural size */
flex: 0 0 200px;    /* fixed 200px โ€” never grows, never shrinks */
flex: 2 1 150px;    /* starts at 150px, grows at 2x rate, shrinks at 1x rate */
flex: none;         /* fully inflexible: 0 0 auto โ€” stays natural size */
flex: auto;         /* fully flexible: 1 1 auto */

๐Ÿ’ก Best practice: Use the shorthand flex rather than the three separate properties. It also sets sensible defaults for the properties you don't specify.

Equal-width columns with flex: 1:

css
.container {
  display: flex;
}

.column {
  flex: 1;   /* every column shares space equally */
}
code
[โ”€โ”€ Column 1 โ”€โ”€][โ”€โ”€ Column 2 โ”€โ”€][โ”€โ”€ Column 3 โ”€โ”€]
All three columns are exactly equal width

Sidebar + main content layout:

css
.sidebar {
  flex: 0 0 250px;   /* fixed 250px sidebar, never grows or shrinks */
}

.main-content {
  flex: 1;           /* takes all remaining space */
}
code
[โ”€โ”€ Sidebar 250px โ”€โ”€][โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Main Content โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€]

align-self ยท Override Alignment for One Item

align-self lets a single item override the container's align-items setting for itself only.

Syntax:

css
.item {
  align-self: auto | flex-start | flex-end | center | baseline | stretch;
}

These values work exactly like align-items but apply to just one item.

Example:

css
.container {
  display: flex;
  align-items: flex-start;   /* all items start at the top */
  height: 200px;
}

.special-item {
  align-self: center;   /* THIS item centres itself vertically, overriding the container rule */
}
code
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ [Item1]  [Item2]       [Item3]          โ”‚
โ”‚           โ†‘ starts at top               โ”‚
โ”‚         [Special]                       โ”‚
โ”‚           โ†‘ centred (overrides rule)    โ”‚
โ”‚                                         โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

gap, row-gap, column-gap ยท Space Between Items

gap (previously called grid-gap) adds space between flex items without affecting the outer edges of the container. This is cleaner than using margin on every item.

Syntax:

css
.container {
  gap: 20px;              /* equal gap in both directions */
  row-gap: 10px;          /* gap between rows only */
  column-gap: 20px;       /* gap between columns only */
  gap: 10px 20px;         /* row-gap then column-gap */
}

Example:

css
.container {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
}
code
[Item1]  [Item2]  [Item3]
  16px     16px
[Item4]  [Item5]
  โ†‘ 16px between rows too

๐Ÿ’ก Much better than margins: With margin, the outer items have an unwanted margin on the container edge. gap only creates space BETWEEN items, not outside them.


Flex Item Properties Summary

PropertyControlsDefault
orderVisual order of this item0
flex-growHow much this item grows0
flex-shrinkHow much this item shrinks1
flex-basisStarting size before grow/shrinkauto
flexShorthand: grow + shrink + basis0 1 auto
align-selfThis item's cross-axis alignmentauto

โœ๏ธ Your Task
Practise what you just learned about Flex Item Properties. Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 8 of 10
Part 6 ยท Responsive Flexbox Layouts

What is Responsive Design?

Responsive design means your layout adapts gracefully to different screen sizes ยท desktop, tablet, and mobile ยท without building separate websites. Flexbox is perfectly suited for this because flex items can grow, shrink, and wrap automatically.

The two main tools for responsive Flexbox are:

  1. flex-wrap: wrap ยท items wrap naturally when the container is too narrow
  2. Media queries ยท override Flexbox properties at specific screen widths

Technique 1 ยท Natural Wrapping with flex-wrap

The simplest responsive pattern: set items to a minimum width and let them wrap.

css
.container {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
}

.card {
  flex: 1 1 300px;   /* grow: 1, shrink: 1, basis: 300px minimum */
}

What happens at different screen sizes:

  • Wide screen (1200px): Four 300px cards fit in one row โ†’ four columns
  • Medium screen (800px): Two cards per row (each about 380px after growing)
  • Narrow screen (400px): One card per row (fills full width)

No media queries needed at all ยท the wrapping is completely automatic! This is the most elegant responsive pattern.

๐Ÿค” Think About This: What does flex: 1 1 300px mean exactly? It says: "Start at 300px. Grow to fill extra space. Shrink if needed. But never let wrapping happen if 300px fits."


Technique 2 ยท Responsive Navigation Bar

A navigation bar that is horizontal on desktop but stacks vertically on mobile:

css
/* Base styles (mobile first โ€” stack vertically) */
.nav {
  display: flex;
  flex-direction: column;
  background-color: #333;
}

.nav a {
  color: white;
  padding: 12px 20px;
  text-decoration: none;
}

/* Desktop (wider than 600px) โ€” switch to horizontal row */
@media (min-width: 600px) {
  .nav {
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
  }
}

Expected result:

  • Mobile: Links stack vertically in a column
  • Desktop (600px+): Links sit side by side in a row, spread across the nav bar

Technique 3 ยท Responsive Two-Column Layout

A sidebar + main content layout that collapses to a single column on mobile:

css
/* Base: single column for mobile */
.page-layout {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* Desktop: two-column sidebar + content */
@media (min-width: 768px) {
  .page-layout {
    flex-direction: row;
  }

  .sidebar {
    flex: 0 0 240px;   /* fixed 240px sidebar */
  }

  .main-content {
    flex: 1;           /* takes remaining space */
  }
}

Expected result:

  • Mobile: Sidebar and main content stack vertically
  • Desktop (768px+): Sidebar fixed at 240px on the left, main content fills the right

Technique 4 ยท Responsive Card Grid

A classic card grid that adjusts columns automatically:

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

.card {
  background: white;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);

  /* 3 cards per row on desktop, wraps on smaller screens */
  flex: 1 1 calc(33% - 24px);
  min-width: 200px;   /* never smaller than 200px */
}

Expected result:

  • Wide screens: 3 cards per row
  • Medium screens: 2 cards per row (when 3 don't fit at 200px minimum)
  • Narrow screens: 1 card per row

Technique 5 ยท Mobile-First with Media Queries

The professional approach: start with mobile styles, then add desktop styles in media queries.

css
/* ===== MOBILE FIRST ===== */
.features {
  display: flex;
  flex-direction: column;   /* stack on mobile */
  gap: 16px;
}

.feature-item {
  flex: 1;                  /* full width on mobile */
  padding: 20px;
  background: #f5f5f5;
  border-radius: 8px;
}

/* ===== TABLET (600px+) ===== */
@media (min-width: 600px) {
  .features {
    flex-direction: row;    /* side by side on tablet */
    flex-wrap: wrap;
  }

  .feature-item {
    flex: 1 1 calc(50% - 8px);   /* 2 per row */
  }
}

/* ===== DESKTOP (900px+) ===== */
@media (min-width: 900px) {
  .feature-item {
    flex: 1 1 calc(33% - 16px);  /* 3 per row */
  }
}

Expected result:

  • Mobile: Items in a column (one per row)
  • Tablet: Two items per row
  • Desktop: Three items per row

order for Responsive Reordering

You can also use order to change the visual sequence between mobile and desktop ยท without touching HTML.

css
/* Mobile: natural HTML order โ€” content first, sidebar second */
.sidebar    { order: 2; }
.main-content { order: 1; }

/* Desktop: sidebar goes before content visually */
@media (min-width: 768px) {
  .sidebar    { order: 1; }   /* sidebar appears first on desktop */
  .main-content { order: 2; }
}

โœ๏ธ Your Task
Practise what you just learned about Responsive Flexbox Layouts. Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 9 of 10
Part 7 ยท Guided Practice Exercises
๐ŸŽฏ Your Challenge

Exercise 1 ยท Your First Flex Container (Beginner)

Objective: Activate Flexbox and control direction.

Scenario: You have a row of navigation links that should display horizontally, centred, with even spacing.

โœ๏ธ Task
- Cards start at 280px wide - Cards grow to fill available space - Cards shrink if needed - Cards wrap when they can't fit at 280px Expected Behaviour: - Wide screen โ†’ 3 or more cards per row - Medium screen โ†’ 2 per row - Narrow screen โ†’ 1 per row, full width Answer: flex: 1 1 280px; ยท
html
<!DOCTYPE html>
<html>
<head>
  <style>
    nav {
      background-color: #2c3e50;
      padding: 15px;
      /* TODO: Make this a flex container */
      /* TODO: Centre items horizontally */
      /* TODO: Add 30px gap between items */
    }

    nav a {
      color: white;
      text-decoration: none;
      font-size: 18px;
      padding: 8px 16px;
    }
  </style>
</head>
<body>
  <nav>
    <a href="#">Home</a>
    <a href="#">About</a>
    <a href="#">Projects</a>
    <a href="#">Contact</a>
  </nav>
</body>
</html>

Steps:

  1. Add display: flex to nav
  2. Add justify-content: center to centre the links
  3. Add gap: 30px for spacing between links

Expected Result: Four white nav links sit in a centred horizontal row on a dark background with 30px gaps between them.

Self-Check: Can you change it to spread the links evenly edge-to-edge using justify-content: space-between?


Exercise 2 ยท Perfect Centring (Beginner)

Objective: Centre content both vertically and horizontally.

Scenario: A hero section that should display its text perfectly centred in the middle of a 400px tall box.

css
.hero {
  height: 400px;
  background-color: #3498db;
  /* TODO: Use Flexbox to centre .hero-content both ways */
}

.hero-content {
  color: white;
  text-align: center;
}

Solution ยท fill in the blanks:

css
.hero {
  height: 400px;
  background-color: #3498db;
  display: _______;           /* activate Flexbox */
  justify-content: _______;   /* centre on main axis */
  align-items: _______;       /* centre on cross axis */
}

Expected Result: The .hero-content div appears perfectly centred inside the blue hero box.


Exercise 3 ยท Sidebar Layout (Intermediate)

Objective: Build a two-column page layout.

Task: Using Flexbox, create a layout with:

  • A sidebar that is always 220px wide and never shrinks or grows
  • A main content area that fills all remaining space
html
<div class="page">
  <aside class="sidebar">Sidebar</aside>
  <main class="content">Main Content</main>
</div>
css
.page {
  display: flex;
  min-height: 100vh;
  gap: 0;
}

/* Add your flex item rules below */
.sidebar {
  background: #ecf0f1;
  padding: 20px;
  /* TODO: Fixed 220px, never grows, never shrinks */
}

.content {
  background: #ffffff;
  padding: 20px;
  /* TODO: Takes all remaining space */
}

Hints:

  • Use flex: 0 0 220px on the sidebar
  • Use flex: 1 on the content

Exercise 4 ยท Responsive Card Grid (Intermediate)

Objective: Build a card grid that wraps responsively without any media queries.

css
.card-container {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
  padding: 20px;
}

.card {
  background: white;
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 24px;
  /* TODO: Set flex so cards start at 280px and grow to fill space */
}

Task: Write the flex shorthand for .card so that:

  • Cards start at 280px wide
  • Cards grow to fill available space
  • Cards shrink if needed
  • Cards wrap when they can't fit at 280px

Expected Behaviour:

  • Wide screen โ†’ 3 or more cards per row
  • Medium screen โ†’ 2 per row
  • Narrow screen โ†’ 1 per row, full width

Answer: flex: 1 1 280px;


Exercise 5 ยท Item Order and Alignment (Advanced)

Objective: Use order and align-self.

Scenario: A three-card row where:

  • The second card should appear first
  • The first card should appear last
  • The third card should be aligned to the bottom of the row (while others stretch to full height)
css
.container {
  display: flex;
  align-items: stretch;
  height: 250px;
  gap: 16px;
}

.card-1 { order: ___; }   /* appears last */
.card-2 { order: ___; }   /* appears first */
.card-3 {
  order: ___;
  align-self: ___;        /* pinned to bottom */
}

Answer:

css
.card-1 { order: 3; }
.card-2 { order: 1; }
.card-3 { order: 2; align-self: flex-end; }

Phase 10 of 10
Part 9 ยท Common Beginner Mistakes

Mistake 1: Setting Flex Properties on the Wrong Element

css
/* โŒ WRONG โ€” setting container properties on an item */
.item {
  justify-content: center;   /* this does nothing on an item */
}

/* โœ… CORRECT โ€” justify-content goes on the container */
.container {
  display: flex;
  justify-content: center;
}

Why: justify-content, align-items, flex-direction, and flex-wrap are container properties. order, flex-grow, flex-shrink, flex-basis, flex, and align-self are item properties.


Mistake 2: Forgetting display: flex

css
/* โŒ WRONG โ€” nothing works without activating Flexbox */
.container {
  justify-content: center;   /* has zero effect */
}

/* โœ… CORRECT */
.container {
  display: flex;
  justify-content: center;
}

Mistake 3: Confusing align-items with justify-content

Many beginners mix up which property controls which axis. The mnemonic:

  • justify-content โ†’ Just like text Justification โ†’ works along the main axis (same direction as the text/flow)
  • align-items โ†’ Across โ†’ works across on the cross axis (perpendicular)
css
/* For a row container (flex-direction: row): */
justify-content: center;   /* centres LEFT-RIGHT */
align-items: center;       /* centres UP-DOWN */

/* For a column container (flex-direction: column): */
justify-content: center;   /* centres UP-DOWN (main axis is now vertical) */
align-items: center;       /* centres LEFT-RIGHT (cross axis is now horizontal) */

Mistake 4: Using align-content When You Mean align-items

css
/* โŒ WRONG โ€” align-content has no effect on a single-line flex container */
.container {
  display: flex;
  /* NO flex-wrap, so only one line */
  align-content: center;   /* does nothing! */
}

/* โœ… CORRECT for single-line containers */
.container {
  display: flex;
  align-items: center;   /* this is the right property for single line */
}

/* align-content only works WITH flex-wrap: wrap AND actual wrapping */
.container {
  display: flex;
  flex-wrap: wrap;
  align-content: center;   /* โœ… now it works โ€” there are multiple lines */
}

Mistake 5: Expecting flex-grow to Set a Percentage

css
/* โŒ COMMON MISUNDERSTANDING */
.item { flex-grow: 50; }   /* this does NOT mean 50% width */

/* flex-grow is a RATIO โ€” items share free space in proportion */
.item-a { flex-grow: 1; }
.item-b { flex-grow: 1; }   /* item-a and item-b each get 50% of FREE space */

If you want exactly 50% width, use flex: 0 0 50% or flex-basis: 50%.


Mistake 6: Adding flex-wrap Without Enough Min-Width

css
/* โŒ PROBLEM โ€” items wrap immediately if they have no natural or set minimum size */
.container {
  display: flex;
  flex-wrap: wrap;
}

.item { }   /* no width/basis set โ€” items might wrap at 1px wide */

/* โœ… CORRECT โ€” give items a minimum size to control when they wrap */
.item {
  flex: 1 1 200px;   /* wraps only when 200px won't fit */
}

Mistake 7: Assuming Flex Children Are Still Block Elements

css
/* โŒ MISUNDERSTANDING */
/* Flex items do NOT stack vertically by default, they go horizontal */
/* And they do NOT respect margin: auto the same way block elements do */
/* BUT margin: auto DOES work for centering one item in Flexbox! */

.container {
  display: flex;
}

.push-right {
  margin-left: auto;   /* โœ… This DOES work โ€” pushes item to the far right! */
}

โœ๏ธ Your Task
Practise what you just learned about Common Beginner Mistakes. Open your editor, type the examples above by hand, modify them, and observe what changes.
๐Ÿ—๏ธ Build It โ€” Mini Project
Stage 1 ยท Navigation Bar

Build a complete responsive portfolio page for a fictional web developer called "Jordan Lee". The page uses Flexbox for every layout decision.

Stage 1 ยท Navigation Bar

starter.html
<nav class="navbar">
  <div class="logo">Jordan Lee</div>
  <ul class="nav-links">
    <li><a href="#">Home</a></li>
    <li><a href="#">Work</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Contact</a></li>
  </ul>
</nav>

Lesson 69 complete! ๐ŸŽ‰

You covered: