我有代码如下: <div style="width: 100%; height: 100%;display: flex;justify-content: center;align-items: center;"> <div style="width:100px;height: 100px; background: rgba(7, 187, 255, 0.22);z-index: 3;"></div> <div style="width:200px;height: 200px; background: #0B5588;opacity: 0.5;z-index: 2;"></div> <div style="width:300px;height: 300px; background: #0B5588;opacity: 0.2;z-index: 1;"></div> </div> 如何让三个子div重叠居中
时间: 2024-02-21 20:57:17 浏览: 22
如果您希望三个子 div 重叠在一起并居中显示,您可以使用绝对定位来实现。将父 div 设置为相对定位,然后将每个子 div 都设置为绝对定位,并将它们的 left 和 top 值设置为相同的值,以使它们重叠。以下是修改后的代码:
```html
<div style="position: relative; width: 100%; height: 100%; display: flex; justify-content: center; align-items: center;">
<div style="position: absolute; left: 0; top: 0; width: 100px; height: 100px; background: rgba(7, 187, 255, 0.22); z-index: 3;"></div>
<div style="position: absolute; left: 0; top: 0; width: 200px; height: 200px; background: #0B5588; opacity: 0.5; z-index: 2;"></div>
<div style="position: absolute; left: 0; top: 0; width: 300px; height: 300px; background: #0B5588; opacity: 0.2; z-index: 1;"></div>
</div>
```
请注意,这种方法可能会导致子元素遮挡彼此,因此您需要根据您的需求来选择最适合您的方法。