Explain it simply
Explain it simply
Think of normal flow as rows of cinema seats: everyone sits in order in their own seat. relative is leaning sideways while staying seated; the seat is still yours. absolute is standing up and placing yourself by measuring from some wall of the room, and the others close up the gap where your seat was.
fixed is a sticker on the screen itself: however the film moves, it stays in the same spot. sticky sits normally until scrolling brings it to the edge, then it sticks there, but it leaves together with its group (its parent) when that group scrolls away.
Problem → Why → Solution
The problem: the badge flies to the page corner
You want a "-20%" badge in the top-right corner of a product card, so you write position: absolute; top: 8px; right: 8px;. Instead of sitting on the card, the badge lands in the top-right corner of the whole page, and the badges of every card pile up on that same spot. When you scroll down they stay at the top of the document instead of following their cards.
.card {
width: 300px;
/* no position here */
}
.badge {
position: absolute;
top: 8px;
right: 8px;
}Why does this happen?
An absolute element measures top and right from its containing block, which is the nearest ancestor whose position is not static. .card is static, so the browser keeps climbing, finds nothing, and falls back to the initial containing block, a viewport-sized rectangle at the top of the document. Adding position: relative; to .card makes the card the badge's containing block.
Core concept: stay in flow, and measure from where?
Each position value becomes clear once you ask two questions. First, does the element still keep its space in normal flow? Second, which box are top, right, bottom and left (the offset properties) measured from? static is the default: the element sits in flow normally and ignores the offsets and z-index.
| Value | Space in flow | Offsets measured from |
|---|---|---|
| relative | Kept | Its own normal position |
| absolute | Removed | Nearest non-static ancestor |
| fixed | Removed | The viewport (with an exception) |
| sticky | Kept | Nearest scrolling ancestor |
A relative element given top: 10px is drawn 10px lower, but the elements below still lay out around its original spot, so they can overlap. That is why relative is used more often as a containing block for absolute children than for nudging. An absolute element with both left: 0 and right: 0 and an auto width stretches across its containing block, and inset: 0 is the shorthand that sets all four offsets to 0 at once.
The exception for fixed: if an ancestor has a transform, filter or perspective, that ancestor becomes the fixed element's containing block, and the element scrolls with the ancestor instead of staying on the viewport. sticky behaves like relative until its threshold (for example top: 0) is reached, then holds at that edge of its scroll container, but it never leaves its parent's box.
Elements that leave the flow (absolute and fixed) have two more side effects. First, their parent no longer counts them when computing its own height, so a parent whose children are all absolute collapses to a height of 0. Second, with width: auto they no longer stretch across the line like a block; they shrink to fit their content.
- Is its space kept in flow?
- Find the containing block
- Measure offsets from that box
- Check what it moves with on scroll
Whenever an element lands somewhere unexpected, ask "who is its containing block?" first.
General pattern
parent {
position: relative; /* becomes the containing block */
}
child {
position: absolute | fixed | sticky;
top: length; /* and/or right, bottom, left */
inset: length; /* shorthand for all four */
}Make the parent the containing block with relative, then give the child a position value and offsets. An absolute element without offsets stays where it would have been in flow (its static position).
Example: a chat button and a card badge
This CSS uses two kinds of positioning. The chat button stays in a corner of the viewport, while the badge sits in a corner of its card. For the badge to work, it must live inside a card that has position: relative.
.chat-button {
position: fixed;
right: 20px;
bottom: 20px;
z-index: 50;
}
.badge {
position: absolute;
top: 8px;
right: 8px;
}Code breakdown
- Line 2
position: fixed;Takes the button out of flow and uses the viewport as its containing block, so page scrolling does not move it.
- Lines 3-4
right: 20px;right and bottom are measured from the viewport's right and bottom edges, so the button sits 20px in from the bottom-right corner.
- Line 5
z-index: 50;Because the button is positioned, z-index applies and keeps it above page content; the next lesson covers this in depth.
- Line 9
position: absolute;The badge leaves the flow, so it does not push the card's text around. Its containing block is the card, the nearest non-static ancestor.
- Lines 10-11
top: 8px;top and right are measured from the card's padding-box edges, placing the badge 8px inside the card's top-right corner.
The chat button will always stay in the bottom-right corner of the browser. The badge will stick to the top-right corner of its parent card.Three reasons a sticky header will not stick
No threshold
position: sticky alone does nothing; you must say where to stick, for example top: 0.
An ancestor with overflow
If an ancestor in between has overflow: hidden or auto, it becomes a new scroll container and sticky watches that box only. If that box never scrolls itself, you never see the element stick.
The parent is too short
A sticky element can only travel inside its parent's box. If the parent is barely taller than the header, there is no room left to stick.
In real projects
Where this shows up in real projects
absolute is the tool for small overlays: a sale badge on a card, an online dot on an avatar, a clear button inside an input, a dropdown menu. The pattern is always the same: make the parent relative, then place the child with absolute. Do not build the main page structure, such as columns, header and footer, with absolute; as soon as content length changes things overlap, so use flex or grid instead.
fixed is used for chat widgets, cookie banners, back-to-top buttons and bottom navigation bars on mobile. sticky shows up in site headers, table column headings and the A, B, C letter headings of a contact list. Fixed bars cover content, so remember to add matching padding-bottom to the body, otherwise the last lines of the page hide underneath the bar.
Common mistakes
Common mistakes
1. Forgetting position: relative on the parent
Absolute badges piling up in the page corner is this mistake. Select the badge in DevTools and check $0.offsetParent in the console to see which element it is measured from.
2. Nudging with relative and leaving a hole
Moving a heading up with position: relative; top: -40px; leaves a 40px hole where it used to be, because the flow still reserves that space. Change the margin when you are really fixing spacing.
3. A transform trapping a fixed modal
Put a fixed modal inside a panel that has a transform for its slide-in animation, and the modal no longer covers the viewport: it is sized to the panel and scrolls with it. Render modals as direct children of body.
Best practices
Best practices
- Put position: relative on the root of every component that has absolute children, so badges travel with the component wherever it is used.
- Build the main structure, such as page columns, header and footer, with flex or grid, and keep positioning for overlays.
- On pages with a sticky header, give anchor targets a scroll-margin-top equal to the header height so they do not land underneath it.
Mini challenge
Mini challenge: a badge and sticky headings
Pin a discount badge onto a product card, and make the letter headings in a scrolling list sticky.
- .badge sits 8px in from the card's top-right corner
- The badge must not push the card's text (it leaves the flow)
- While .list scrolls, each h3 holds at the top of .list
- When the next section arrives, it pushes the previous heading out
<style>
.card {
/* TODO: make the card the badge's containing block */
}
.badge {
/* TODO: 8px in from the card's top-right corner */
}
.list {
/* TODO: 200px tall and scrollable */
}
.list h3 {
/* TODO: hold at the top of .list while scrolling */
}
</style>
<div class="card"><h2>Wireless Mouse</h2><span class="badge">-20%</span></div>
<div class="list">
<section>
<h3>A</h3><p>Apple</p><p>Apricot</p><p>Avocado</p><p>Acai</p><p>Almond</p>
</section>
<section>
<h3>B</h3><p>Banana</p><p>Blueberry</p><p>Brazil nut</p><p>Bilberry</p>
</section>
</div>Expected: The badge appears near the card's top-right corner and moves with the card. Scroll the list by 100px and the "A" heading holds at the top of the list until the "B" section arrives and pushes it out.
Show hint
Make the card relative. Give the headings a background, otherwise the scrolling text shows through while they stick.
Show solution
<style>
.card {
position: relative;
width: 260px;
padding: 16px;
border: 1px solid #ccc;
}
.badge {
position: absolute;
top: 8px;
right: 8px;
padding: 2px 8px;
background: crimson;
color: white;
}
.list {
height: 200px;
overflow-y: auto;
border: 1px solid #ccc;
}
.list h3 {
position: sticky;
top: 0;
margin: 0;
padding: 4px 8px;
background: #f1f5f9;
}
</style>
<div class="card"><h2>Wireless Mouse</h2><span class="badge">-20%</span></div>
<div class="list">
<section>
<h3>A</h3><p>Apple</p><p>Apricot</p><p>Avocado</p><p>Acai</p><p>Almond</p>
</section>
<section>
<h3>B</h3><p>Banana</p><p>Blueberry</p><p>Brazil nut</p><p>Bilberry</p>
</section>
</div>Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- relative and sticky keep their space in flow; absolute and fixed leave the flow.
- An absolute element measures from the nearest non-static ancestor, so give the parent position: relative.
- fixed sticks to the viewport, unless an ancestor with a transform or filter captures it.
- sticky needs a threshold such as top: 0, a container that actually scrolls, and a parent with room to travel.
CSS reference — MDN Web Docs