P粉1037395662023-08-31 10:34:27
下次請分享你的程式碼而不是截圖,無論如何,這裡是一個不使用z-index的範例程式碼
HTML
<div class="container"> <div class="your-image"> </div> <div class="your-boxes"> </div> </div>
CSS
.container { display: grid; grid-template-rows: 1 / 1; grid-template-columns: 1 / 1; justify-items: center; align-items: center } .your-image { background-color: red; width: 250px; height: 250px; grid-area: 1 / 1 / 1 / 1; } .your-boxes { background-color: green; width: 150px; height: 150px; grid-area: 1 / 1 / 1 / 1; justify-self: center; }
基本上,你創建了一個1x1的網格,將兩個div重疊在同一列和同一行上。
https://codepen.io/ChrisCoder9000/pen/NWMJdBo
#