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.
.badge { position: absolute; top: 0; right: 0; }
Try it
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
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
positionother thanstatic. Addposition: relative(with no offsets) to the parent to anchor children to it. - Transforms change fixed. An ancestor with
transform,filterorcontain: paintbecomes the containing block forfixedandabsolutedescendants. - Sticky needs an offset.
position: stickydoes nothing withouttop,bottom,leftorright, and it stops working if an ancestor hasoverflowset to something other thanvisibleand does not scroll the way you expect. - Stacking. A positioned element with a
z-indexother thanautocreates a stacking context;fixedandstickyalways 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-topso 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.