Tutorials CSS Position Property
Ch 21 / 60
Chapter 21 Intermediate 8 min read CSS

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.

What you'll learn
All 5 position values: static, relative, absolute, fixed, sticky
The top, right, bottom, left offset properties
What "positioned ancestor" means
z-index and the stacking context
Centring with absolute position
Real-world patterns: nav bars, tooltips, badges, overlays

All 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:

static
Normal Flow
Default value. Element is in the normal document flow. Offsets have no effect. Cannot use z-index.
relative
Offset
Stays in normal flow but can be offset from its own position. Original space is preserved. Creates a stacking context.
absolute
Removed from Flow
Removed from flow. Positioned relative to nearest positioned ancestor. Other elements ignore it.
fixed
Fixed to Viewport
Removed from flow. Positioned relative to the viewport. Stays in place when scrolling. Navbars and FABs.
sticky
Hybrid
Relative until it hits a threshold, then acts like fixed within its scroll container. Sticky headers and TOCs.
CSS — position syntax
.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.

Static elements — normal document flow
Box 1 — position: static (default)
Box 2 — position: static
Box 3 — position: static

Even with top:50px set — nothing moves. Offsets are ignored.

CSS — position: static
/* 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.

Box 2 is shifted top:12px left:24px — its original space (ghost) is preserved
Box 1 — position: static
Box 2 — position: relative; top: 12px; left: 24px
← ghost (original space Box 2 keeps)
Box 3 — position: static (still below Box 2's original position)
CSS — position: relative
.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 */
}
The #1 use of position: relative
Setting 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.

position: relative (parent — the anchor)
top:28px left:12px
top:28px right:12px
bottom:12px left:12px
bottom:12px right:12px
50% 50% translate

Classic Centering with absolute

top+left 50% + translate
Perfectly centred
inset:0 + margin:auto
inset:0 margin:auto
CSS — position: 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;
}
The "positioned ancestor" rule
If no ancestor has 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.

Simulated viewport (scroll to see fixed elements stay put)
position: fixed — stays at top
⊕
↓ scroll ↓
CSS — position: fixed
/* 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:

Item A-1
Item A-2
Item A-3
Item A-4
Item A-5
Item B-1
Item B-2
Item B-3
Item B-4
Item B-5
Item C-1
Item C-2
Item C-3
Item C-4
CSS — position: sticky
/* 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 */
sticky checklist
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: 1
z-index: 3 (front)
z-index: 2
CSS — z-index & stacking
/* 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;
}
Stacking context
Certain CSS properties create a new stacking context — a self-contained layer where z-index values are compared only within that context. Creating a stacking context: 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:

NEW
PRO
Card content
Card Badge
Parent: relative. Badge: absolute; top:-8px; right:-8px.
Image title
Image Overlay
Parent: relative. Overlay: absolute; inset:0. Text: absolute; bottom:0.
Hover me
Tooltip text
Tooltip
Trigger: relative. Tip: absolute; bottom:100%. Arrow: border trick.
Menu ▾
Profile
Settings
Sign out
Dropdown Menu
Trigger: relative. Menu: absolute; top:100%; left:0.
Dialog
position: fixed overlay
Modal / Dialog
Backdrop: fixed; inset:0. Dialog: fixed; top:50%; left:50%; translate.
Sticky table header
Row 1
Row 2
Row 3
Sticky Header
Header: sticky; top:0. Parent must be scrollable. Background required.
CSS — Real-world patterns
/* ── 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);
}
Free Resource
CSS3 Cheat Sheet — All Properties & Selectors
Complete reference covering all position values, z-index, offset properties and layout rules.
Download PDF

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:

Live Editor
✎ Editor
👁 Preview
Featured Course — Enroll Now
Modern Web Development With AI
Master HTML, CSS, JavaScript, React and Node.js with real projects and placement support.
120+
Chapters
40 hrs
Content
10 Real
Projects
4.9 ⭐
Rating
HTML5CSS3JavaScriptReact.jsNode.jsTailwind CSSAI Tools

Quick Check — Chapter 21

Questions & Answers
A positioned ancestor is any ancestor element that has 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.
Both are removed from the normal document flow, but they differ in their reference point and scroll behaviour:

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.
Two reliable methods:

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.
Multiple Choice Questions
Question 1 of 5
Which position value removes an element from the document flow AND keeps it visible at the same screen position when scrolling?
Question 2 of 5
What does setting position: relative on a parent element (with no offsets) primarily accomplish?
Question 3 of 5
What is the default value of the CSS position property?
Question 4 of 5
An absolutely positioned element has no positioned ancestors. Where does it position itself?
Question 5 of 5
Which of the following will cause position: sticky to stop working?
Chapter Summary
  • 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 with position: relative on the parent.
  • fixed — removed from flow. Positioned relative to the viewport. Stays on screen when scrolling. Use for navbars, FABs, banners.
  • sticky — hybrid: relative until threshold, then fixed within scroll container. Requires a threshold (top/bottom), no overflow:hidden ancestor, 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%) or inset:0; margin:auto.
  • Common patterns: card badge, image overlay, tooltip, dropdown menu, modal backdrop, sticky table header.
~8 min read
Intermediate level
Chapter 21 of 60

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel