CSS Basics

CSS Position

CSS Position property controls how an element is positioned in the document, with values static, relative, absolute, fixed, and sticky determining the positioning context and behavior.

How does the CSS position property work?

position controls how an element is placed. static is the default normal flow. relative offsets the element from where it would normally be. absolute removes it from the flow and places it relative to its nearest positioned ancestor. fixed pins it to the viewport, and sticky scrolls normally until it reaches an offset, then sticks.

.parent { position: relative; }      /* becomes the reference box */
.badge {
  position: absolute;
  top: 0;
  right: 0;
}
.header { position: sticky; top: 0; }
.toast  { position: fixed; bottom: 1rem; right: 1rem; }

Position Property Demo

Code Editor

<div class="card">
  <span class="badge">NEW</span>
  <h3>Relative parent</h3>
  <p>The badge is absolutely positioned inside this card.</p>
</div>
<div class="spacer">Scroll the preview: the header below sticks.</div>
<header class="sticky">Sticky header</header>
<div class="spacer tall"></div>

Preview

Understanding CSS Positioning

CSS positioning controls exactly where elements appear on the page. From basic document flow to complex overlays and interactive components, positioning is crucial for creating modern, sophisticated layouts that respond to user interaction and viewport changes.

Position Values

  • static - Normal document flow
  • relative - Offset from normal position
  • absolute - Relative to positioned ancestor
  • fixed - Relative to viewport
  • sticky - Conditional positioning

Offset Properties

  • top - Distance from top edge
  • right - Distance from right edge
  • bottom - Distance from bottom edge
  • left - Distance from left edge
  • z-index - Stacking order

Position Types Explained

Static & Relative

Static (Default)

Elements follow normal document flow. Offset properties have no effect.

Relative

Element maintains its space but can be offset. Creates positioning context.

Absolute & Fixed

Absolute

Removed from flow. Positioned relative to nearest positioned ancestor.

Fixed

Removed from flow. Always positioned relative to viewport.

Sticky Positioning

Sticky positioning is a hybrid of relative and fixed. Elements behave as relatively positioned until they cross a specified threshold, then become fixed.

Common Uses

  • • Sticky navigation headers
  • • Table headers that stick on scroll
  • • Sidebar content
  • • Call-to-action buttons

Requirements

  • • Must specify threshold (top, bottom, etc.)
  • • Parent must have height
  • • Good browser support (IE11+ with prefix)

Z-Index & Stacking Context

Understanding Stacking

Z-Index Rules

  • • Only works on positioned elements
  • • Higher values appear on top
  • • Auto value = 0
  • • Negative values go behind parent

Stacking Context

  • • Created by various CSS properties
  • • Elements stack within their context
  • • Child can't escape parent's context
  • • Understanding prevents z-index wars

Best Practices & Common Patterns

Best Practices

  • • Use transform for animations, not position
  • • Establish z-index scales for consistency
  • • Prefer modern layout methods when possible
  • • Test sticky positioning across browsers
  • • Consider accessibility for fixed elements
  • • Use contain property for performance

Common Pitfalls

  • • Using absolute positioning for everything
  • • Not understanding stacking contexts
  • • Ignoring mobile viewport behavior
  • • Breaking accessibility with fixed elements
  • • Animating left/top instead of transform
  • • Forgetting to test sticky fallbacks

Performance & Accessibility

Performance Tips

  • • Use will-change for elements that will move
  • • Avoid animating position properties
  • • Use transform3d() for GPU acceleration
  • • Consider contain: layout for positioned elements

Accessibility

  • • Fixed elements shouldn't block content
  • • Provide focus management for modals
  • • Test with screen readers
  • • Ensure keyboard navigation works

CSS Position — step by step

1 / 4
1

static, relative, absolute

static is the default (normal flow). relative offsets from its natural position. absolute removes from flow and positions relative to the nearest non-static ancestor.

1 of 4
⚠️z-index Does Not Work on static Elements
Applying z-index to an element with position: static has no effect. You must set position to relative, absolute, fixed, or sticky first for z-index to take effect.

🎯 Quick Check

Which position value scrolls with the page until a threshold, then sticks?

CSS Position: summary

Key points

  • top, right, bottom, left and z-index have no effect on position: static elements.
  • An absolutely positioned element is placed relative to the nearest ancestor whose position isn't static. That's why the parent usually gets position: relative.
  • absolute and fixed elements are taken out of the normal flow, so the surrounding content behaves as if they aren't there.
  • fixed positions relative to the viewport, except when an ancestor has a transform, filter or perspective, which becomes the containing block instead.
  • sticky needs an offset such as top: 0, and it only sticks within its parent's box.

Common mistakes

  • Forgetting position: relative on the parent, so an absolute child jumps to the page corner.
  • Using position: sticky inside a parent with overflow: hidden or auto, which stops it sticking.
  • Setting z-index on a static element and wondering why nothing changes.

Frequently asked questions

›What is the difference between position absolute and relative?

relative keeps the element in the normal flow and shifts it visually by top/left, leaving its original space empty. absolute removes the element from the flow and places it against its nearest positioned ancestor.

›Why is position: sticky not working?

The usual causes are: no top (or other offset) set, an ancestor with overflow: hidden, auto or scroll, or a parent no taller than the sticky element, so there's no room to stick.

›What is the difference between fixed and sticky?

fixed always stays at the same spot in the viewport and leaves the document flow. sticky stays in the flow and scrolls with the page until it reaches its offset, then sticks while its parent is still visible.

›What is the default value of position?

static. The element follows the normal document flow, and offset properties are ignored.