CSS Positioning: relative, absolute, fixed, sticky, and z-index
Lesson 11 of 41 in the CSS3 learning path.
How do relative, absolute, fixed, and sticky positioning differ?
Flow is how elements stack down the page. relative keeps a box in that flow, can nudge it, and becomes the containing block for absolute children. absolute removes a box from flow and anchors it to the nearest positioned ancestor, which is how a badge sits in a card corner. fixed anchors to the viewport. sticky acts like relative until a scroll would carry it off, then it holds at the offset you set.
The sample uses all four values. A course bar is sticky at top: 0. Each card is relative so its level badge, positioned absolute, stays in that card's corner. A help button is fixed to the viewport. z-index compares boxes only inside one stacking context. The card sets z-index: 0, so the badge cannot climb over the sticky bar. Scroll the page to see the bar hold and the badge travel with its card.
Syntax & Parameters
.card { position: relative; z-index: 0; }
.badge { position: absolute; top: 0.7rem; right: 0.7rem; }
.bar { position: sticky; top: 0; z-index: 20; }
.pin { position: fixed; right: 1rem; bottom: 1rem; }
Example
Interactive preview available*, *::before, *::after { box-sizing: border-box; }
body {
margin: 0;
font-family: "Segoe UI", system-ui, sans-serif;
line-height: 1.5;
color: #1c2430;
background: #f4f1ea;
}
.topbar {
position: sticky;
top: 0;
z-index: 20;
display: flex;
align-items: center;
justify-content: space-between;
padding: 0.7rem 1rem;
background: #fff;
border-bottom: 1px solid #e3ddd2;
}
.topbar span { color: #5c6b7a; font-size: 0.85rem; }
.page { max-width: 40rem; margin-inline: auto; padding: 1rem 1rem 4.5rem; }
h1 { margin: 0 0 0.35rem; font-size: 1.4rem; }
.lead { margin: 0 0 0.9rem; color: #5c6b7a; }
.course-card {
position: relative;
z-index: 0;
margin: 0 0 0.8rem;
padding: 0.95rem 1rem;
background: #fff;
border: 1px solid #e3ddd2;
border-radius: 12px;
}
.badge {
position: absolute;
top: 0.7rem;
right: 0.7rem;
z-index: 5;
padding: 0.12rem 0.45rem;
border-radius: 999px;
background: #0f766e;
color: #f8fafc;
font-size: 0.72rem;
font-weight: 700;
}
.course-card h2 { margin: 0 3rem 0.3rem 0; font-size: 1.05rem; }
.course-card p { margin: 0; }
.nudge {
position: relative;
left: 0.75rem;
margin: 0.75rem 0 0;
color: #5c6b7a;
font-size: 0.85rem;
}
.help-pin {
position: fixed;
right: 1rem;
bottom: 1rem;
z-index: 30;
padding: 0.55rem 0.8rem;
border: 0;
border-radius: 999px;
background: #1d4ed8;
color: #fff;
font: inherit;
font-weight: 700;
}
Expected Output
A white course bar sits at the top of the viewport. When you scroll, the bar stays stuck to the top and the cards slide underneath it. Each card is in normal flow, one after another, with an 0.8rem bottom margin. A teal level badge sits in the top-right corner of its own card and scrolls away with that card. It does not jump to the corner of the page. A blue Help button stays in the bottom-right of the viewport the whole time. A short note under the cards is shifted 0.75rem to the right, and the space it originally occupied is still there. When the sticky bar overlaps a card, the bar paints on top. The badge, even with z-index 5, does not climb over the bar.
Line-by-Line Walkthrough
- Static is the initial position. The headings and paragraphs stay in normal flow until a position value takes them out of it.
- .topbar is sticky with top: 0. It behaves like a relative bar until the scroll would push it off, then it sticks to the top of the viewport.
- Sticky needs a scroll container that is an ancestor. Overflow hidden on a parent between the bar and the viewport will stop it from sticking.
- .course-card is relative with no offset. It stays in flow, and it becomes the containing block for the absolute badge.
- .badge is absolute at top and right 0.7rem. Those offsets are against the card padding edge, not against the page, because the card is positioned.
- Absolute removes the badge from flow, so the heading uses a right margin to keep its text from sliding under the badge.
- .nudge is relative with left: 0.75rem. It shifts visually, and the original gap in the flow stays put. Relative does not hand its space to the next sibling.
- .help-pin is fixed to the viewport. Scrolling the cards does not move it. fixed uses the viewport as its containing block, ignoring the card.
- z-index only competes inside one stacking context. The card creates a context by combining position: relative with z-index: 0.
- The badge z-index of 5 is trapped in that card context. The bar at z-index 20 on the root context still paints above the badge when they overlap.
- The help button is z-index 30 in the root context, so it stays above the bar and the cards. A child cannot escape its parent context by picking a bigger number.
Scroll the lesson. The bar holds, each badge travels with its card, and Help stays in the corner.
Common Pitfalls & Security
Sticky fails more often from an ancestor than from a wrong top value. Absolute fails more often because the containing block is the page instead of the card you intended.
- position: sticky does not stick if any ancestor has overflow set to hidden, auto, or scroll, and that ancestor is not the scroll container you expected.
- An absolute badge with no positioned ancestor is placed against the initial containing block, so it jumps to the page corner.
- fixed is anchored to the viewport, not to a transformed parent. A transform, filter, or will-change on an ancestor becomes the containing block and the pin appears to stick to that box instead.
- z-index on a static element is ignored. The element also needs a position other than static before the number does anything.
- A fixed control with a high z-index can cover buttons and capture clicks. Keep the hit area small, and do not use it to hide a consent or security prompt.
Best Practices & Modern Standards
Leave layout to flex and grid. Use positioning for the exceptions: a sticky page bar, a badge in the corner of a relative card, and a fixed help control. Give the relative parent z-index: 0 only when you understand you just created a stacking context. Keep badge offsets inside that parent so they do not depend on the page.
Prefer a flex row for the bar contents and a normal document flow for the cards. Custom properties can store the bar height if later content needs scroll-margin or padding that clears it. A container query can still change the card layout. Positioning does not replace that. If the reader asked for less motion, do not animate the sticky bar or the fixed pin.
Real-World Use
A lesson index has a sticky course title bar, cards for each module, and a level badge pinned to the corner of the card the student is reading. A Help button stays fixed while the modules scroll. The badge never drifts to the viewport, because the card is the containing block.
The same four values cover a store product card with a sale badge, a dashboard with a sticky filter bar, and a support widget fixed to the corner. The columns of the page, if it has a sidebar, still come from grid.
Frequently Asked Questions
What is the containing block for an absolute element?
It is the nearest ancestor whose position is relative, absolute, fixed, or sticky. In this sample that ancestor is the card, so top and right are measured from the card. If no ancestor is positioned, the badge is placed against the page, which is why a corner badge seems to fly to the viewport.
How is sticky different from fixed?
Sticky keeps its place in flow until a scroll threshold, then holds at the offset you set, usually top: 0. Fixed is taken out of flow immediately and stays relative to the viewport. A sticky bar leaves no hole once it sticks, because it was already in the flow. A fixed bar does not occupy flow at all.
Why can a child with a huge z-index still paint underneath?
A positioned element with a z-index other than auto creates a stacking context. Children compete inside that context, and the whole context is then ordered against its siblings. The card context is below the sticky bar, so the badge cannot climb over the bar no matter how large its own z-index is.
Does relative with an offset remove the element from flow?
No. The element is painted in a shifted place, and the original box still occupies flow, which is what the nudged note demonstrates. Absolute and fixed do remove the box from flow, so later siblings move up into the gap. Use relative when you only need a containing block or a small optical shift.