Chapter 24: Positioning & Z-Index

Positioning + z-index

These two properties together let you take elements out of the normal document flow and place them exactly where you want, even stacking them on top of each other like layers in Photoshop.

But they are also responsible for 90% of “why is this element hiding behind something?”, “why is my fixed navbar broken?”, “why is this modal not centered?” frustrations.

So let’s go very slowly, very clearly, with lots of visuals in words and a real, playable example.

1. The Five Positioning Values (What They Actually Do)

Value Takes element out of normal flow? Respects document flow of others? Reference point for top/right/bottom/left Most common real-world uses
static No Yes — (ignored) Default — almost everything starts here
relative No Yes Its original position Small nudges, creating space for absolute children
absolute Yes No Nearest positioned ancestor (or body) Overlays, tooltips, badges, modals, dropdowns
fixed Yes No Viewport (browser window) Sticky headers, floating action buttons, chat widgets
sticky No (until scroll threshold) Yes (until stuck) Its flow position until it sticks Sticky table headers, section titles, sidebars

Key sentence to memorize:

Only elements with position ≠ static respond to top, right, bottom, left and create a new stacking context for their absolute/fixed children.

2. z-index – Who Sits on Top?

z-index only works on elements that have:

  • position: relative | absolute | fixed | sticky
  • (or position: static with some rare transform/filter tricks — ignore for now)

Higher z-index = sits on top Default z-index = auto (usually behaves like 0)

Stacking order rules (when z-index is equal):

  1. Later in HTML = on top
  2. Stacking contexts (more on this later)

3. Real, Hands-On Example – Play with This Right Now

index.html

HTML

style.css

CSS

What You Should Do Right Now

  1. Open the file with Live Server
  2. Scroll down → watch sticky header stick at top
  3. See fixed purple button always stay bottom-right
  4. Notice how absolute child stays relative to its blue relative parent
  5. Look at stacking squares → higher z-index sits on top
  6. Try removing position: relative from .relative → absolute child jumps to body

Quick Mastery Checklist – Positioning & z-index

  • static = normal flow (default)
  • relative = nudge + stacking context for children
  • absolute = taken out of flow, positioned relative to nearest positioned ancestor
  • fixed = glued to viewport (ignores scroll)
  • sticky = normal until scroll threshold, then fixed-like
  • z-index only works on non-static positioned elements
  • Always create stacking context with position + z-index or transform, filter, opacity < 1, etc.

How does it feel when you play with the demo? Any element behaving unexpectedly? Want to see a modal or tooltip example next?

Next possible lessons (just tell me):

  • “real-world positioning: modal, tooltip, badge, dropdown”
  • “z-index stacking context deep dive”
  • “sticky sidebar vs fixed header”
  • “common positioning bugs & fixes”
  • “Flexbox + positioning combination patterns”

You’ve just unlocked one of the most powerful tools in CSS. Chai khatam? Fresh cup le aao — let’s keep going! 🚀 😄

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *