—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.
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.
.badge { position: relative; top: -4px; left: 8px; }
What this means:
position: relative → keeps the element in normal flow, but allows a visual shift.top: -4px → moves the element 4 pixels upward from where it would normally sit.left: 8px → moves the element 8 pixels to the right from its normal position.
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.
.tooltip { position: absolute; top: 100%; left: 0; }
What this means:
position: absolute → removes the element from normal flow and positions it relative to the nearest positioned ancestor (usually a parent with position: relative).top: 100% → places the top edge of the tooltip exactly one full height below its anchor element. left: 0 → Aligns the tooltip’s left edge with the left edge of its positioned ancestor.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.
.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.
.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.
Whenever elements overlap,
CSS decides which one appears on top
using stacking context.
.modal { position: fixed; z-index: 1000; }
What this means:
position: fixed → removes the element from normal flow and attaches it to the viewport*. z-index: 1000 → controls stacking order 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.
Positioning becomes fragile when used for:
If positioning feels like a workaround,
it probably is.
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.
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