HTML & CSS - Mastery / CSS Grid System

Grid Item Z-Index & Overlapping

🤔 Z Index Overlapping

In CSS Grid, items can overlap when they're placed in the same grid cell or span across the same area. When this happens, the z-index property is used to control which item appears on top.


📌 When Do Grid Items Overlap?

  • When two or more items are explicitly positioned into the same cell or overlapping grid areas using grid-row / grid-column.

  • When you use negative margins, absolute positioning, or overlapping content intentionally.


🧭 z-index in Grid Items

  • By default, grid items follow the HTML source order to stack.

  • To control the stack order manually, use z-index on items with position set to anything other than static (relative, absolute, fixed, or sticky).


✅ Example: Overlapping Grid Items with z-index

🔍 Explanation:

  • Both items overlap in the middle (grid-column: 2 / 3).

  • item2 has a higher z-index, so it appears on top of item1.


⚠️ Notes:

  • z-index only works if position is not static.

  • Overlapping is usually not common in standard grid layouts, but can be powerful for complex designs (e.g., cards, overlays, layer effects).