CSS Position Property
By default every element flows in the document like words on a page. The position property lets you break out of that flow — offset elements from
their normal position, layer things on top of each other, pin elements to the screen, or create headers that
stick as users scroll. It's one of the most powerful and frequently misunderstood properties in CSS.
static,
relative, absolute, fixed, sticky
top, right, bottom, left offset propertiesz-index and the stacking
contextAll 5 Position Values — Overview
The position property accepts five values. The
first is the default. The other four all enable top, right, bottom, left offsets and are called "positioned" elements:
.element {
position: static; /* default — normal flow */
position: relative; /* offset from own position */
position: absolute; /* relative to positioned ancestor */
position: fixed; /* relative to viewport */
position: sticky; /* relative then fixed at threshold */
}
/* Offset properties — work with relative/absolute/fixed/sticky */
.positioned {
position: absolute;
top: 20px; /* distance from top edge */
right: 16px; /* distance from right edge */
bottom: 0; /* flush with bottom */
left: auto; /* browser calculates */
}
position: static — Normal Flow
static is the default. Every element starts here.
The element is placed by the browser in normal document flow. Offset properties (top, right, bottom, left) and z-index have no effect on static elements.
Even with top:50px set — nothing moves. Offsets are ignored.
/* Default — you rarely need to write this explicitly */
div { position: static; }
/* Offsets are IGNORED on static elements */
.box {
position: static;
top: 50px; /* no effect */
left: 20px; /* no effect */
z-index: 10; /* no effect */
}
/* Useful to reset a positioned element back to flow */
@media (max-width: 640px) {
.sidebar { position: static; }
}
position: relative — Offset from Own Position
With relative, the element stays in the normal
flow but can be shifted from where it would normally sit using offset properties. The key: the
original space is still reserved — other elements don't move.
.shifted {
position: relative;
top: 20px; /* moves DOWN 20px from normal position */
left: 30px; /* moves RIGHT 30px from normal position */
}
/* Note: original space is STILL reserved */
/* Negative offsets move in the opposite direction */
.up { position: relative; top: -10px; } /* moves UP */
.back{ position: relative; left: -10px; } /* moves LEFT */
/* Most important use: create a positioning CONTEXT for children */
.card {
position: relative; /* no offset needed — just establishes context */
}
.card .badge {
position: absolute;
top: 10px;
right: 10px; /* anchors to .card, not the page */
}
position: relative on a parent without any offset
values is the most common usage. It simply establishes a positioning context for
absolutely positioned children — making the parent act as the coordinate origin for position: absolute children.
position: absolute — Precise Placement
Absolutely positioned elements are removed from the normal flow —
they take up no space and other elements act as if they don't exist. They are placed relative to their
nearest positioned ancestor (any ancestor with position
other than static). If none exists, they are placed relative to the
<html> element.
Classic Centering with absolute
/* Parent must be "positioned" to act as anchor */
.parent { position: relative; } /* establishes coordinate origin */
.badge {
position: absolute;
top: -8px;
right: -8px;
}
/* Pin to corners using inset shorthand (modern) */
.overlay {
position: absolute;
inset: 0; /* top:0 right:0 bottom:0 left:0 */
}
/* ── Centring techniques ── */
/* Method 1: translate */
.centered {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
/* Method 2: inset + margin (modern) */
.centered {
position: absolute;
inset: 0;
margin: auto;
width: fit-content;
height: fit-content;
}
position set to anything other than static, the element is positioned relative to the initial containing
block (usually the <html> root). This is a common
source of bugs — always add position: relative to the intended parent
container.
position: fixed — Pinned to the Viewport
Fixed elements are also removed from the flow but positioned relative to the viewport — the visible browser window. They stay in the same screen position even when the user scrolls. Used for sticky navbars, floating action buttons, chat widgets and cookie banners.
/* Fixed navbar */
.navbar {
position: fixed;
top: 0;
left: 0;
right: 0; /* or width: 100% */
z-index: 1000; /* always on top */
}
/* Add padding-top to body equal to navbar height to avoid content hiding */
body { padding-top: 64px; }
/* Floating action button (FAB) */
.fab {
position: fixed;
bottom: 24px;
right: 24px;
width: 56px;
height: 56px;
border-radius:50%;
z-index: 500;
}
/* Cookie / notification banner */
.cookie-banner {
position: fixed;
bottom: 0;
left: 0;
right: 0;
z-index: 9999;
}
/* ⚠ Fixed positioning breaks inside elements with transform,
filter or perspective — those create a new containing block */
position: sticky — The Best of Both
sticky is a hybrid: the element behaves as relative until the user scrolls it to a defined threshold, then it "sticks"
and behaves like fixed — but only within its scroll container. The
element unsticks when its parent scrolls out of view.
Scroll the box below to see the section headers stick:
/* Sticky table header */
thead th {
position: sticky;
top: 0; /* threshold — sticks when top edge hits 0 */
z-index: 1;
background: white; /* MUST have background to cover scrolled content */
}
/* Sticky section headers (like iOS contacts list) */
.section-header {
position: sticky;
top: 0;
background: var(--bg);
}
/* Sticky sidebar TOC (top of sidebar) */
.toc {
position: sticky;
top: 80px; /* accounts for fixed navbar height */
max-height:calc(100vh - 100px);
overflow-y:auto;
}
/* ⚠ Common sticky failures:
1. Parent has overflow:hidden/auto/scroll — prevents sticking
2. No threshold set (top/bottom missing) — won't work
3. Parent is too short — sticks briefly then scrolls away */
position: sticky fails silently if: (1) no threshold is set (top, bottom, left, or right must be defined), (2) any
ancestor has overflow: hidden, auto
or scroll, (3) the sticky element's parent container is too short. Also
always set a background colour — without it, content scrolls behind transparently.
z-index & Stacking Context
z-index controls the stacking order of positioned
elements — which one appears on top when they overlap. Higher values are in front. z-index only works on positioned elements (not static).
/* z-index only works on positioned elements */
.behind { position: relative; z-index: 0; }
.middle { position: relative; z-index: 1; }
.infront { position: relative; z-index: 2; }
/* Common z-index scale for a full app */
:root {
--z-below: -1;
--z-base: 0;
--z-raised: 1;
--z-dropdown: 100;
--z-sticky: 200;
--z-navbar: 300;
--z-overlay: 400;
--z-modal: 500;
--z-toast: 600;
--z-tooltip: 700;
}
/* Negative z-index — behind the parent's background */
.deco {
position: absolute;
z-index: -1;
}
position + z-index !== auto, opacity < 1, transform, filter, isolation: isolate. This is why
z-index: 9999 sometimes doesn't work — the element is inside a stacking
context with a low z-index at the root level.
All 5 Values — Complete Comparison
| Value | Removed from flow? | Offsets work? | Positioned relative to | Scrolls with page? | z-index works? |
|---|---|---|---|---|---|
| static | No | No | N/A | Yes | No |
| relative | No (space kept) | Yes | Own position in flow | Yes | Yes |
| absolute | Yes | Yes | Nearest positioned ancestor | Yes (with page) | Yes |
| fixed | Yes | Yes | Viewport | No (stays put) | Yes |
| sticky | No (relative phase) | Yes (threshold) | Own position → scroll container | Until threshold, then sticks | Yes |
Real-World Positioning Patterns
These are the patterns you will use constantly in real projects — all built with CSS position:
relative. Badge: absolute; top:-8px; right:-8px.relative. Overlay: absolute; inset:0. Text: absolute; bottom:0.relative. Tip: absolute; bottom:100%. Arrow: border trick.relative. Menu: absolute; top:100%; left:0.fixed; inset:0. Dialog: fixed; top:50%; left:50%; translate.sticky; top:0. Parent must be
scrollable. Background required./* ── Badge on card ── */
.card { position: relative; }
.badge { position: absolute; top: -8px; right: -8px; }
/* ── Full image overlay ── */
.thumb { position: relative; overflow: hidden; }
.overlay { position: absolute; inset: 0; }
/* ── Tooltip ── */
[data-tip] { position: relative; }
[data-tip]::after{
content: attr(data-tip);
position: absolute;
bottom: calc(100% + 8px);
left: 50%;
transform: translateX(-50%);
}
/* ── Modal ── */
.backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 400; }
.dialog {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 500;
width: min(90vw, 540px);
}
Try It Yourself
The editor demonstrates all 5 position values in one page. Change offset values, swap
absolute for relative, or move the
position: relative off the parent to see how absolute escapes to the
page root:
Quick Check — Chapter 21
position set to anything other than static — i.e., relative, absolute, fixed, or sticky.It matters because
position: absolute places the element relative to
its nearest positioned ancestor. The offsets (top,
right, bottom, left) are measured from that ancestor's edges.If no positioned ancestor exists, the absolutely positioned element falls back to the
<html> initial containing block — often causing it to escape its
intended container and appear somewhere unexpected on the page.This is why you almost always pair
position: absolute children with
position: relative on the parent.
absolute:
— Positioned relative to the nearest positioned ancestor
— Scrolls with the page — the element moves when the user scrolls
— Use for: tooltips, dropdown menus, card badges, image overlays
fixed:
— Positioned relative to the viewport (the browser window)
— Stays put when the page scrolls — always visible at the same screen position
— Use for: sticky navigation bars, floating action buttons, cookie banners, chat widgets
Memory rule: absolute = relative to a parent box. fixed = relative to the screen.
static (default) places the element in the normal document flow with
no offset capability. top, left,
z-index all have zero effect.relative also keeps the element in the normal flow, but adds two
abilities:1. Offset — move the element from its natural position using
top, right, bottom, left. The original space is
still reserved.2. Positioning context — makes the element a positioned ancestor for any
position: absolute children.The second use is far more common in practice. Setting
position: relative on a parent without any offsets is the
standard way to contain absolutely positioned children.
position: sticky fails silently — no error, just normal scrolling.
The three most common causes:1. No threshold defined — you must set at least one of
top, bottom, left, or right. Without a threshold,
the browser doesn't know when to "activate" the sticky behaviour.2. Ancestor has overflow: hidden/auto/scroll — sticky positioning is relative to the nearest scrolling ancestor. If an ancestor has
overflow: hidden, the
element is confined to that container and appears stuck or not sticking.3. Parent is too short — sticky elements only stick within their parent's boundaries. If the parent is the same height as the sticky element, there is no room to stick.
Also remember: always set a background colour on sticky elements — without it, content scrolls behind them transparently, which looks broken.
Method 1 — translate (most common):
position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);top: 50%; left: 50% moves the element's top-left corner to
the centre. The translate(-50%, -50%) shifts it back by half its own
width and height, centering it perfectly. Works even with unknown element dimensions.Method 2 — inset + margin (modern):
position: absolute;inset: 0;margin: auto;width: fit-content;height: fit-content;inset: 0 stretches to all edges, then margin: auto distributes the space equally — centering the element.
Requires explicit width/height or fit-content.
position: relative on a
parent element (with no offsets) primarily accomplish?position
property?position: sticky to stop working?static(default) — in normal flow. Offsets and z-index have no effect.relative— stays in flow but can be offset from own position. Original space is preserved. Most importantly: creates a positioning context for absolute children.absolute— removed from flow. Positioned relative to nearest positioned ancestor (or<html>if none). Always pair withposition: relativeon the parent.fixed— removed from flow. Positioned relative to the viewport. Stays on screen when scrolling. Use for navbars, FABs, banners.sticky— hybrid:relativeuntil threshold, thenfixedwithin scroll container. Requires a threshold (top/bottom), nooverflow:hiddenancestor, and a background colour.- Offsets —
top,right,bottom,left. Modern shorthand:inset: 0(all four sides). z-index— stacking order. Only works on positioned elements. Higher = in front. Use a CSS variable scale (--z-dropdown: 100, --z-modal: 500 etc.) for maintainability.- Centre with absolute:
top:50%; left:50%; transform:translate(-50%,-50%)orinset:0; margin:auto. - Common patterns: card badge, image overlay, tooltip, dropdown menu, modal backdrop, sticky table header.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.