Table of Contents

03. Grid — Structure and Composition

—Part of CSS Primer for the AI Era — Layout

Grid is the browser’s tool for structure.
Where flexbox arranges items along a single axis,
grid creates a two‑dimensional layout
— rows and columns working together.

Grid is not about alignment.
Grid is about composition
.

We reach for grid when we need:

multi‑column layouts

Grid is the layout system for shaping space.


1. The Mental Model — A Plane, Not a Line

While Flexbox thinks in one dimension:

→ → → → →   (a line)

Grid thinks in two dimensions:

→ → → → →   (columns)
↓ ↓ ↓ ↓ ↓   (rows)

Grid creates:

This makes grid a structural system —
a way to define the shape of a layout
before placing anything inside it.

Flexbox arranges items in a line.
Grid arranges items in a plane.


2. When to Use Grid

Grid is ideal for:

Grid is not ideal for:

Those belong to flexbox.


3. The Core Properties

Grid has a rich feature set,
but only a few properties matter for everyday use.


a) display: grid — turning on the system

.container {
  display: grid;
}

What this means:


b) Defining columns and rows

grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: auto auto;

What this means:


c) Using fr units — the flexible fraction

grid-template-columns: 2fr 1fr;

What this means:


d) gapspacing between rows and columns

gap: 1rem;

What this means:


e) Placing items

.item {
  grid-column: 1 / 3;
  grid-row: 2;
}

What this means:

The following diagram illustrates what that means:

line 1  ──────────────
Row 1
line 2  ──────────────   ← item starts here
Row 2   [   item   ]
line 3  ──────────────

This places the item precisely within the grid’s structure.


4. A Quiet Example — A Simple Three‑Column Layout

.layout {
  display: grid;
  grid-template-columns: 1fr 2fr 1fr;
  gap: 1.5rem;
}

This creates:

[ 1fr ][   2fr   ][ 1fr ]

A stable, predictable structure —
no hacks, no floats, no flexbox gymnastics.


5. Grid and Normal Flow

Grid does not replace normal flow —
it redefines the container’s structure
.

Inside each grid cell:

Grid shapes the outer structure.
— Normal flow shapes the content inside it.

This is why grid feels architectural.


6. Common Patterns

a) Equal‑width card grid

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 1.5rem;
}

What this means:


b) Two‑column layout with sidebar

.page {
  display: grid;
  grid-template-columns: 250px 1fr;
  gap: 2rem;
}

What this means:


c) Center column with margins

.centered {
  display: grid;
  grid-template-columns: 1fr min(70ch, 100%) 1fr;
}

What this means:


d) Named areas

grid-template-areas:
  "header header"
  "sidebar main"
  "footer footer";

What this means:

In this example, repeating a name means the area stretches across two columns.
Here's a visual model:

header | header
sidebar | main
footer | footer

The repetition feel intentional, not mysterious.


7. When Grid Causes Problems

Grid becomes fragile when used for:

These are signs we should switch to flexbox or normal flow.


8. AI‑Steering Cues for Grid

These cues help AI choose grid only when appropriate:

“Use grid when the layout has both rows and columns.”
“Use grid for structure, not alignment.”
“Use fr units for flexible, proportional columns.”
“Use auto-fill or auto-fit for responsive card grids.”
“Use named areas when the layout has a clear composition.”
“Switch to flexbox if the layout is one‑dimensional.”

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


9. A Quiet Transition to Positioning

Grid → gives us structure.
Flexbox → gives us alignment.
Normal flow → gives us the baseline.

But sometimes an element must step outside the flow entirely —
a tooltip, a badge, a floating button.

That’s where Positioning becomes the next natural tool.

Positioning is for exceptions —
the moments when an element
must break free of the layout
.

—- Tony de Araujo —New York | Lisbon