Layout Lesson 17 of 33
CSS Position — relative, absolute, fixed and sticky
Move elements out of the normal flow with the position property — relative, absolute, fixed and sticky — and control stacking with z-index.
Normally, elements are placed one after another in the normal flow: blocks stack down the page, inline elements flow in lines. The position property lets you break out of that flow, to put a "SALE" badge on the corner of a photo, keep a header at the top while scrolling, or show a chat button that floats over the page.
Once an element is positioned, you move it with four offset properties: top, right, bottom and left.
static (the default)
Every element starts as position: static. It sits in the normal flow, and top/left do nothing.
relative
position: relative moves an element relative to where it would normally be. Its original space is kept, so nothing else moves.
<style>
.box { width: 160px; padding: 14px; background: #e3f1ec; border: 2px solid #0f5c4d; margin: 6px 0; font-family: Arial, sans-serif; }
.moved { position: relative; top: 10px; left: 40px; background: #fdf3dc; border-color: #f2b33d; }
</style>
<div class="box">Box 1</div>
<div class="box moved">Box 2 (moved)</div>
<div class="box">Box 3</div>
Box 2 moved 10px down and 40px right, but Box 3 didn't move up to fill the gap. In practice, relative is used mostly for one reason: to be the anchor for an absolute child.
absolute
position: absolute removes an element from the normal flow (other elements act as if it isn't there) and places it relative to its nearest positioned ancestor — the closest parent with a position other than static. If there's none, it's placed relative to the whole page.
The classic pattern: parent gets relative, child gets absolute.
<style>
.card {
position: relative; /* the anchor */
width: 220px;
padding: 16px;
border: 1px solid #ddd;
border-radius: 12px;
font-family: Arial, sans-serif;
}
.badge {
position: absolute;
top: -10px;
right: -10px;
background: #CE1126;
color: white;
font-weight: bold;
padding: 6px 10px;
border-radius: 999px;
font-size: 13px;
}
.card img { width: 100%; }
</style>
<div class="card">
<span class="badge">-30%</span>
<img src="/img/mango.svg" alt="Dried mangoes">
<h3>Dried Mangoes</h3>
<p>₱120 <s>₱170</s></p>
</div>
top: -10px; right: -10px; places the badge slightly outside the card's top-right corner. Try removing position: relative from .card. The badge flies to the top-right of the whole frame, because now there's no positioned ancestor to anchor it.
Centering an overlay
Setting all four offsets to 0 makes an absolute element cover its whole parent, which is perfect for a dark overlay with text on an image:
.overlay {
position: absolute;
inset: 0; /* shorthand for top: 0; right: 0; bottom: 0; left: 0; */
background: rgba(0, 0, 0, 0.5);
}
fixed
position: fixed places an element relative to the browser window. It stays in the same spot on the screen even when you scroll. It's used for floating chat buttons, cookie notices and "back to top" buttons.
<style>
body { font-family: Arial, sans-serif; }
.chat {
position: fixed;
right: 16px;
bottom: 16px;
background: #0f5c4d;
color: white;
padding: 12px 18px;
border-radius: 999px;
box-shadow: 0 4px 12px rgba(0,0,0,0.25);
}
</style>
<h3>Scroll down ↓</h3>
<p style="height: 600px; background: linear-gradient(#fff, #e3f1ec);">Lots of content here…</p>
<a class="chat" href="#">💬 Chat with us</a>
sticky
position: sticky is a mix: the element scrolls normally until it reaches a set position (like top: 0), then it sticks there while its parent is still on screen. It's perfect for headers and table headings.
<style>
body { margin: 0; font-family: Arial, sans-serif; }
.header {
position: sticky;
top: 0;
background: #0f5c4d;
color: white;
padding: 12px;
}
p { padding: 0 12px; }
</style>
<div class="header">📌 Barangay San Roque — Announcements</div>
<p>Scroll down. The header sticks to the top.</p>
<p style="height: 700px;">More announcements…</p>
z-index: what's on top?
When positioned elements overlap, z-index decides which one is in front. A higher number appears on top. It only works on positioned elements (not static).
<style>
.c { position: absolute; width: 120px; height: 120px; border-radius: 16px; color: white; font: bold 18px Arial; display: grid; place-items: center; }
.a { background: #0038A8; left: 20px; top: 20px; z-index: 1; }
.b { background: #CE1126; left: 80px; top: 60px; z-index: 3; }
.y { background: #d4a800; left: 140px; top: 30px; z-index: 2; }
</style>
<div class="c a">z: 1</div>
<div class="c b">z: 3</div>
<div class="c y">z: 2</div>
Change the numbers and watch the cards change order. Modals and dropdown menus usually get a high z-index (like 100) so they appear above everything else.
Summary
| Value | Positioned relative to… | Keeps its space? |
|---|---|---|
static | Normal flow (offsets ignored) | Yes |
relative | Its own normal position | Yes |
absolute | Nearest positioned ancestor | No |
fixed | The browser window | No |
sticky | Normal, then sticks when scrolled | Yes |
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04