CSS Positioning: Relative, Absolute, Fixed, and Sticky
Every box you lay out with normal flow has an implicit promise: it occupies a slot in the document, and everything around it accounts for its size. Positioning is how you opt out of that promise â partially or completely. The five values of the position property each break the contract in a different way, and almost every mysterious overlap, unreachable dropdown, or header that covers content traces back to picking the wrong one. This lesson explains what each value actually anchors to, how the browser decides what paints on top, and the recipes that come up in production: fixed headers, tooltips, dropdowns, modals, and sticky sidebars.
Five position values, five different anchors
The values are static, relative, absolute, fixed, and sticky. Static is the default: the element sits in flow, and offsets like top or left are ignored. Relative keeps the element in flow but lets you nudge it with offsets â and it silently becomes the reference point for absolutely positioned descendants. Absolute removes the element from flow entirely; it anchors to the nearest ancestor with a position other than static, or to the initial containing block if there is none. Fixed anchors to the viewport (more precisely, the page area, which excludes scrollbars). Sticky is a hybrid: the element stays in flow and scrolls normally until it hits a threshold, then behaves like fixed within its container.
| Value | Leaves normal flow | Offset relative to | Affects siblings |
|---|---|---|---|
| static | No | Ignored | Yes, occupies space |
| relative | No | Its own normal position | Yes, space is kept |
| absolute | Yes | Nearest positioned ancestor | No, space collapses |
| fixed | Yes | Viewport | No, space collapses |
| sticky | No, until threshold | Its flow position, then sticks | Yes, space is kept |
The last column is the one beginners miss. An absolutely positioned badge does not push its siblings apart, so the layout looks fine until the badge grows. A relatively positioned element keeps its slot, so the rest of the page does not move when you nudge it.
The relative-plus-absolute pair
Almost every overlay in a real interface is the same two-line recipe: put position: relative on the container, position: absolute on the overlay. The container does not move â it simply volunteers as the coordinate system. Tooltips, dropdown menus, notification dots, and image captions all follow this shape:
.dropdown {
position: relative;
}
.dropdown-menu {
position: absolute;
top: calc(100% + 8px);
left: 0;
z-index: 40;
min-width: 12rem;
background: #ffffff;
border: 1px solid #e2e8f0;
border-radius: 10px;
box-shadow: 0 12px 32px #0f172a26;
}
.dropdown-menu[hidden] {
display: none;
}
.tooltip {
position: relative;
display: inline-flex;
}
.tooltip-bubble {
position: absolute;
bottom: calc(100% + 8px);
left: 50%;
transform: translateX(-50%);
padding: 0.4rem 0.7rem;
border-radius: 6px;
background: #0f172a;
color: #f8fafc;
font-size: 0.8rem;
white-space: nowrap;
opacity: 0;
pointer-events: none;
transition: opacity 0.15s ease;
}
.tooltip:hover .tooltip-bubble,
.tooltip:focus-visible .tooltip-bubble {
opacity: 1;
}
If you forget the relative on the container, the bubble escapes to the nearest positioned ancestor â often the page root â and appears nowhere near its trigger. That single missing declaration is responsible for a huge share of overlay bugs.
Fixed headers and the content jump
A fixed header is straightforward: position: fixed, pinned to the top, full width, with a z-index above the page. The subtlety is that fixed elements leave the flow, so the first section of your page slides underneath them. The standard fix is to reserve space with padding or a margin on the content wrapper sized to the header height. With a sticky header you do not need the offset at all, because the header keeps its slot in flow until it sticks. Choose fixed when the bar must float over scrollable content (a video player toolbar, a cookie banner); choose sticky for page headers, because you avoid the offset math and search engines still see the header in its natural position.
.site-header {
position: sticky;
top: 0;
z-index: 50;
display: flex;
align-items: center;
justify-content: space-between;
gap: 1rem;
height: 64px;
padding-inline: 1.5rem;
background: #ffffffee;
backdrop-filter: blur(8px);
border-bottom: 1px solid #e2e8f0;
}
.table-scroll {
max-height: 26rem;
overflow-y: auto;
border: 1px solid #e2e8f0;
border-radius: 12px;
}
.table-scroll thead th {
position: sticky;
top: 0;
z-index: 1;
background: #f8fafc;
border-bottom: 1px solid #e2e8f0;
}
Sticky table headers inside a scrollable wrapper are the same trick applied to a local scrolling context: the threshold is the top of .table-scroll, not the viewport.
Sticky only works inside a scrolling ancestor
position: sticky needs two things: a threshold (top, bottom, left, or right) and a scrollport the element can stick within. Three classic failures share one root cause. First, no threshold â without top: 0 (or similar) the element never sticks; the declaration is effectively ignored. Second, the parent is too short â a sticky child cannot travel beyond its containing block, so if the parent is exactly as tall as the child, there is nowhere to stick. Third, an ancestor with overflow: hidden â that ancestor becomes the scrollport, and since it does not scroll, the child never sticks. That third one bites hardest because the overflow property may live five levels up in a layout wrapper you forgot about. When a sticky element refuses to work, walk up the DOM looking for overflow: hidden, auto, or scroll on ancestors.
In flex and grid containers there is one more wrinkle: a sticky item stretches to fill its grid area by default, and a stretched element has no room to slide. align-self: flex-start (or start in grid) shrinks the item back to its content height, which restores the sticky travel distance.
Stacking contexts: why z-index does not always win
z-index only orders items inside the same stacking context. Any positioned element with a z-index other than auto â plus elements with properties like transform, filter, opacity below 1, or will-change â creates a new stacking context and traps its descendants inside it. Raising a child's z-index cannot break it out of a parent's context; the parent competes as a whole against its siblings. That is why a modal with z-index: 9999 can still render under a header with z-index: 100: the header's context was created later in the tree or has a higher value at the shared level. The practical rule is to assign layer numbers at the top of each context â for example 10 for the header, 50 for dropdowns, 100 for the modal overlay, 110 for the modal panel â and never sprinkle arbitrary huge numbers on deeply nested children.
:root {
--z-header: 50;
--z-dropdown: 40;
--z-overlay: 100;
--z-modal: 110;
}
.site-header { z-index: var(--z-header); }
.dropdown-menu { z-index: var(--z-dropdown); }
.modal-overlay {
position: fixed;
inset: 0;
z-index: var(--z-overlay);
display: grid;
place-items: center;
background: #0f172a99;
}
.modal-panel {
z-index: var(--z-modal);
width: min(32rem, calc(100vw - 2rem));
background: #ffffff;
border-radius: 16px;
box-shadow: 0 24px 64px #0f172a40;
}
Notice the overlay and the panel share a context here because the panel is a child of the overlay; giving the panel its own z-index orders it against siblings inside that context, while the overlay's value orders the whole stack against the header.
Centering with absolute: still useful
Flexbox and grid made one-axis centering trivial, but the absolute-plus-transform recipe remains handy when you need a true overlay center â an icon on top of an image, a spinner over a panel â because the element is already removed from flow and needs no wrapper alignment:
.media-frame {
position: relative;
aspect-ratio: 16 / 9;
overflow: hidden;
border-radius: 12px;
}
.media-frame .overlay-content {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.media-frame .play-badge {
position: absolute;
top: 0.75rem;
right: 0.75rem;
}
Prefer inset: 0 over writing four longhand properties; it is one declaration and behaves identically. And when you only need to shift an element a little from where flow put it, relative beats absolute because the layout around it stays intact.
Common Mistakes
Absolutely positioned overlay appears at the top of the page
Fix: Add position: relative to the intended containing element so the overlay anchors there instead of the initial containing block
Sticky header or sidebar never sticks
Fix: Give it a threshold such as top: 0, and check that no ancestor sets overflow: hidden or a fixed height that leaves no scroll room
z-index: 9999 on a nested element still renders behind a modal
Fix: Raise the element's parent stacking context instead; z-index values only compare siblings inside the same context
Content hides under a fixed header after scrolling to an anchor
Fix: Add scroll-margin-top to the target sections equal to the header height, or switch the header to sticky
Best Practices
- Define a small layer scale with custom properties and reuse it, instead of inventing z-index values per component
- Use position: relative on containers and absolute only on the few overlay children that need it
- Prefer sticky page headers over fixed ones unless the bar must float above the content
- Reserve space for fixed elements with padding on the content wrapper so the first viewport does not jump
- Use inset: 0 instead of separate top, right, bottom, left declarations when filling a containing block
Interview Questions
Q1: What is the difference between position: fixed and position: sticky?
Fixed elements are removed from flow and always anchor to the viewport, so they stay in place during scroll. Sticky elements stay in flow and scroll normally until they cross a threshold such as top: 0, then stick within their containing block until that block scrolls away.
Q2: Why does an absolutely positioned element with a high z-index still appear behind another element?
z-index only orders items inside the same stacking context. If the element sits inside a parent that created a stacking context (positioned with z-index, or with transform, opacity, filter, and similar), the whole parent context is compared against siblings; the child cannot outrank elements outside its parent's context.
Q3: How do you center an element both horizontally and vertically with positioning?
Set position: absolute with top: 50% and left: 50%, then transform: translate(-50%, -50%). The transform shifts the element back by half its own size, so the center of the element lands on the center of the containing block. Flexbox or grid with place-items: center is the more modern alternative when the parent is already a layout container.
Key Takeaways
- static ignores offsets; relative offsets in place and becomes the anchor for absolute descendants; absolute and fixed leave the flow; sticky sticks within its container after a threshold
- The relative container plus absolute overlay pair builds tooltips, dropdowns, badges, and captions
- Sticky fails when a threshold is missing, the parent is too short, or an ancestor sets overflow: hidden
- z-index competes only within one stacking context, so layer management happens at the context level, not the leaf level
- Fixed headers need reserved space or scroll-margin; sticky headers do not