CSS Position Cheatsheet: Interactive Visual Guide
October 18, 2026 | Cheatsheet | CSS
By Syed · Frontend Engineer & Creator of UITweak
Positioning is where most CSS confusion lives: absolute inside what, why sticky does nothing, where fixed actually anchors. Every section below is interactive, so you can see each value behave instead of memorizing definitions.
Interactive position playground
Five values, one mental model. static and relative keep the element in normal flow: the browser lays it out as an ordinary block, and relative then shifts only the painted position while the original space stays reserved. absolute and fixed pull the element out of flow entirely, so siblings close the gap, and the browser resolves top, left, right, and bottom against the element's containing block rather than its static position. sticky is the hybrid: it lays out in flow like relative, then behaves like fixed inside its parent once scrolling crosses the threshold you set.
stage (positioned)
.target {
position: relative;
top: 20px;
left: 20px;
}relative offsets the box from its normal spot, but its original space stays reserved.
The containing block rule
For an absolutely positioned element, the browser does not default to the viewport or the direct parent. It walks up the ancestor chain looking for the nearest element whose position is anything but static, and that element's padding box becomes the containing block that top: 0; left: 0 refers to. If no such ancestor exists, the containing block is the initial containing block, roughly the viewport. Three more properties quietly create containing blocks for absolutely positioned descendants: a non-none transform, filter, or perspective, plus will-change: transform. That is why a dropdown sometimes anchors to a grandparent card instead of the button you expected.
demo stage (positioned)
parent (relative)
I am the parent box.
An absolutely positioned element anchors to its nearest positioned ancestor: the closest parent with any position other than static. Flip the parent between static and relative and watch the child jump between the stage anchor and the parent anchor.
/* parent static: child anchors to the stage */
.parent { position: static; }
/* parent relative: child anchors to the parent */
.parent { position: relative; }
.child { position: absolute; top: 12px; left: 12px; }Sticky in a scroll box
Sticky positioning resolves against two boxes, not one. The top, right, bottom, and left values act as thresholds, but the element can only stick inside its containing block, usually its parent, and only relative to its nearest scrolling ancestor. As you scroll, the browser keeps the element in flow until the scroll position would push it past the threshold, then pins it there, and releases it when the parent's edge arrives. If any ancestor between the element and the viewport has overflow: hidden, that ancestor becomes a non-scrolling scroll container, leaving the sticky element no scroll distance to travel, so it never sticks.
Scroll this box. The header above behaves like a normal element until its top edge reaches the threshold.
Once it hits top: 0, it sticks there like a fixed element, but only inside this container.
When the container ends, the header scrolls away with it. That boundary is the part people forget.
Keep scrolling to feel where it releases. This is how sticky navbars and table headers work.
Sticky acts like relative until its edge hits the threshold you set (here top: 0), then it sticks like fixed. It only works while its parent is taller than it and the parent has room to scroll.
.sticky-header {
position: sticky;
top: 0; /* the threshold: required for sticky to work */
}Tailwind position utilities
Tailwind's utilities map one to one onto the CSS values: static, relative, absolute, fixed, sticky, plus inset-0, top-4, left-1/2, and the rest for offsets. There is no magic in the compiled output: absolute top-0 left-0 is exactly position: absolute; top: 0; left: 0. The value of the utilities is speed and consistency. The spacing scale keeps offsets on the design system's rhythm instead of scattered pixel values, and inset-0 combined with m-auto is the shortest correct way to center an absolutely positioned box of known size.
staticThe default. top, right, bottom, left do nothing.
relativeOffset from its normal spot. Keeps its space in flow.
absoluteAnchors to the nearest positioned ancestor.
fixedAnchors to the viewport. Ignores scrolling.
stickyRelative until the scroll threshold, then stuck.
inset-0Sets top, right, bottom, left to 0. Full-bleed inside a positioned parent.
top-41rem from the top edge of its anchor.
left-1/2Halfway across its anchor. Pair with -translate-x-1/2 to truly center.
z-10Stacking order. Only works on positioned elements.
Combine them freely: class="absolute top-4 right-4 z-10" is a badge pinned to the top-right corner. For values outside the scale, use arbitrary values like top-[73px].
<div class="relative">
<div class="absolute top-4 right-4 z-10">badge</div>
</div>
<div class="sticky top-0">navbar</div>
<div class="fixed bottom-6 right-6">back to top</div>Common recipes
Four patterns you will build constantly: a centered modal, a notification badge, a sticky navbar, and a fixed back-to-top button. Each one is live above, and the code for all four is in one block below.
1. Perfectly centered modal
Modal title
Centered with absolute plus translate.
2. Notification badge on an icon
3. Sticky navbar
Scroll: the navbar sticks to the top of this box.
On a real page the same two classes pin it to the viewport top while the page scrolls.
That is the whole sticky navbar pattern. Two classes, no JavaScript.
4. Fixed back-to-top button
page preview (the button below is a mock)
On a real page the button uses fixed bottom-6 right-6 so it stays glued to the viewport corner while you scroll.
<!-- 1. centered modal -->
<div class="relative h-screen">
<div class="absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2">
...
</div>
</div>
<!-- 2. notification badge -->
<div class="relative w-14 h-14">
<span class="absolute -top-1.5 -right-1.5">3</span>
</div>
<!-- 3. sticky navbar -->
<nav class="sticky top-0 z-40">...</nav>
<!-- 4. fixed back-to-top -->
<button class="fixed bottom-6 right-6 z-50" aria-label="Back to top">
...
</button>Related guides
Keep going with these guides that build directly on positioning:
Sticky vs Stuck States
Detect when a sticky element actually sticks with scroll-state queries.
Anchor Positioning Tooltips
Position tooltips against their anchor with the CSS anchor positioning API.
Modal z-index Fix
Escape stacking context traps with the top layer and the dialog element.
Common Production Bugs & Fixes
Bug 1: The absolutely positioned child anchors to the wrong ancestor
What goes wrong: a tooltip or dropdown appears far from its trigger, aligned to some distant card or the viewport edge. Why: the trigger's wrapper has the default position: static, so the browser kept walking up the tree until it found a positioned ancestor, or fell back to the initial containing block. Fix: put relative on the immediate wrapper you want to anchor to. relative with no offsets changes nothing visually; it exists purely to establish the containing block.
.trigger-wrap {
position: relative; /* containing block for the dropdown */
}
.dropdown {
position: absolute;
top: 100%;
left: 0;
}Bug 2: z-index has no effect
What goes wrong: you raise z-index to 9999 and the element still paints underneath its sibling. Why: z-index only applies to positioned elements (and flex or grid items); on a static element it is ignored entirely. The deeper trap is stacking contexts. Once a parent creates one, through positioned plus non-auto z-index, opacity below 1, or any transform or filter, its children's z-index values compete only inside that context and can never paint above a sibling context. Fix: first give the element position: relative (or absolute), then check whether an ancestor's stacking context is capping it.
.modal {
position: fixed; /* positioned, so z-index applies */
z-index: 50;
}Bug 3: position: sticky never sticks
What goes wrong: the header scrolls away like a normal block. Why: something between the sticky element and the viewport clips scrolling. The usual culprit is overflow: hidden (or overflow-x: hidden) on a wrapper, often added to clear floats or clip a background. That makes the wrapper the scroll container, and with no scrollable distance inside it, the sticky thresholds never trigger. A second cause: the sticky element is the only child, or it is taller than its parent, leaving no room to travel. Fix: remove the clipping overflow or move it to an inner element, and make sure the parent is taller than the sticky child.
/* overflow: hidden makes the wrapper a non-scrolling scroll container,
so sticky thresholds never trigger */
.page { overflow-x: hidden; }
/* overflow: clip clips without creating a scroll container,
so sticky keeps working against the viewport scroller */
.page { overflow-x: clip; }Bug 4: position: fixed sticks to a parent instead of the viewport
What goes wrong: a fixed modal or toast scrolls with the page, or positions itself relative to a card. Why: an ancestor has transform, filter, or perspective set to anything but none. Per spec, that ancestor becomes the containing block for all fixed-position descendants, so fixed degrades into something that behaves like absolute. This bites constantly with wrappers that carry transform: translateZ(0) for performance. Fix: move the fixed element out of the transformed subtree, usually by portaling it to document.body, or remove the transform if it serves no purpose.
/* any of these on an ancestor hijacks fixed descendants */
.animated-wrapper {
transform: translateZ(0); /* containing block for fixed children */
}Real-World Recipes
Recipe 1: Centered modal overlay
Steps: 1. A backdrop div with fixed and inset: 0 covers the viewport and dims it. 2. Make the backdrop a flex container with centered alignment, so the dialog centers on both axes without transform hacks. 3. Give the dialog position: relative and a z-index above page content, and stop click propagation on the dialog so only backdrop clicks close it.
.modal-backdrop {
position: fixed;
inset: 0;
background: rgba(15, 23, 42, 0.5);
display: flex;
align-items: center;
justify-content: center;
z-index: 50;
}
.modal-dialog {
position: relative;
background: #ffffff;
border-radius: 12px;
padding: 24px;
max-width: 480px;
width: calc(100% - 32px);
}Recipe 2: Notification badge on an avatar corner
Steps: 1. Wrap the avatar in a relative span so it becomes the containing block. 2. Place the badge absolute at top: 0; right: 0 and pull it slightly outward with a small translate so it straddles the corner. 3. Add a border in the page background color so the badge reads as separate from the avatar underneath.
.avatar-wrap {
position: relative;
display: inline-block;
}
.avatar-badge {
position: absolute;
top: 0;
right: 0;
transform: translate(25%, -25%);
min-width: 20px;
height: 20px;
border-radius: 9999px;
background: #ef4444;
color: #ffffff;
font-size: 11px;
border: 2px solid #ffffff;
}Recipe 3: Sticky navbar that stays on top
Steps: 1. Give the header position: sticky with top: 0 so it pins once scrolled to the viewport top. 2. Add a z-index above content layers and a solid background so scrolled content does not show through. 3. Keep every ancestor's overflow visible along the scroll path, or the stick never engages.
.site-header {
position: sticky;
top: 0;
z-index: 40;
background: #ffffff;
border-bottom: 1px solid #e2e8f0;
}Frequently Asked Questions
Why does my absolutely positioned element ignore top and left?
Offsets only apply to positioned elements. With the default position: static, top and left do nothing at all. Set relative, absolute, fixed, or sticky first; the offsets then resolve against the containing block.
Can z-index be negative, and when is that useful?
Yes. Within a stacking context, negative z-index children paint above the parent's background but below its content, so a negative value is useful for decorative layers that must sit behind text yet above the parent's own background. They still cannot escape the parent's stacking context.
Does sticky work inside a flex or grid container?
Yes. Sticky is independent of the parent's display type. The common failure is not the layout mode but an overflow: hidden ancestor on the scroll path, or a parent with no extra height for the element to travel within.
Why is my fixed tooltip clipped by a parent with overflow: hidden?
overflow: hidden clips the entire subtree, including fixed descendants. Clipping is not positional, so no z-index escapes it. Render the tooltip outside the clipping ancestor, typically with a portal to document.body.
More cheatsheets:
- Flexbox Cheatsheet: Interactive Visual Guide
- CSS Grid Cheatsheet: Interactive Visual Guide
- CSS Animation Cheatsheet: Interactive Visual Guide
- Box Shadow Cheatsheet: Interactive Visual Guide
- Border Radius Cheatsheet: Interactive Visual Guide
- CSS Transitions Cheatsheet: Interactive Visual Guide
- CSS Gradients Cheatsheet: Interactive Visual Guide
- CSS Transforms Cheatsheet: Interactive Visual Guide