Display & CSS Positioning (Relative, Absolute, Fixed, Sticky)
1. Simple Definition
Display property decide karti hai ki element flow me kaise behave karega (line break lega ya usi line me rahega). Aur Position property decide karti hai ki element normal page flow me rahega ya fir screen par kisi specific coordinate (top, left, right, bottom) par pin ya lock kiya jayega.
2. Real-Life Analogy
• Static: Normal flow me line me khada insaan.
• Relative: Apni normal jagah se thoda 10 step aage ya peeche khisakna, par purani seat reserved rakhna.
• Absolute: Room ke reference frame (relative parent) ke kisi specific corner me magnet se chipak jana.
• Fixed: Car ki windscreen par laga GPS screen — car kitni bhi door chale, GPS hamesha driver ki ankhon ke samne chipka rehta hai (floating navigation bar).
• Sticky: Shuru me page ke sath scroll hota hai, par jab top par pahunchta hai toh wahan permanently chipak jata hai (sticky header).
3. Why Do We Need It?
Website ke sticky headers, modal overlays, card ke upar floating "SALE" discount badges, aur floating WhatsApp chat buttons position properties se hi bante hain.
4. Syntax & Positioning Types
/* 1. Display Types */
.block { display: block; } /* New line, takes 100% width */
.inline { display: inline; } /* Same line, no width/height allowed */
.inline-block{ display: inline-block; } /* Same line, but width/height allowed */
.hidden { display: none; } /* Removed from page layout */
/* 2. Position Golden Combo (Relative Parent + Absolute Child) */
.parent-card {
position: relative; /* Reference frame ban gaya */
}
.badge-tag {
position: absolute;
top: 10px;
right: 10px; /* Parent ke top-right me lock */
}
/* 3. Sticky Navbar */
.site-header {
position: sticky;
top: 0; /* Top par aate hi chipak jayega */
z-index: 100;
}
5. Basic Example (Relative + Absolute Badge)
<div class="product-card">
<span class="discount-badge">50% OFF</span>
<h3>Wireless Earbuds</h3>
<p>Price: ₹1,499</p>
</div>
<style>
.product-card {
position: relative; /* Anchor for absolute child */
background: #ffffff;
border: 1px solid #cbd5e1;
border-radius: 12px;
padding: 24px;
max-width: 280px;
}
.discount-badge {
position: absolute; /* Floats at top-right */
top: -10px;
right: -10px;
background: #ef4444;
color: white;
font-size: 12px;
font-weight: bold;
padding: 4px 10px;
border-radius: 999px;
}
</style>
6. Output / Expected Result
Wireless Earbuds
₹1,499
7. Code Explanation
position: relative: Element normal flow me rehta hai, lekin ye apne kisi bhiposition: absolutechild ke liye boundary/anchor ban jata hai.position: absolute: Normal flow se bahar nikal jata hai aur apne sabse kareebi positioned parent ke hisaab se place hota hai.position: fixed: Browser window (viewport) ke hisab se freeze hota hai. Page scroll karne par bhi nahi hilta.position: sticky: Relative aur Fixed ka hybrid — scrolling threshold (e.g.top: 0) touch hone tak normal rehta hai, fir freeze ho jata hai.
8. Real-World Example
Instagram reels ka like/comment button bar (sticky right column), Flipkart cart notification badge, aur Swiggy ka sticky category filter bar.
9. Common Mistakes
Agar aap child ko position: absolute; top: 0; right: 0; dete hain lekin parent ko position: relative nahi dete, toh child card ke top-right me aane ke bajaye poore browser window (viewport) ke top-right me chala jata hai!
position: sticky; tabhi kaam karta hai jab aap top: 0; jaisa threshold specify karein aur kisi parent element par overflow: hidden; na laga ho!
10. Best Practices
- Floating badges ke liye hamesha: Parent = relative, Child = absolute formula use karein.
- Header ko sticky banane ke liye
position: sticky; top: 0; z-index: 100;use karein.
11. Try It Yourself
Playground me ek card ke top-right corner par "HOT DEAL" badge absolute position karke dekhein.
12. Challenge
Ek Floating WhatsApp Action Button banayein jo screen ke bottom-right corner me position: fixed; bottom: 20px; right: 20px; par freeze rahe.
13. Interview Questions
Answer: display: none; element ko DOM me rakhta hai lekin render tree se poori tarah hata deta hai (koi space nahi leta). visibility: hidden; element ko invisible karta hai lekin uski physical jagah (space) page layout par reserved rehti hai.
14. Quick Revision
display: block/inline/inline-block/none.- Relative Parent + Absolute Child = Floating Badge.
position: sticky; top: 0;= Modern sticky navbars.
15. FAQ
Q1. inline elements par width aur height kyun kaam nahi karti?
display: inline (jaise <span>) sentence ke beech flow karne ke liye hote hain. Dimensions dene ke liye display: inline-block ya block karna padta hai.
Q2. Sticky element parent container ke bahar kyun nahi chipakta?
Sticky element sirf apne direct parent container ke boundary ke andar hi stick karta hai. Jab parent viewport se nikal jata hai, sticky element bhi nikal jata hai.
Q3. Fixed element mobile par zoom hone par kaisa behave karta hai?
Viewport ke sath bind rehta hai, isliye screen par wahi rehta hai chahe user zoom kare.
Q4. inset property kya karti hai?
inset: 0; shorthand hai top: 0; right: 0; bottom: 0; left: 0; ka (full overlay banane ke liye best).
Q5. position: static kya hota hai?
HTML ka default position behavior jahan top, left, z-index ka koi asar nahi hota.
Display & CSS Positioning (Relative, Absolute, Fixed, Sticky)
1. Simple Definition
The display property dictates an element's formatting context (e.g. block, inline, inline-block, none). CSS Positioning (relative, absolute, fixed, sticky) allows elements to be removed from normal document flow and pinned or offset precisely using top, right, bottom, and left.
2. Real-Life Analogy
• Static (Normal Flow): Paragraphs printed directly on paper in consecutive lines.
• Relative: Nudging a printed photo 10px to the right while reserving its original empty slot on paper.
• Absolute: A pushpin stuck directly through a cutout photo onto a specific corkboard frame.
• Fixed: A plastic sticker glued to the inside of your sunglasses — wherever you look, it stays right in view!
• Sticky: A sticky note scrolling naturally until hitting the top desk edge, where it clings in place.
3. CSS Position Types
| Position | In Normal Flow? | Positioned Relative To | Common Application |
|---|---|---|---|
static |
Yes | Normal document flow (Offsets have no effect) | Default behavior |
relative |
Yes | Its own original default position | Parent anchor for absolute children |
absolute |
No (Removed) | Nearest ancestor with position != static | Badges, close buttons, modal icons |
fixed |
No (Removed) | Browser viewport (Screen window) | Sticky top navbars, scroll-to-top buttons |
sticky |
Hybrid | Normal flow until scroll threshold is reached | Sticky section headers, table headers |
4. The Classic Absolute Badge Pattern
<!-- Parent MUST have position: relative -->
<div class="product-card" style="position: relative;">
<span class="sale-badge" style="position: absolute; top: 12px; right: 12px;">
50% OFF
</span>
<h3>Sneakers</h3>
</div>
5. Common Mistakes
When an absolute child is placed inside a parent lacking position: relative, the child escapes all the way up to the <body> tag and pins to the browser window!
6. Best Practices
- Always establish
position: relativeon the containing card before positioning absolute sub-elements. - Use
position: sticky; top: 0;for persistent headers instead of JavaScript scroll listeners. - For sticky to function, none of the ancestor parents can have
overflow: hiddenenabled.
7. Practice Exercise
Build a floating chat action button using position: fixed; bottom: 2rem; right: 2rem; that remains visible regardless of page scroll position.
8. Interview Questions
Answer: The most common reasons are: 1) Missing a directional threshold like top: 0; 2) An ancestor having overflow: hidden or overflow: auto; or 3) The sticky element's parent container has the exact same height as the element itself.
9. Summary / Cheat Card
relativekeeps its space and anchors absolute children.absoluteremoves element from flow, relative to closest positioned parent.fixedlocks relative to the browser viewport.stickytransitions from static to fixed during scroll.
10. FAQ
Q1. What is the difference between visibility: hidden and display: none?
display: none removes the element completely from document flow (takes zero space). visibility: hidden hides the element visually, but preserves its invisible blank layout space.
Q2. Can z-index be used on position: static elements?
No, z-index only takes effect on positioned elements (relative, absolute, fixed, sticky) or flex/grid children.