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:
- a tooltip
- a badge on a card
- a dropdown menu
- a modal
- a floating button
- a notification bubble
- a highlight that sits on top of something
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.:
- static — the default; stays in normal flow
- relative — stays in flow but can be nudged
- absolute — removed from flow; positioned relative to an ancestor
- fixed — removed from flow; positioned relative to the viewport
- sticky — behaves like relative until it hits a threshold, then sticks
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:
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.
Negative values pull upward; positive values push downward.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:
- tiny nudges
- adjusting icons
- aligning decorative elements
- creating the anchor for absolutely positioned children
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:
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.
In other words:
“Start the tooltip immediately below the element that owns it.”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:
- tooltips
- dropdown menus
- badges
- overlays inside components
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:
- floating action buttons
- sticky chat widgets
- global notifications
- modals and overlays
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 headers
- sticky table columns
- section markers
- scroll‑based UI patterns
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:
position: fixed→ removes the element from normal flow and attaches it to the viewport*.z-index: 1000→ controls stacking order
— which element appears on top when elements overlap.- Higher numbers sit above lower numbers.
- z-index only works on positioned elements (relative, absolute, fixed, sticky).
- A modal needs a high value so it appears above the page, not behind headers, sidebars, or other positioned elements.
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:
- modals*
- dropdowns
- tooltips
- banners
- overlays
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:
- layout (never use it for layout)
- centering (use flexbox)
- multi‑column structure (use grid)
- spacing (use margin or gap)
- aligning siblings (use flexbox or normal flow)
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
