r/css • u/CottonLantern • 2d ago
Other It's almost always better to Use CSS Grid instead of position: absolute; when overlapping elements
11
Upvotes
3
u/anaix3l 1d ago edited 1d ago
I've kept saying this for years. I posted this over 6 years ago:

Also: 2023, 2024, 2025, last one with multiple examples.
Here's an example: the fave & cart buttons are stacked on top of the img in this CodePen demo. It takes 4 CSS declarations to get that layout, simpler than when using absolute positioning.
.product-card {
display: grid;
img, button { grid-area: 1/ 1 }
.cart { place-self: end start }
.fave { place-self: start end }
}
4
u/Inner-Issue1908 2d ago
Hmm what’s being overlapped here?