CSS Positioning Explained: Static, Relative, Absolute, Fixed & Sticky (With Code & Live Results)
Master CSS position properties (static, relative, absolute, fixed, sticky) with step-by-step code examples, visual result previews, z-index rules, and practical layout tips.
2026-09-26 Β· 11 min read
CSS positioning is one of the most fundamental concepts every web developer must master. Whether you are building floating badges, sticky headers, centered modals, or dropdown menus, understanding how the position property works is key to creating precise, modern web layouts.
In this guide, we will break down all 5 CSS position values (static, relative, absolute, fixed, and sticky), explain how offset properties (top, right, bottom, left) work, dive into z-index, and provide interactive-style code examples with live visual results!
Quick Summary of CSS position Values
| Position Value | Reference Point | Leaves Space in Document Flow? | Offset (top/left...) Works? | Supports z-index? | Common Use Case |
|---|---|---|---|---|---|
static | Normal document flow | Yes | No | No | Default value for standard page flow |
relative | Its own natural position | Yes (leaves ghost space) | Yes | Yes | Parent container for absolute elements |
absolute | Nearest positioned parent | No (removed from flow) | Yes | Yes | Badges, tooltips, dropdowns, modals |
fixed | Browser viewport (screen) | No (removed from flow) | Yes | Yes | Fixed navbars, Back-to-Top buttons |
sticky | Scroll container / Viewport | Yes (until scrolled) | Yes | Yes | Sticky headers, sticky sidebars |
Offset Properties: top, right, bottom, left
Before diving into each position mode, remember that offset properties move an element from its reference point:
top: Distance from the top edge.bottom: Distance from the bottom edge.left: Distance from the left edge.right: Distance from the right edge.
π‘ Important: Offset properties only work if
positionis set torelative,absolute,fixed, orsticky. They are completely ignored whenposition: static(the default).
1. position: static (Default Flow)
static is the default position for every HTML element. Elements are positioned according to the natural flow of the page: block elements stack vertically, and inline elements sit side-by-side.
Key Rules:
- Elements render in the exact order they appear in HTML.
top,right,bottom,left, andz-indexhave zero effect.
Code Example:
<div class="box box-1">Box 1 (Static)</div>
<div class="box box-2">Box 2 (Static)</div>
<div class="box box-3">Box 3 (Static)</div>
.box {
padding: 15px 20px;
margin: 10px 0;
border-radius: 8px;
color: #ffffff;
font-weight: bold;
}
.box-1 { background-color: #3b82f6; }
.box-2 { background-color: #10b981; }
.box-3 { background-color: #8b5cf6; }
β‘ Live Result Preview:
2. position: relative (Offset from Self)
An element with position: relative remains in the normal document flow, but you can offset it relative to where it would normally sit.
Key Rules:
- Shifted using
top,bottom,left, orright. - Leaves its original space empty (ghost space) in the layout; surrounding elements will NOT shift to fill the gap.
- Acts as the anchor container (
parent) for any child elements usingposition: absolute.
Code Example:
.box-normal {
background-color: #3b82f6;
}
.box-relative {
position: relative;
top: 20px;
left: 40px;
background-color: #f59e0b;
}
β‘ Live Result Preview:
π‘ Best Practice: When creating card components with notification dots, icons, or badges, always set
position: relative;on the card container!
3. position: absolute (Positioned Relative to Ancestor)
An element with position: absolute is completely removed from the normal document flow. It takes up no space in the page layout, and siblings behave as if it does not exist.
Key Rules:
- Positioned relative to its nearest positioned ancestor (an ancestor with
relative,absolute, orfixed). - If no positioned ancestor exists, it aligns relative to the root
<html>element. - Overlaps other elements easily.
Example A: Notification Badge on a Card
<div class="card">
<span class="badge">NEW</span>
<h3>Product Feature</h3>
<p>Learn how to use CSS absolute positioning like a pro.</p>
</div>
.card {
position: relative; /* Anchor container */
width: 300px;
padding: 20px;
background-color: #1e293b;
border-radius: 12px;
}
.badge {
position: absolute;
top: -10px;
right: -10px;
background-color: #ef4444;
color: white;
padding: 4px 10px;
font-size: 12px;
border-radius: 9999px;
font-weight: bold;
}
β‘ Live Result Preview (Badge on Card):
Product Feature
This card has position: relative, making the position: absolute badge snap top-right cleanly!
Example B: Perfect Centering with position: absolute
You can center any element perfectly inside a container using top: 50%, left: 50%, and transform: translate(-50%, -50%).
.parent-container {
position: relative;
height: 200px;
}
.centered-modal {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: #0284c7;
color: white;
padding: 16px 24px;
border-radius: 8px;
}
β‘ Live Result Preview (Centered Modal Box):
4. position: fixed (Pinned to Viewport)
An element with position: fixed is removed from document flow and anchored to the browser window (viewport).
Key Rules:
- Does NOT scroll with the page. It stays pinned in the exact same spot on screen.
- Useful for header navigation bars, floating action buttons (FAB), chat widgets, and cookie consent overlays.
Code Example (Floating Chat Button):
.floating-chat-btn {
position: fixed;
bottom: 20px;
right: 20px;
background-color: #2563eb;
color: white;
padding: 14px 20px;
border-radius: 50px;
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.3);
z-index: 999;
}
β‘ Live Result Preview:
5. position: sticky (Hybrid Relative + Fixed)
sticky positioning is a hybrid between relative and fixed. It behaves as relative until the user scrolls past a designated threshold (such as top: 0), at which point it sticks to that position on screen.
Key Rules:
- Must specify at least one offset property (e.g.
top: 0;). - Remains sticky only within its parent container. Once the parent container scrolls out of view, the sticky element scrolls away too.
- Will NOT work if any parent container has
overflow: hidden,overflow: auto, oroverflow: scroll.
Code Example:
.sticky-header {
position: sticky;
top: 0;
background-color: #0f172a;
color: #38bdf8;
padding: 12px 16px;
font-weight: bold;
z-index: 10;
}
β‘ Live Result Preview (Scroll inside the container to see sticky behavior):
<div style="position: sticky; top: 0; background-color: #0284c7; color: white; padding: 10px 16px; font-weight: 700; font-size: 14px; border-bottom: 1px solid #0369a1; box-shadow: 0 4px 6px -1px rgba(0,0,0,0.3);">
π Sticky Section Header (Sticks to Top when scrolling)
</div>
<div style="padding: 16px; color: #cbd5e1; font-size: 14px; line-height: 1.8;">
<p style="margin: 0 0 12px 0;">Paragraph 1: Keep scrolling down! The blue header box stays pinned at top = 0 while reading content.</p>
<p style="margin: 0 0 12px 0;">Paragraph 2: Sticky headers are perfect for category lists, table headers, and blog chapter navigation.</p>
<p style="margin: 0 0 12px 0;">Paragraph 3: As long as the parent container is visible, the sticky element holds its place!</p>
<p style="margin: 0 0 12px 0;">Paragraph 4: You reached the middle of the article text.</p>
<p style="margin: 0;">Paragraph 5: End of sticky demo content box.</p>
</div>
6. Understanding z-index & Stacking Context
When elements overlap using relative, absolute, fixed, or sticky, z-index determines which element sits in front and which sits behind.
Key Rules:
- Higher
z-indexvalues appear on top of lower values. - Default
z-indexisauto(evaluated as 0). z-indexonly works on positioned elements (ignored onposition: static).
Code Example:
.card-back {
position: relative;
z-index: 1;
background-color: #475569;
}
.card-front {
position: relative;
top: -40px;
left: 30px;
z-index: 10; /* Higher number comes to front */
background-color: #2563eb;
}
β‘ Live Result Preview:
7. Golden Rules for CSS Positioning
- Always set
position: relativeon parent cards when placingposition: absolutebadges, close buttons, or icons inside them. - Don't forget top/bottom/left/right when using
absolute,fixed, orstickyβotherwise they won't shift properly. - If sticky isn't sticking, check if any parent element has
overflow: hidden,overflow: auto, oroverflow: scroll. - Use
fixedsparingly on mobile devices so you don't take up valuable screen space for users. - Remember
z-indexrequires a position property (relative,absolute,fixed, orsticky). It will not work onstaticelements!
Conclusion
Understanding CSS positioning opens up endless possibilities for web layouts. Here is a quick reminder when choosing:
- Use
relativefor subtle offsets or setting up an anchor parent. - Use
absolutefor popups, tooltips, badges, and exact placement within a card. - Use
fixedfor floating action buttons or headers that stay on screen during page scroll. - Use
stickyfor section titles and table headers that scroll naturally until reaching the edge.
Happy coding! If you found this post helpful, share it with fellow developers!