📡 You're offline — showing cached content
New version available!
Quick Access

CSS Positioning: Static, Relative, Absolute, Fixed, Sticky

Learn all five CSS position values with practical examples — dropdowns, tooltips, sticky headers, and fixed buttons.

EzyCoders Admin August 6, 2026 7 min read 7 views
CSS Positioning: Static, Relative, Absolute, Fixed, Sticky
Share: Twitter LinkedIn WhatsApp

What is it?

CSS positioning removes an element from normal document flow and places it at a precise location. The five position values each create different placement behaviour relative to different reference points.

Why does it matter?

Dropdowns, modals, tooltips, sticky headers, and fixed sidebars all require CSS positioning. Without understanding containing blocks and z-index, elements appear in the wrong place or behind other content.

Learn all five CSS position values with practical examples — dropdowns, tooltips, sticky headers, and fixed buttons.

Real-World Use Cases

  • 🗂️ Dropdown menus - position:absolute on dropdown, relative on the menu item — it drops exactly below its parent.
  • 💬 Tooltips on hover - position:absolute tooltip positioned relative to its parent — appears at hover with z-index above other content.
  • 📌 Sticky table headers - position:sticky top:0 on th elements makes headers stick to viewport as user scrolls the table.
  • 🛒 Fixed cart icon - position:fixed bottom:20px right:20px keeps the cart in the viewport corner regardless of scroll.

Position Values

/* static: normal flow (default) */
/* relative: stays in flow, can be offset, creates containing block */
.moved { position: relative; top: 10px; left: 20px; }

/* absolute: removed from flow, positioned to nearest positioned ancestor */
.dropdown {
    position: absolute;
    top: 100%;   /* directly below parent */
    left: 0;
    z-index: 100;
}

/* fixed: positioned to the viewport */
.floating-btn { position: fixed; bottom: 24px; right: 24px; z-index: 1000; }

/* sticky: relative until threshold, then fixed */
.table-header th { position: sticky; top: 0; z-index: 10; }

Dropdown Menu Pattern

.nav-item { position: relative; display: inline-block; }

.dropdown-menu {
    position: absolute;
    top: 100%;    /* below the nav-item */
    left: 0;
    z-index: 100;
    display: none;
    background: white;
    border: 1px solid #e2e8f0;
    border-radius: 8px;
    box-shadow: 0 10px 30px rgba(0,0,0,0.1);
}

/* Show on hover */
.nav-item:hover .dropdown-menu { display: block; }

Z-index and Stacking Context

/* z-index only works on positioned elements (not static) */
.modal-overlay { position: fixed; z-index: 1000; }
.modal-content { position: relative; z-index: 1001; }
.tooltip       { position: absolute; z-index: 500; }
.sticky-header { position: sticky; z-index: 50; }

/* STACKING CONTEXT GOTCHA:
   A z-index:999 child INSIDE a z-index:1 parent
   appears BEHIND z-index:2 elements outside that parent.
   transform, opacity<1, will-change also create new contexts. */

Q: Why is my z-index not working?

Two reasons: (1) z-index only works on positioned elements (not static). (2) Stacking context: if your element is inside a parent with a lower z-index that creates a stacking context, the child cannot appear above elements outside that context. Check parent elements for transform, opacity less than 1, or will-change — these also create stacking contexts.

🧩
Test Your Knowledge
50 questions · Takes ~1500 seconds

Answer these questions to confirm you understood the article. Instant feedback on every answer.

Q1
What is the primary purpose of this topic in Career development?
Q2
In a Career technical interview, what do interviewers test?
Q3
What is the time/space complexity consideration for this Career topic?
Q4
When should a Career developer apply this technique?
Q5
What is the most common mistake when using this Career pattern?
Q6
Best practice for Career concept #6?
Q7
Best practice for Career concept #7?
Q8
Best practice for Career concept #8?
Q9
Best practice for Career concept #9?
Q10
Best practice for Career concept #10?
Q11
Best practice for Career concept #11?
Q12
Best practice for Career concept #12?
Q13
Best practice for Career concept #13?
Q14
Best practice for Career concept #14?
Q15
Best practice for Career concept #15?
Q16
Best practice for Career concept #16?
Q17
Best practice for Career concept #17?
Q18
Best practice for Career concept #18?
Q19
Best practice for Career concept #19?
Q20
Best practice for Career concept #20?
Q21
Best practice for Career concept #21?
Q22
Best practice for Career concept #22?
Q23
Best practice for Career concept #23?
Q24
Best practice for Career concept #24?
Q25
Best practice for Career concept #25?
Q26
Best practice for Career concept #26?
Q27
Best practice for Career concept #27?
Q28
Best practice for Career concept #28?
Q29
Best practice for Career concept #29?
Q30
Best practice for Career concept #30?
Q31
Best practice for Career concept #31?
Q32
Best practice for Career concept #32?
Q33
Best practice for Career concept #33?
Q34
Best practice for Career concept #34?
Q35
Best practice for Career concept #35?
Q36
Best practice for Career concept #36?
Q37
Best practice for Career concept #37?
Q38
Best practice for Career concept #38?
Q39
Best practice for Career concept #39?
Q40
Best practice for Career concept #40?
Q41
Best practice for Career concept #41?
Q42
Best practice for Career concept #42?
Q43
Best practice for Career concept #43?
Q44
Best practice for Career concept #44?
Q45
Best practice for Career concept #45?
Q46
Best practice for Career concept #46?
Q47
Best practice for Career concept #47?
Q48
Best practice for Career concept #48?
Q49
Best practice for Career concept #49?
Q50
Best practice for Career concept #50?
EzyCoders Admin
Written by
EzyCoders Admin

Team Lead and Full-Stack Developer with experience in PHP, JavaScript, SQL, DSA, and System Design. Passionate about software engineering, scalable web technologies, and helping developers prepare for coding interviews and tech careers through practical tutorials and professional guidance.

Comments (0)

No comments yet. Be the first!

Leave a Comment