Skip to main content

CSS Positioning Guide

Complete CSS positioning reference with interactive examples.
Master static, relative, absolute, fixed, sticky, and z-index.

Position Values

PropertyDescription
position: static;Normal document flow, ignores top/right/bottom/left
position: relative;Positioned relative to its normal position
position: absolute;Positioned relative to nearest positioned ancestor
position: fixed;Positioned relative to the viewport
position: sticky;Hybrid of relative and fixed based on scroll

position: static

Default positioning. Elements flow normally in the document. Top, right, bottom, left have no effect.

Box 1 (static)
Box 2 (static) - top: 20px has no effect
Box 3 (static)
position: static; /* default - ignores offsets */

position: relative

Positioned relative to its normal position. Original space is preserved.

Box 1 (static)
Box 2 (relative) - moved 20px
Original position preserved
Box 3 (static)
position: relative; top: 20px; left: 20px;

position: absolute

Positioned relative to the nearest positioned ancestor (not static). Removed from normal flow.

Parent (position: relative)
Absolute child
Static sibling
position: absolute; top: 10px; right: 10px;

position: fixed

Positioned relative to the viewport. Stays in place when scrolling.

Viewport (simulated)
Fixed element
position: fixed; top: 0; right: 0;

position: sticky

Behaves like relative until it reaches a threshold, then becomes fixed. Scroll the container below.

Content above...
More content...
Sticky Header (scroll me!)
Content below...
More content...
Keep scrolling...
Almost there...
End of content
position: sticky; top: 0;

Offset Properties (top, right, bottom, left)

PropertyDescription
top: 0;Distance from the top edge
right: 0;Distance from the right edge
bottom: 0;Distance from the bottom edge
left: 0;Distance from the left edge
inset: 0;Shorthand for top, right, bottom, left

z-index Stacking

Controls stacking order of positioned elements. Higher values appear on top.

z: 1
z: 2
z: 3

Common z-index Scale

CSSValueTypical Usage
z-index: -1;-1Behind content
z-index: 0;0Base level
z-index: 10;10Dropdowns
z-index: 20;20Sticky headers
z-index: 30;30Fixed elements
z-index: 40;40Modals
z-index: 50;50Tooltips
z-index: 9999;9999Top priority overlays

Understanding Stacking Contexts

A new stacking context is created when an element has:

  • position: absolute/relative with z-index other than auto
  • position: fixed or position: sticky
  • opacity less than 1
  • transform, filter, or backdrop-filter
  • isolation: isolate

Key insight: z-index only works within the same stacking context. A child with z-index: 9999 cannot appear above a sibling of its parent with z-index: 1.

Common Positioning Patterns

Centering with Absolute

Centered
.container {
  position: relative;
}
.centered {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

Sticky Header

Sticky Header
header {
  position: sticky;
  top: 0;
  z-index: 20;
  background: white;
}

Fixed Sidebar

Fixed
.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  height: 100vh;
  width: 250px;
}
.main-content {
  margin-left: 250px;
}

Modal Overlay

Modal
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 40;
}
.modal {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 50;
}

Tooltip Positioning

Tooltip text
.tooltip-container {
  position: relative;
}
.tooltip {
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%);
  margin-bottom: 8px;
}

Corner Badge

3
Card with badge
.card {
  position: relative;
}
.badge {
  position: absolute;
  top: -8px;
  right: -8px;
}

Quick Reference

When to use each position:

  • static - Default flow, most elements
  • relative - Minor offsets, absolute parent
  • absolute - Overlays, tooltips, badges
  • fixed - Headers, sidebars, modals
  • sticky - Scrolling headers, sidebars

Key principles:

  • Absolute needs a positioned parent
  • Fixed ignores all ancestors
  • Sticky needs a scroll container
  • z-index only works on positioned elements
  • Use inset: 0; for full coverage