2. Modern CSS Layout & Design Systems
CSS Positioning, Sticky Headers & z-index
Break cleanly out of normal document flow when needed: master static, relative, absolute, fixed, and sticky positioning alongside z-index layering.
- Understand normal document flow vs positioned elements
- Pin badges inside cards accurately using relative + absolute positioning
- Build sticky navigation bars, fixed floating buttons, and clean z-index layers
Flexbox and CSS Grid arrange whole sections cleanly, yet real interfaces also need elements that overlap or stay pinned during scrolling: a "NEW" badge tucked into the top-right corner of a product card, a header bar that stays anchored at the top while you read, or a modal popup floating above everything else.
That is what the CSS position and z-index properties control.
Normal Document Flow (position: static)
By default, every HTML element uses position: static. Blocks stack peacefully one after another from top to bottom, and setting top, left, or z-index on a static element has zero effect.
Switching position to one of the four active modes unlocks coordinates (top, right, bottom, left) and z-index:
| Mode | Stays in Page Flow? | Moves Relative To... | Everyday UI Example |
|---|---|---|---|
relative | Yes (keeps its space) | Its own original resting spot | Subtle 2px icon nudge, or anchor box for an absolute child |
absolute | No (lifted out of flow) | Nearest ancestor with position != static (or the whole page) | "Sale" corner badge inside a card, dropdown menus |
fixed | No (lifted out of flow) | The browser viewport window | Floating "Help" chat button, full-screen backdrop |
sticky | Yes (until scrolled!) | Its scroll container / viewport | Top site header (top: 0), sticky table column headers |
The Golden Duo: relative Parent + absolute Child
Almost every corner badge, notification dot (3), or "Play Video" overlay icon uses this exact two-part pattern:
/* 1. Make the card an anchor box */
.course-card {
position: relative;
padding: 20px;
border: 1px solid #e2e8f0;
border-radius: 12px;
}
/* 2. Pin the badge strictly to the card's top-right corner */
.badge {
position: absolute;
top: 12px;
right: 12px;
}If you put position: absolute; top: 12px; right: 12px; on .badge without putting position: relative on .course-card, the badge flies right out of the card and pins itself to the top-right corner of the entire webpage!
sticky vs fixed Navigation Bars
position: fixed; top: 0;tears the header completely out of document flow and glues it over the top of the screen—so the first paragraph underneath hides behind the header unless you add matchingpadding-topmanually.position: sticky; top: 0;gives you the best of both worlds: the header occupies normal layout space at first, then smoothly sticks to0pxfrom the top edge as soon as the user scrolls past it!
(One classic gotcha: position: sticky stops sticking if an ancestor wrapper has overflow: hidden.)
Layer Depth with z-index
Think of a desk covered in sheets of paper: z-index controls which element sits on top along the front-to-back depth axis:
- Higher
z-indexwins over lowerz-index(z-index: 50sits in front ofz-index: 10). z-indexonly works on positioned elements (relative,absolute,fixed,sticky) or Flex/Grid children—it does nothing onposition: static.- Keep clean spacing between tiers (e.g., dropdowns
20, sticky header40, modals50) rather than chaotic numbers likez-index: 999999.
Try It Live: Corner Badge + Sticky Top Bar
Scroll inside the preview box below to watch .sticky-bar stay glued to the top while scrolling past .card, and notice how .badge stays anchored cleanly inside .card:
Quick Checkpoints
You add position: absolute; top: 8px; right: 8px; to a badge inside .card, yet the badge jumps to the top-right corner of the whole browser page. What is missing?
Which position value lets a header take up normal page space initially, then remain pinned at the top of the screen when scrolling?