Table of Contents

04. Positioning — Exceptions and Overlays

—Part of CSS Primer for the AI Era — Layout

Most of the time, elements live inside normal flow.
Flexbox and Grid extend that flow
by giving us alignment and structure.

But sometimes an element must break free:

These are exceptions —
moments when
the element cannot participate in the layout
because it must sit over or outside it.

This is what positioning is for.

Positioning is the escape hatch of CSS. Use it only when an element must leave the flow.


1. The Mental Model — Leaving the Flow

Normal flow arranges elements
top‑to‑bottom and left‑to‑right.
Positioning lets an element
step out of that system.

There are five positioning states,
but only four of them are actual positioning modes
since “static” is simply the default.:

Each mode changes how the element participates in layout.


2. ''position: relative'' — Stay in Flow, Move Slightly

.badge {
  position: relative;
  top: -4px;
  left: 8px;
}

What this means:

The space the element would have occupied remains in place —
only the visual box moves. This is why relative positioning is ideal for small, precise adjustments that do not disturb surrounding layout.
The element stays in normal flow, but we can shift it visually without affecting surrounding elements.

Use it for:

Relative positioning is gentle — it never breaks the layout.


3. position: absolute — Removed from Flow

.tooltip {
  position: absolute;
  top: 100%;
  left: 0;
}

What this means:

Together, these values place the tooltip directly below and left‑aligned with the element it belongs to — the classic dropdown/tooltip pattern.

The element is removed from normal flow and positioned relative to the nearest positioned ancestor (an ancestor with position: relative, absolute, or fixed).

Use it for:

Absolute positioning is precise but fragile if overused.


4. position: fixed — Attached to the Viewport

.fab {
  position: fixed;
  bottom: 2rem;
  right: 2rem;
}

What this means:

The element is removed from flow and positioned relative to the viewport.
It stays in place even when the page scrolls.

Use it for:

Fixed positioning is powerful — use it sparingly.


5. position: sticky — Hybrid Behavior

.header {
  position: sticky;
  top: 0;
}

What this means:

The element behaves like relative until the scroll reaches a threshold, then it “sticks” like fixed.

Use it for:

Sticky positioning is flow‑aware and less intrusive than fixed.


6. Stacking Context — Who Sits on Top

Whenever elements overlap,
CSS decides which one appears on top
using stacking context.

.modal {
  position: fixed;
  z-index: 1000;
}

What this means:

In this example, 1000 doesn’t have special meaning — it’s simply a high, safe value that ensures the modal sits at the top of the visual stack.

z-index only works on positioned elements.
Higher numbers sit above lower ones within the same stacking context.

Use stacking context for:

A clean mental model:

Positioning determines where.
Z‑index determines which one is on top.

* The modal is positioned relative to the viewport, meaning:
it stays in the same place even when the page scrolls
its coordinates (top, right, bottom, left)
are measured from the edges of the visible window

In web development, a modal (also called a modal window or dialog box)
is a child element that appears on top of the main webpage content.

It creates a temporary state that requires the user to interact with the modal before they can return to the rest of the application.


7. When Positioning Causes Problems

Positioning becomes fragile when used for:

If positioning feels like a workaround,
it probably is
.


8. AI‑Steering Cues for Positioning

These cues help AI use positioning only when appropriate:

“Use absolute positioning only when 
the element must leave the flow.”
“Use relative positioning to create 
the anchor for absolutely positioned children.”
“Use fixed positioning for elements that 
must stay visible during scroll.”
“Use sticky positioning for headers or markers 
that follow the scroll.”
“Avoid positioning for layout — 
use flexbox or grid instead.”

These phrases keep AI grounded in the intent of the system.


9. What's next?

With layout complete
— normal flow, flexbox, grid, and positioning —
we now move from structure to appearance.

Visual Style is where we shape color,
typography, spacing, and rhythm —
the layer that makes the structure feel alive.


Tony de Araujo —New York | Lisbon