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.

4 min readUpdated 2026-11-29
In This Lesson
  • 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:

ModeStays in Page Flow?Moves Relative To...Everyday UI Example
relativeYes (keeps its space)Its own original resting spotSubtle 2px icon nudge, or anchor box for an absolute child
absoluteNo (lifted out of flow)Nearest ancestor with position != static (or the whole page)"Sale" corner badge inside a card, dropdown menus
fixedNo (lifted out of flow)The browser viewport windowFloating "Help" chat button, full-screen backdrop
stickyYes (until scrolled!)Its scroll container / viewportTop 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:

css
/* 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;
}
Forgetting position: relative on the Parent Card

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 matching padding-top manually.
  • position: sticky; top: 0; gives you the best of both worlds: the header occupies normal layout space at first, then smoothly sticks to 0px from 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-index wins over lower z-index (z-index: 50 sits in front of z-index: 10).
  • z-index only works on positioned elements (relative, absolute, fixed, sticky) or Flex/Grid children—it does nothing on position: static.
  • Keep clean spacing between tiers (e.g., dropdowns 20, sticky header 40, modals 50) rather than chaotic numbers like z-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:

HTML / CSS / JSLive CSS Positioning — Sticky Header & Pinned Badge
Live Browser Preview

Quick Checkpoints

Check Your Understanding

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?

Check Your Understanding

Which position value lets a header take up normal page space initially, then remain pinned at the top of the screen when scrolling?