Part 3: Flexbox

Introduction

Before Flexbox, building a horizontal navigation bar or vertically centring content required floats, negative margins, or table hacks. Flexbox changed that. It is the layout system I reach for most often — any time I need to align or distribute items along a single axis, Flexbox is the right tool.

This part covers every Flexbox property I use, the mental model behind it, and the patterns that show up repeatedly in real layouts.


The Flexbox Mental Model

Flexbox operates on two elements: a flex container and its direct flex items.

<div class="container">   <!-- flex container -->
  <div class="item">A</div>
  <div class="item">B</div>
  <div class="item">C</div>
</div>
.container {
  display: flex;  /* activates flexbox */
}

Flexbox introduces two axes:

  • Main axis — the primary direction items are laid out (default: horizontal, left to right)

  • Cross axis — perpendicular to the main axis (default: vertical)

All Flexbox properties interact with these two axes.


Container Properties

flex-direction

Sets the main axis direction.

When flex-direction: column, the main axis becomes vertical and the cross axis becomes horizontal.

flex-wrap

By default, flex items try to fit on one line and shrink if needed. flex-wrap controls this:

flex-flow Shorthand

justify-content

Aligns items along the main axis.

space-between is what I use for navigation bars where items should spread evenly. center is what I use when centring a single element or group.

align-items

Aligns items along the cross axis.

align-items: center combined with justify-content: center is the shortest path to perfectly centring an element:

align-content

When items wrap onto multiple lines (flex-wrap: wrap), align-content distributes those lines along the cross axis. It has no effect with a single line.

gap

Sets gaps between flex items without margin.

gap does not add space at the edges of the container — only between items. This is preferable to using margin on items because it does not add unwanted space at the start or end.


Item Properties

These properties are set on the flex items (children), not the container.

flex-grow

Controls how much an item grows to fill available space. The value is a proportion.

All items with flex-grow: 1 share available space equally.

flex-shrink

Controls how much an item shrinks when there is not enough space.

flex-basis

Sets the initial size of an item before grow/shrink is applied. Can be a length or a keyword.

flex Shorthand

flex: grow shrink basis — the shorthand I use in practice:

The most common value in real layouts is flex: 1 — makes an item expand to fill available space equally with its siblings.

align-self

Overrides align-items for an individual item.

order

Changes the visual order of items without changing the DOM order.

Use order with caution — it creates a disconnect between DOM order (used by screen readers and keyboard navigation) and visual order.


Common Flexbox Patterns

Horizontal Navigation Bar

The margin-right: auto on the logo consumes all available space and pushes the rest of the links to the right — a common and clean technique.

Card Row That Wraps

Cards grow to fill the row but wrap when they would shrink below 280px.

Vertically Centred Full-Page Layout

The min-width: 0 on flex items is important — flex items have an implicit min-width: auto which can cause overflow when the content (like long code blocks or tables) is wider than available space.

Centring Anything


min-width: 0 — The Classic Flex Gotcha

A flex item's minimum width defaults to auto, meaning it cannot shrink below the size of its content. This causes overflow with long text, code blocks, or tables inside flex items.

This is the fix I apply whenever a flex item with text content overflows unexpectedly.


Debugging Flexbox

Browser developer tools (Chrome, Firefox, Edge) have dedicated Flexbox inspectors. In Chrome DevTools:

  • Select a flex container

  • The Layout panel shows the flex configuration visually

  • The overlay in the Elements panel highlights flex lines and gaps

Turning off flex-wrap temporarily can reveal whether items are shrinking more than expected.


Summary

Property
Applies to
Purpose

display: flex

Container

Activates flexbox

flex-direction

Container

Sets main axis direction

flex-wrap

Container

Whether items wrap

justify-content

Container

Main axis alignment

align-items

Container

Cross axis alignment (single line)

align-content

Container

Cross axis alignment (multiple lines)

gap

Container

Space between items

flex-grow

Item

How much item grows

flex-shrink

Item

How much item shrinks

flex-basis

Item

Initial size before grow/shrink

flex

Item

Shorthand for grow/shrink/basis

align-self

Item

Cross axis override for one item

order

Item

Visual reordering

min-width: 0

Item

Fix for shrinking/overflow in flex items


Up Next

Part 4 covers CSS Grid — the two-dimensional layout system for complex page structures.

Last updated