Course contentsShow
← Web Development
Lesson 236 of 2,9824. CSS Layout FoundationsFree lesson

Practical Stacking Patterns

Common scenarios: modals over content, dropdowns, tooltips, and managing layers in complex layouts.

Practical Stacking Patterns

You'll learn: How to confidently manage layers in real-world UI components like modals, dropdowns, and tooltips using stacking contexts and z-index.


Now that you understand stacking contexts and z-index, let's apply that knowledge to common design patterns you'll encounter constantly.

Modals Over Content

Modals (popup dialogs) need to appear above everything else. Create a new stacking context with position: fixed and a high z-index (commonly z-index: 1000). This ensures the modal breaks free from parent stacking contexts and sits on top of your main content.

Dropdowns and Menus

Dropdown menus must appear above adjacent content. Use position: absolute with a modest z-index (like z-index: 100). Since dropdowns are usually positioned relative to their parent, they only need to win locally—not globally like modals.

Tooltips

Tooltips need to overlay nearby elements but stay "humble." Use position: absolute with z-index: 10 or so. They don't need sky-high values because they're contextual helpers, not page-blocking overlays.

Managing Complex Layouts

Establish a z-index scale early: Reserve ranges like 1-10 for minor overlays, 100-200 for dropdowns, and 1000+ for modals. This prevents "z-index wars" where you keep adding zeros hoping something finally wins.

Think in layers, not numbers: Don't randomly assign z-index: 999999. Instead, understand which stacking context your element lives in (remember parent-child boundaries from earlier lessons). Sometimes the solution isn't a higher z-index—it's creating a new stacking context with isolation: isolate.


Key Takeaway:
Use predictable z-index ranges for different UI layers (tooltips < dropdowns < modals), and remember that stacking contexts define boundaries—higher numbers don't always win across context borders.