CSS position Property

Theory & Deep Explanation

The position property in CSS is fundamental for controlling layout, stacking, and advanced UI effects. It determines how an element is placed in the document, how it interacts with other elements, and how it responds to scrolling and resizing. Understanding each value is key to mastering modern web design.

static
relative
absolute
fixed
sticky

Real-World Use Cases

Accessibility & Best Practices

Code Examples

/* Overlay Example */
.overlay-demo {
  position: relative;
}
.overlay-demo .overlay {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(52,152,219,0.15);
  display: flex; align-items: center; justify-content: center;
  z-index: 2;
}

/* Tooltip Example */
.tooltip-demo {
  position: relative;
}
.tooltip-demo .tooltip {
  position: absolute;
  bottom: 120%; left: 50%;
  transform: translateX(-50%);
  background: #23272e; color: #fff;
  padding: 6px 14px; border-radius: 6px;
  font-size: 0.98rem;
  opacity: 0; pointer-events: none;
  transition: opacity 0.2s;
  z-index: 3;
}
.tooltip-demo:focus-within .tooltip,
.tooltip-demo:hover .tooltip {
  opacity: 1;
  pointer-events: auto;
}

/* Sticky Header Example */
.sticky-header {
  position: sticky;
  top: 0;
  background: #3498db;
  color: #fff;
  padding: 10px 0;
  text-align: center;
  font-weight: bold;
  z-index: 10;
}

Interactive Demos

static
Static (default)
Follows normal flow. No offset or stacking.
relative
Relative (moved)
Moved visually, but space is preserved.
absolute
Parent (relative)
Child (absolute)
The yellow child is position: absolute inside a position: relative parent. It is placed at top: 12px; right: 12px of the parent.
Parent (static)
Child (absolute)
The orange child is position: absolute inside a static parent. It is placed at top: 12px; left: 12px of the page (initial containing block).
fixed
Fixed (scroll me!)
Stays visible on scroll. Try the notification.
sticky
Sticky (scroll inside)

Scroll down to see the header stick to the top of this card. The sticky header will highlight when it becomes fixed.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque euismod, nisi eu consectetur.

Aliquam erat volutpat. Etiam nec velit nec nulla dictum dictum. Suspendisse potenti.

Vivamus euismod, urna eu tincidunt consectetur, nisi nisl aliquam nunc, eget aliquam nisl nunc eu nisl.

More content to enable scrolling...

Even more content to enable scrolling...

Scroll this card to see sticky in action. The header will highlight when sticky.
tooltip
Tooltip using position: absolute
Accessible tooltip appears on hover/focus.
z-index
z-index: 1
z-index: 2
z-index: 3
Stacking context demo. Hover to highlight.

Try scrolling, resizing, and interacting with the demos to see how position changes layout and behavior.

← Back to Home