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 flowrelative- Offset from normal positionabsolute- Relative to positioned ancestorfixed- Relative to viewportsticky- Conditional positioning
Offset Properties
top- Distance from top edgeright- Distance from right edgebottom- Distance from bottom edgeleft- Distance from left edgez-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
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.
🎯 Quick Check
Which position value scrolls with the page until a threshold, then sticks?
CSS Position: summary
Key points
top,right,bottom,leftandz-indexhave no effect onposition: staticelements.- An absolutely positioned element is placed relative to the nearest ancestor whose
positionisn'tstatic. That's why the parent usually getsposition: relative. absoluteandfixedelements are taken out of the normal flow, so the surrounding content behaves as if they aren't there.fixedpositions relative to the viewport, except when an ancestor has atransform,filterorperspective, which becomes the containing block instead.stickyneeds an offset such astop: 0, and it only sticks within its parent's box.
Common mistakes
- Forgetting
position: relativeon the parent, so an absolute child jumps to the page corner. - Using
position: stickyinside a parent withoverflow: hiddenorauto, which stops it sticking. - Setting
z-indexon 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.