Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Positioning and Stacking

The position property

Reference for the CSS position property: static, relative, absolute, fixed and sticky explained with live examples, containing blocks and common mistakes.

The position property chooses how an element is placed in the page. By default elements are in the normal flow (static). The other values let you offset an element with top, right, bottom, left (or inset), pin it to a parent, or fix it to the screen.

CSS
.badge {
  position: absolute;
  top: 0;
  right: 0;
}

Try it

relative, absolute and sticky

The blue box is nudged from where it would normally sit. The red one is placed in the bottom-right corner of the gray stage (its nearest positioned ancestor). The green header sticks to the top of the scroll area.

Syntax

CSS
position: static | relative | absolute | fixed | sticky
  • Initial value: static
  • Applies to: all elements
  • Inherited: no
  • Animatable: no (discrete)
  • Computed value: as specified

Values

Value Description
static Normal flow. The offset properties and z-index have no effect.
relative Stays in the flow, taking up its normal space, but is shifted visually by the offsets. It becomes the containing block for absolute descendants.
absolute Removed from the flow and placed against its nearest positioned ancestor (or the initial containing block). Siblings close the gap.
fixed Removed from the flow and placed against the viewport, so it stays on the screen while the page scrolls.
sticky Behaves like relative until the scroll position reaches its offset threshold, then sticks like fixed inside its scrolling parent.

Usage notes

  • The containing block. An absolutely positioned element is placed relative to the nearest ancestor with a position other than static. Add position: relative (with no offsets) to the parent to anchor children to it.
  • Transforms change fixed. An ancestor with transform, filter or contain: paint becomes the containing block for fixed and absolute descendants.
  • Sticky needs an offset. position: sticky does nothing without top, bottom, left or right, and it stops working if an ancestor has overflow set to something other than visible and does not scroll the way you expect.
  • Stacking. A positioned element with a z-index other than auto creates a stacking context; fixed and sticky always do.
  • Layout alternatives. Do not build whole layouts with absolute. Flexbox and grid adapt to content; positioned elements do not push neighbors.
  • Accessibility. Covered or off-screen fixed headers can hide focused elements; use scroll-padding-top so anchor links and focus are not hidden under them.

Browser support

All five values are supported in every current browser; sticky is supported in all browsers released in recent years.