Table of Contents

02. Flexbox — Alignment and Distribution

—Part of CSS Primer for the AI Era — Layout

Flexbox is the browser’s tool for alignment.
Where normal flow gives us vertical stacking and inline wrapping,
flexbox gives us a way to align, distribute, and order elements along a single axis:
horizontaly or vertically. → or ↓

It is a one‑dimensional layout system —
a quiet, intentional extension
of normal flow, but not a replacement
.

We reach for flexbox when we need:

Flexbox is at its best when used with restraint.


1. The Mental Model — One Axis at a Time

Flexbox is a one‑dimensional layout system.
It arranges items along one axis,
and lets us align them along the other.

There are only two axes:

We choose the direction (main axis) with:

flex-direction: row;      /* items flow left → right */
flex-direction: column;   /* items flow top → bottom */

Everything flexbox does happens within this simple structure.

If flex-direction: row
The main axis (direction) is horizontal:

[ item ][ item ][ item ]   ← main axis (row)
      ↑
      | cross axis (height of the row)
      |

Flexbox does not create multiple rows with shared alignment rules.
It aligns items within a single row.

If flex-direction: column
The main axis is vertical:

[ item ]
[ item ]
[ item ]   ← main axis (column)
← cross axis (width of the column) →

Flexbox does not create multiple columns with shared alignment rules.
It aligns items within a single column.


Why this matters

Flexbox always has two axes, but only one is structural.

Flexbox never creates a second structural dimension.


2. When to Use Flexbox

Flexbox is ideal for:

It is not ideal for:

Those belong to CSS Grid.


3. The Core Properties

Flexbox has only a few properties that matter in everyday use.
Everything else is refinement.

a) display: flexturning on the system

.container {
  display: flex;
}

This creates a flex container.
Its children become flex items.

b) flex-directionchoosing the axis

flex-direction: row;    /* default */
flex-direction: column;

c) justify-contentalignment along the main axis
Used for:

justify-content: flex-start;
justify-content: center;
justify-content: space-between;

d) align-itemsalignment along the cross axis
Used for:

align-items: center;

e) gapspacing between items

gap: 1rem;

This is the modern, clean way to create space.


4. A Quiet Example — Aligning a Logo and Title

.header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

This gives us:

[logo] [title]

No floats.
No positioning.
Just alignment.


5. Flexbox and Normal Flow

Flexbox does not replace normal flow — it modifies it.
Here's what we mean:

Flexbox simply gives us control over alignment,
not structure.

Use flexbox to align items,
not to build the page
.


6. Common Patterns

a) Centering a single element

.center {
  display: flex;
  justify-content: center;
  align-items: center;
}

b) Spacing items evenly

nav {
  display: flex;
  justify-content: space-between;
}

c) Aligning an icon with text

.button {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

d) Simple two‑column layout

.two-column {
  display: flex;
  gap: 2rem;
}

7. When Flexbox Causes Problems

Flexbox becomes fragile when used for:

These are signs we should switch to Grid or return to normal flow.


8. AI‑Steering Cues for Flexbox

These cues help AI choose flexbox only when appropriate:

These phrases keep AI grounded in the system’s intent.


9. A Quiet Transition to Grid

Flexbox gives us alignment along a single axis.
But some layouts need structure in two directions at once
— rows and columns.

That’s where Grid becomes the natural next step.


Tony de Araujo —New York | Lisbon