How can I overlap two elements using CSS Grid?
Quick answer
Set the parent to display: grid and place both children in the same grid cell, or give them row and column ranges that intersect. Use alignment and margins to position the items within the shared area, and set z-index when you need a particular stacking order. Give the container dimensions that suit its content so the overlap has a usable space. [S1][S2]
The question, the details, the next step.
A practical checklist
Set
display: gridon the parent element.Place both children in the same cell or in intersecting row and column ranges.
Choose container dimensions that suit the content.
Use alignment and margins to position items within the shared area.
Set
z-indexwhen you want a particular stacking order.
A few more useful answers
Do both elements have to use exactly the same grid area?
Why is my overlap appearing in an unexpected place?
Check the row and column start and end lines first. Incorrect line choices can create unintended overlap; also confirm the shared area’s size and each item’s alignment. [S2]
How do I choose which item is on top?
Set z-index on the item that should appear above the other when the default stacking order is not what you want. [S1]
Sources
References used for this guide.
- [S1] Basic concepts of grid layout developer.mozilla.org · Checked
- [S2] Grid layout using line-based placement developer.mozilla.org · Checked
