Positioning, z-index & Stacking Context
π‘ Intermediate
π Definition
The position property specifies how an element is placed within the document flow using offset properties (top, right, bottom, left). The z-index property controls vertical front-to-back layer stacking order of overlapping positioned elements within a Stacking Context.
π Multilingual Explanation
English
position controls layout placement (static, relative, absolute, fixed, sticky). absolute positions relative to its nearest non-static ancestor. z-index controls layer stacking order.
Hindi
position elements ko jagah dene ke kaam aati hai. absolute apne sabse paas wale relative parent ke hisaab se set hota hai. Overlap hone wale elements ki layering ko z-index control karta hai.
Marathi
position mule element halavta yeto. Overlap zaleleya elements madhye z-index mule konta element var disel te tharte.
π Position Values Reference Table
| Value | Document Flow | Positioned Relative To | Common Use Cases |
|---|---|---|---|
static (Default) |
In normal flow | Normal document flow (top/left ignored) |
Default browser element layout |
relative |
In normal flow | Its own normal starting position | Parent container anchor for absolute children |
absolute |
Removed from flow | Nearest non-static ancestor element | Badge overlays, dropdown menus, modal popups |
fixed |
Removed from flow | Browser Viewport Window | Fixed header bars, floating chat widgets |
sticky |
In flow until scroll threshold | Scroll container parent | Sticky navigation bars, table header titles |
π Absolute Positioning Pattern: Parent relative + Child absolute
To position an absolute child element precisely inside a container, you must set position: relative on the parent container. Otherwise, the child positions relative to the entire <body> page!
/* Parent Container Anchor */
.card {
position: relative; /* Establishes positioning anchor boundary */
width: 300px;
}
/* Child Overlay Badge */
.card-badge {
position: absolute;
top: 10px;
right: 10px;
background-color: #ff3b30;
color: white;
padding: 4px 8px;
border-radius: 4px;
}
π z-index & Stacking Context
z-index controls layer overlap along the Z-axis (towards or away from the viewer). Higher z-index values render in front of lower values.
[!IMPORTANT] Stacking Context Rule:
z-indexworks ONLY on positioned elements (relative,absolute,fixed,sticky). A child element withz-index: 9999inside a parent with lower stacking context cannot appear above an element in a higher parent stacking context!
A new Stacking Context is created by:
- Positioned elements (
relative/absolute) withz-indexother thanauto. position: fixedorposition: sticky.- Elements with
opacityless than1. - Elements with
transform,filter, orperspectiveproperties applied.
π» HTML + CSS Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Positioning Demo</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header class="sticky-nav">
<span>Sticky Header Bar</span>
</header>
<div class="product-card">
<span class="badge">SALE</span>
<h2>Wireless Headphones</h2>
<p>Premium noise-cancelling headphones.</p>
</div>
</body>
</html>
/* style.css */
body {
font-family: Arial, sans-serif;
margin: 0;
background-color: #f4f6f8;
height: 200vh; /* Extra height to test sticky scroll */
padding-top: 20px;
}
.sticky-nav {
position: sticky;
top: 0;
z-index: 1000; /* Stays above body content during scroll */
background-color: #111827;
color: white;
padding: 15px 30px;
}
.product-card {
position: relative; /* Anchor for absolute badge */
width: 300px;
margin: 40px auto;
padding: 25px;
background: white;
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}
.badge {
position: absolute;
top: -10px;
right: -10px;
background-color: #ef4444;
color: white;
padding: 6px 12px;
border-radius: 20px;
font-size: 12px;
font-weight: bold;
}
π What You Will See
- The navigation bar stays pinned to the top of the browser window as you scroll down (
position: sticky). - The red βSALEβ badge floats at the top-right corner of the product card (
position: absoluterelative to.product-card).
π§ͺ Try It Yourself
- Remove
position: relativefrom.product-cardinstyle.css. - Notice how the βSALEβ badge jumps to the top-right corner of the entire
<body>page instead of staying attached to the card!
β οΈ Common Mistakes
- Forgetting
position: relativeon parent container: Causesposition: absolutechildren to align to the document root instead of the card. - Applying
z-indextoposition: staticelements:z-indexis ignored onposition: staticelements. - Using
position: fixedfor main layout columns: Destroys document layout flow and causes overlapping text.
π‘ Real-World Usage
Positioning powers essential UI patterns: fixed header bars, notification badges on shopping cart icons, modal popup windows, and floating support chat widgets.
π Related Topics
β Remember
- Use
position: relativeon parent containers to anchorposition: absolutechildren. position: fixedattaches to the browser window.position: stickytoggles pinning based on scroll position.z-indexrequires a non-static positioned element.
π§ Navigation
| β Previous | CSS Home | Next β |