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