Position Utilities
CSS positioning lifts elements out of the normal document flow and lets you place them precisely — relative to their parent, the viewport, or the page itself. Tailwind exposes all five position values as utility classes, along with a complete set of inset (top, right, bottom, left) and z-index utilities. Mastering these is essential for tooltips, modals, sticky headers, floating action buttons, and overlay patterns.
relative on the parent matters
top-*, right-*, bottom-*, left-*, and shorthand inset-*
All Position Values at a Glance
Every CSS position value has a direct Tailwind
class. Unlike display utilities, position classes only define how the element
is positioned — you still need inset utilities (top-*,
left-*, etc.) to actually move it.
static & relative
static is the default for all elements — you
rarely need to write it explicitly (only to reset a position set by a parent or plugin).
relative is one of the most important positioning
classes because it creates the containing block that absolute children
are positioned against.
<!-- static — the default, rarely written explicitly -->
<div class="static">Resets position to default</div>
<!-- relative — offset from natural position -->
<span class="relative top-1">Nudged down 4px from normal position</span>
<!-- ✅ The most common use — containing block for absolute children -->
<div class="relative">
<img class="block w-full rounded-xl" src="photo.jpg" alt="...">
<!-- Badge pinned to top-right of the image -->
<span class="absolute top-3 right-3
bg-cyan-500 text-white text-xs font-bold px-2 py-1 rounded-full">
NEW
</span>
</div>
<!-- relative to offset from natural position with offset utilities -->
<sup class="relative -top-2 text-xs text-cyan-500">®</sup>
absolute — Precise Placement
absolute removes an element from the document flow
and positions it relative to its nearest positioned ancestor (any ancestor
with relative, absolute,
fixed, or sticky).
If no such ancestor exists, it's positioned relative to the initial containing block
(the <html> element).
absolute, always add relative to the parent
you want to position against. Forgetting this is the #1 positioning mistake.
<!-- ── Corner placements ──────────────────────────────── -->
<div class="relative">
<img class="w-full rounded-2xl" src="..." alt="...">
<span class="absolute top-3 left-3 ...">Top left badge</span>
<span class="absolute top-3 right-3 ...">Top right badge</span>
<span class="absolute bottom-3 left-3 ...">Bottom left</span>
<span class="absolute bottom-3 right-3 ...">Bottom right</span>
</div>
<!-- ── Perfectly centred (horizontal + vertical) ──────── -->
<div class="relative h-48 bg-gray-100 dark:bg-gray-800 rounded-xl">
<div class="absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2
bg-cyan-500 text-white px-6 py-3 rounded-xl font-semibold">
Perfectly centred
</div>
</div>
<!-- ── Full overlay (cover entire parent) ─────────────── -->
<div class="relative">
<img class="block w-full rounded-2xl" src="..." alt="...">
<!-- Dark overlay covering the full image -->
<div class="absolute inset-0 bg-black/50 rounded-2xl"></div>
<!-- Text on top of the overlay -->
<div class="absolute inset-0 flex items-end p-6">
<h3 class="text-white text-xl font-bold">Card Title</h3>
</div>
</div>
<!-- ── Horizontally centred at bottom ─────────────────── -->
<div class="relative">
<div class="absolute bottom-4 left-1/2 -translate-x-1/2
bg-white dark:bg-gray-800 shadow-xl rounded-full px-6 py-2">
Bottom-centred label
</div>
</div>
fixed — Viewport-Relative Placement
fixed removes an element from the document flow
and positions it relative to the viewport — it stays in the same
position on screen regardless of how far the user scrolls. Common use cases include
top navigation bars, floating action buttons, cookie banners, and chat widgets.
<!-- ── Fixed top navbar ───────────────────────────────── -->
<nav class="fixed top-0 left-0 right-0 z-50
bg-white dark:bg-gray-950 border-b border-gray-100 dark:border-gray-800
shadow-sm backdrop-blur-sm bg-white/90 dark:bg-gray-950/90">
<div class="container mx-auto px-4 h-16 flex items-center justify-between">
<span class="font-bold text-xl">Logo</span>
<nav class="flex gap-6 text-sm">Nav links</nav>
</div>
</nav>
<!-- Add padding-top to body content to avoid navbar overlap -->
<main class="pt-16">Page content</main>
<!-- ── Floating action button (bottom-right) ──────────── -->
<button class="fixed bottom-6 right-6 z-40
w-14 h-14 rounded-full bg-cyan-500 hover:bg-cyan-600
text-white shadow-xl shadow-cyan-500/30
flex items-center justify-center
transition-all duration-200 hover:scale-110">
<iconify-icon icon="solar:chat-round-bold" width="22"></iconify-icon>
</button>
<!-- ── Cookie banner (bottom of viewport) ─────────────── -->
<div class="fixed bottom-0 left-0 right-0 z-50
bg-gray-900 text-white px-6 py-4
flex flex-col sm:flex-row items-center justify-between gap-4">
<p class="text-sm">We use cookies to improve your experience.</p>
<button class="shrink-0 bg-cyan-500 hover:bg-cyan-600 px-5 py-2 rounded-lg text-sm font-semibold">Accept</button>
</div>
<!-- ── Modal overlay ─────────────────────────────────────── -->
<div class="fixed inset-0 z-50 flex items-center justify-center p-4
bg-black/60 backdrop-blur-sm">
<div class="bg-white dark:bg-gray-900 rounded-2xl p-8 max-w-md w-full shadow-2xl">
<h2 class="text-xl font-bold mb-4">Modal Title</h2>
<p class="text-gray-500">Modal content goes here.</p>
</div>
</div>
sticky — Scroll-Aware Positioning
sticky is a hybrid — it behaves like
relative until the element reaches the scroll
threshold (defined by top-*), then it "sticks"
and behaves like fixed within its parent's
bounds. When the parent scrolls past, the element goes with it.
sticky only works when you add a threshold value like
top-0. Without it, the element won't stick. Also, the parent container
must be taller than the sticky element and must not have overflow: hidden.
<!-- ── Sticky navbar ─────────────────────────────────────── -->
<!-- Unlike fixed, sticky stays in document flow -->
<nav class="sticky top-0 z-40
bg-white/90 dark:bg-gray-950/90 backdrop-blur-sm
border-b border-gray-100 dark:border-gray-800">
<div class="container mx-auto px-4 h-16 flex items-center">
Navigation content
</div>
</nav>
<!-- No pt-16 needed — sticky stays in the flow until it sticks -->
<!-- ── Sticky section headers (table of contents style) ───── -->
<div>
<h2 class="sticky top-16 z-10
bg-gray-50 dark:bg-gray-900
px-4 py-2 text-sm font-bold text-gray-500 uppercase tracking-widest
border-b border-gray-200 dark:border-gray-700">
Section A
</h2>
<ul>
<li class="px-4 py-3 border-b">Item 1</li>
<li class="px-4 py-3 border-b">Item 2</li>
</ul>
<h2 class="sticky top-16 z-10 bg-gray-50 dark:bg-gray-900 px-4 py-2 ...">
Section B
</h2>
<ul>...</ul>
</div>
<!-- ── Sticky sidebar (inside a scrollable layout) ─────────── -->
<div class="grid grid-cols-1 lg:grid-cols-[280px_1fr] gap-8 items-start">
<aside class="sticky top-20">
Sidebar sticks at 80px from top while main content scrolls
</aside>
<main>Long main content</main>
</div>
| Feature | fixed | sticky |
|---|---|---|
| In document flow | ✗ No — takes no space | ✓ Yes — takes space until it sticks |
| Positioned relative to | Viewport always | Scrolling container, until threshold reached |
| Body padding needed | ✓ Yes — add pt-16 to avoid overlap | ✗ No — stays in flow initially |
| Scrolls past parent | No — stays fixed always | ✓ Yes — sticks only within its parent |
| requires top-* value | Optional | ✓ Required — otherwise won't stick |
| Best for | Navbars, FABs, modals, cookie banners | Section headers, sidebar TOC, table headers |
Inset Utilities — top, right, bottom, left
After setting a position value, inset utilities control where the element
goes. Tailwind provides individual directional utilities (top-*,
right-*, bottom-*,
left-*) and the shorthand
inset-* that sets all four at once.
<!-- Cover entire parent (overlay pattern) -->
<div class="absolute inset-0 bg-black/50"></div>
<!-- Full width, pinned at top -->
<div class="absolute inset-x-0 top-0 h-1 bg-gradient-to-r from-cyan-500 to-purple-500"></div>
<!-- Full height, pinned at left (sidebar) -->
<div class="absolute inset-y-0 left-0 w-64 bg-white"></div>
<!-- Precisely centred with translate -->
<div class="absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2">
Perfectly centred
</div>
<!-- Tooltip below element -->
<div class="relative group">
<button>Hover me</button>
<div class="absolute top-full left-1/2 -translate-x-1/2 mt-2
bg-gray-900 text-white text-xs px-3 py-1.5 rounded-lg
invisible group-hover:visible opacity-0 group-hover:opacity-100
transition-all duration-200 whitespace-nowrap">
Tooltip text
</div>
</div>
<!-- Negative inset — poke outside the parent -->
<div class="relative">
<div>Card content</div>
<span class="absolute -top-3 -right-3
w-6 h-6 rounded-full bg-red-500 text-white text-xs
flex items-center justify-center font-bold">3</span>
</div>
Z-Index — Stacking Order
Z-index controls the stacking order of positioned elements. Higher z-index values
appear on top. Tailwind ships z-index values from
z-0 to z-50
in steps, plus z-auto. For UI layers (dropdowns,
modals, toasts) you can also add custom z-index values in the config.
| Class | CSS value | Common use |
|---|---|---|
| z-0 | z-index: 0 | Default stacking context reset |
| z-10 | z-index: 10 | Slightly elevated — dropdowns, tooltips |
| z-20 | z-index: 20 | Sticky headers, side panels |
| z-30 | z-index: 30 | Fixed navbars, overlays |
| z-40 | z-index: 40 | Drawers, dialogs |
| z-50 | z-index: 50 | Modals, toast notifications, highest layer |
| z-auto | z-index: auto | Browser-determined stacking |
| -z-10 | z-index: -10 | Behind all content — background decorations |
/* Recommended z-index layer system */
/* Add to tailwind.config.js theme.extend.zIndex if needed */
<!-- Level 10 — dropdowns / popovers -->
<div class="absolute z-10 mt-2 w-56 bg-white rounded-xl shadow-xl border">
Dropdown menu
</div>
<!-- Level 20 — sticky headers -->
<header class="sticky top-0 z-20 bg-white border-b">Sticky header</header>
<!-- Level 30 — fixed navbar (above sticky headers) -->
<nav class="fixed top-0 left-0 right-0 z-30 bg-white">Fixed nav</nav>
<!-- Level 40 — drawer / side panel -->
<aside class="fixed inset-y-0 left-0 z-40 w-72 bg-white shadow-2xl">Drawer</aside>
<!-- Level 50 — modal (highest) -->
<div class="fixed inset-0 z-50 flex items-center justify-center bg-black/60">
<div class="bg-white rounded-2xl p-8 shadow-2xl">Modal</div>
</div>
<!-- Negative z-index — behind the page -->
<div class="absolute -z-10 inset-0 bg-gradient-to-br from-cyan-500/10 to-purple-500/10"></div>
/* Custom z-index in config */
theme: {
extend: {
zIndex: {
'60': '60', // z-60 for toasts above modals
'70': '70', // z-70
}
}
}
Real-World Positioning Patterns
Here are the six most common UI patterns built with Tailwind position utilities. Each pattern uses a combination of the positioning and inset classes covered in this chapter.
<!-- Notification badge on a bell icon -->
<button class="relative p-2 rounded-xl hover:bg-gray-100 dark:hover:bg-gray-800 transition-colors">
<iconify-icon icon="solar:bell-linear" width="22"></iconify-icon>
<!-- Notification count badge -->
<span class="absolute -top-1 -right-1
min-w-[18px] h-[18px] px-1
bg-red-500 text-white text-[10px] font-bold
rounded-full flex items-center justify-center
ring-2 ring-white dark:ring-gray-950">
3
</span>
</button>
<!-- Online status dot on avatar -->
<div class="relative w-10 h-10">
<img class="w-10 h-10 rounded-full object-cover" src="avatar.jpg" alt="User">
<span class="absolute bottom-0 right-0
w-3 h-3 rounded-full bg-green-400
ring-2 ring-white dark:ring-gray-900"></span>
</div>
<!-- Overlay backdrop -->
<div class="fixed inset-0 z-40 bg-black/50 backdrop-blur-sm"></div>
<!-- Side drawer sliding from the right -->
<aside class="fixed inset-y-0 right-0 z-50 w-full max-w-sm
bg-white dark:bg-gray-900 shadow-2xl
flex flex-col overflow-y-auto">
<!-- Drawer header -->
<div class="sticky top-0 flex items-center justify-between
px-6 py-4 border-b border-gray-100 dark:border-gray-800 bg-inherit">
<h2 class="text-lg font-semibold">Cart</h2>
<button class="p-2 rounded-lg hover:bg-gray-100 dark:hover:bg-gray-800">
<iconify-icon icon="solar:close-linear" width="20"></iconify-icon>
</button>
</div>
<!-- Drawer content -->
<div class="flex-1 overflow-y-auto px-6 py-4">
Cart items here...
</div>
<!-- Sticky footer inside drawer -->
<div class="sticky bottom-0 px-6 py-4 border-t border-gray-100 dark:border-gray-800 bg-inherit">
<button class="w-full bg-cyan-500 text-white py-3 rounded-xl font-semibold">Checkout</button>
</div>
</aside>
- static — the default, rarely written explicitly. relative stays in flow but creates a containing block for absolutely positioned children — this is its most important use.
- absolute — removed from flow, positioned relative to the nearest non-static
ancestor. Always add
relativeto the parent you want to position against. - fixed — removed from flow, stays relative to the viewport. Page content
needs padding to compensate (e.g.
pt-16for a 64px fixed navbar). - sticky — stays in flow until the threshold is reached, then sticks. Must
have a
top-*value to work. Parent must not haveoverflow: hidden. - Inset utilities —
inset-0covers the full parent,inset-x-0stretches horizontally, fractional values liketop-1/2enable precise centering. - Perfect centering —
absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2is the standard pattern for centering an absolute element inside its parent. - Z-index system — use 10-step increments for different UI layers: 10 (tooltips) → 20 (sticky headers) → 30 (fixed nav) → 40 (drawers) → 50 (modals). Negative z-index pushes elements behind the page.
- Sticky vs fixed — use sticky for section headers and sidebar TOCs (stays in flow, constrained to parent). Use fixed for top navbars, FABs, cookie banners, and full-screen modals (viewport-relative, always visible).
Quick Check — Questions & Answers
Click each question to reveal the detailed answer.
relative on the
parent. When the parent doesn't have a position other than
static, the absolutely positioned child travels up the DOM tree looking
for a positioned ancestor. It may find a grandparent or even the
<html> element and position itself relative to that instead of
the intended parent.Fix: add
relative to the direct parent that should contain the
badge:<div class="relative"> ← parent
<span class="absolute top-2 right-2">Badge</span>
</div>
sticky top-0 — the navbar stays in the normal document
flow. Content after it starts directly below it. When the user scrolls down, the
navbar sticks at the top of the viewport. No need to add extra padding to the page
body. It only sticks within its parent container.fixed top-0 — the navbar is immediately removed from
the document flow. Content beneath it would slide up underneath it. You must add
padding-top equal to the navbar height (e.g. pt-16 for a
64px navbar) to the main content. It is always visible regardless of the parent
container.Use sticky when you want the navbar to scroll normally at the top of the page and only "fix" after scrolling. Use fixed when the navbar should always be visible immediately, even if the page is at the very top.
absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2How it works:
1.
top-1/2 moves the element's top edge to the vertical centre of the
parent2.
left-1/2 moves the element's left edge to the horizontal centre3.
-translate-x-1/2 shifts the element left by 50% of its own width,
perfectly aligning its centre4.
-translate-y-1/2 shifts up by 50% of its own heightThis works regardless of the element's size. The parent needs
relative
(or any non-static position) and a defined height.
1. Missing threshold value —
sticky must have at least
one of top-*, right-*, bottom-*, or
left-* defined. Without it, the element never sticks.2. Parent has
overflow: hidden or
overflow: auto — this creates a new scroll container and
confines the sticky element within it. The sticky element won't be visible outside
the parent's bounds. This is often caused by a parent with
overflow-x-hidden applied for layout reasons.3. Parent is not tall enough — sticky only works while the element is still inside its parent's scrollable area. If the parent is the same height as the sticky element, there's no room to stick.
inset-0 is shorthand for
top: 0; right: 0; bottom: 0; left: 0. Combined with
absolute, it stretches the element to exactly cover its
positioned parent:<div class="relative">
<img class="block w-full rounded-2xl" src="...">
<div class="absolute inset-0 bg-black/50 rounded-2xl"></div>
</div>This is the standard pattern for image overlays, gradient fades, and any scenario where you want a layer to cover the entire area of its parent. You can also use
inset-4 to leave a 16px gap on all sides, or inset-x-0 to
stretch only horizontally.
MCQ Quiz — Test Yourself
Select the best answer. The correct option is revealed after you click.
absolute top-4 right-4 to an
element but it positions itself in the wrong place — at the top-right of the screen instead
of its card parent. What is the fix?absolute inset-0 do when applied to
a child of a relative parent?
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.