CSS/layout
Positioning
The position property controls how an element is placed relative to its normal position, its nearest positioned ancestor, or the viewport — enabling overlays, sticky headers, an...
4 min readcss#css#positioning
Introduction
The position property controls how an element is placed relative to its normal position, its nearest positioned ancestor, or the viewport — enabling overlays, sticky headers, and precise placement, alongside z-index for stacking order.
Subtopics
position: static(default)position: relativeposition: absoluteposition: fixedposition: sticky- Offset properties:
top,right,bottom,left z-indexand stacking context
Syntax
css
.static-el { position: static; } /* default, offsets ignored */
.relative-el {
position: relative;
top: 10px;
left: 20px; /* shifts from its normal position, keeps its space */
}
.absolute-el {
position: absolute;
top: 0;
right: 0; /* positioned relative to nearest positioned ancestor */
}
.fixed-el {
position: fixed;
bottom: 20px;
right: 20px; /* stays fixed relative to the viewport, ignores scroll */
}
.sticky-el {
position: sticky;
top: 0; /* behaves relative until scroll threshold, then sticks */
}
.stack {
position: relative;
z-index: 10; /* higher value renders above lower siblings */
}
static— default flow position;top/left/etc. have no effect.relative— positioned relative to its own normal position; still occupies its original space, and becomes a positioning context for absolutely positioned children.absolute— removed from normal flow, positioned relative to the nearest ancestor withpositionother thanstatic(or the viewport if none exists).fixed— removed from normal flow, positioned relative to the viewport; stays in place during scrolling.sticky— hybrid: acts relative until the scroll position reaches a threshold (e.g.,top: 0), then acts fixed within its parent's bounds.z-index— controls stacking order among overlapping positioned elements; only works on elements with apositionvalue other thanstatic.
Important Properties
| Property | Purpose | Syntax | Example |
|---|---|---|---|
position | Positioning scheme | position: value; | position: absolute; |
top/right/bottom/left | Offset from reference edge | top: value; | top: 10px; |
z-index | Stacking order | z-index: value; | z-index: 100; |
Common Use Cases
- Beginner: a small "badge" positioned in the corner of a card using
position: absoluteinside aposition: relativeparent. - Practical UI: a sticky page header using
position: sticky; top: 0;. - Real-world: a modal overlay using
position: fixedcovering the full viewport, with az-indexhigh enough to appear above all other page content.
Common Errors
- ❌
.child { position: absolute; top: 0; }with no positioned parent → ✅ setposition: relative;on the parent so the child positions relative to it, not the whole page. - ❌
z-index: 999;on aposition: staticelement expecting it to stack above others → ✅z-indexonly works on positioned elements (relative,absolute,fixed,sticky). - ❌
position: sticky;without atop/bottom/etc. offset → ✅ sticky requires at least one offset (e.g.,top: 0;) to know when to "stick." - ❌ Two overlapping elements with the same
z-indexand unexpected stacking → ✅ assign distinctz-indexvalues, and be aware source order breaks ties. - ❌ Using
position: fixedinside a parent withtransformapplied, expecting it to stick to the viewport → ✅ atransformon an ancestor creates a new containing block, causingfixedchildren to position relative to that ancestor instead of the viewport — remove the transform or adjust the approach.
Common Mistakes
- Forgetting to set
position: relativeon the parent before usingabsoluteon a child. - Using
z-indexon non-positioned elements (has no effect). - Not providing an offset for
stickypositioning. - Overusing
position: absolutefor layout instead of Flexbox/Grid.
Quick Reference
css
.parent { position: relative; }
.badge { position: absolute; top: 0; right: 0; }
.header { position: sticky; top: 0; z-index: 50; }
.modal { position: fixed; inset: 0; z-index: 1000; }
LB
Author
Lakshya Bansal