Lab 02: Flexbox Deep Dive

Time: 30 minutes | Level: Practitioner | Docker: docker run -it --rm node:20-alpine sh

Overview

Master the full Flexbox model: flex shorthand, alignment axes, ordering, wrapping, and real-world layout patterns including Holy Grail, sticky footer, and card grids.


Step 1: The Flex Shorthand

flex: <grow> <shrink> <basis> — the three properties that control how flex items size themselves.

/* Common patterns */
flex: 1;         /* flex: 1 1 0%    — grow, shrink, start from 0 */
flex: auto;      /* flex: 1 1 auto  — grow, shrink, use content size */
flex: none;      /* flex: 0 0 auto  — fixed, no grow/shrink */
flex: 0;         /* flex: 0 1 0%    — don't grow, can shrink */

/* Full explicit form */
.item {
  flex-grow:   1;     /* proportion of available space to take */
  flex-shrink: 0;     /* don't shrink below flex-basis */
  flex-basis:  200px; /* ideal size before flex adjustment */
}

/* Common layouts */
.sidebar { flex: 0 0 280px; }  /* fixed width sidebar */
.main    { flex: 1; }          /* main takes remaining space */
.icon    { flex: 0 0 1.5rem; } /* icon won't flex */

💡 flex: 1 vs flex: auto: flex: 1 starts distributing from 0 (ignores content size), flex: auto starts from content size. Use flex: 1 for equal columns.


Step 2: Alignment — Two Axes

align-items vs align-content:

  • align-items — aligns items within their current row/line

  • align-content — aligns the lines themselves within the container (only when flex-wrap: wrap)


Step 3: Order & Flex-Wrap


Step 4: Auto Margin Tricks

Auto margins in flex consume all available space on one side:


Step 5: Holy Grail Layout

Classic three-column layout with header and footer:



Step 7: Responsive Card Grid


Step 8: Capstone — Validate with html-validate

📸 Verified Output:


Summary

Property
Values
Purpose

flex

<grow> <shrink> <basis>

Shorthand for flex sizing

flex: 1

1 1 0%

Equal-width columns

flex: none

0 0 auto

Fixed size item

justify-content

space-between etc.

Main axis alignment

align-items

center, stretch

Cross axis (single line)

align-content

space-between etc.

Cross axis (multi-line)

order

integer

Reorder without changing DOM

margin-inline-start: auto

Push item to end

flex-wrap: wrap

Enable multi-line

gap

<length>

Space between items

Last updated