Web Analytics

CSS Position Property

Intermediate ~12 min read

Position: Static (Default)

position: static is the default. Elements flow normally in the document. Top, right, bottom, left have no effect.

HTML
CSS
JS

Position: Relative

position: relative allows you to move an element from its normal position using top, right, bottom, left. The original space is preserved.

HTML
CSS
JS

Position: Absolute

position: absolute removes the element from normal flow and positions it relative to its nearest positioned ancestor (or the viewport if none exists).

HTML
CSS
JS

Position: Fixed

position: fixed positions the element relative to the viewport. It stays in place even when scrolling.

HTML
CSS
JS

Position: Sticky

position: sticky toggles between relative and fixed based on scroll position. Perfect for sticky headers!

HTML
CSS
JS

Z-Index (Stacking Order)

The z-index property controls which elements appear on top. Higher values = closer to viewer. Only works on positioned elements.

HTML
CSS
JS
Key Concept: For position: absolute to work relative to a parent, that parent must have position: relative (or absolute/fixed).

Summary

  • static - Default, normal flow
  • relative - Offset from normal position, space preserved
  • absolute - Removed from flow, positioned relative to parent
  • fixed - Positioned relative to viewport, stays on scroll
  • sticky - Relative until scroll threshold, then fixed
  • z-index - Controls stacking order (higher = on top)

Quick Quiz

Which position value keeps an element in place when scrolling?

A
relative
B
absolute
C
fixed
D
static