要解决“不透明度的子元素不能出现在具有 z-index 的父元素的兄弟元素之下”的问题,可以使用以下代码示例:
HTML代码:
CSS代码:
.parent {
position: relative;
z-index: 1;
}
.sibling {
position: relative;
z-index: 2;
}
.child {
position: relative;
z-index: 3;
opacity: 0.5;
}
在上面的示例中,父元素 .parent
具有 z-index: 1
,兄弟元素 .sibling
具有 z-index: 2
,子元素 .child
具有 z-index: 3
和 opacity: 0.5
。
通过将 .parent
元素的 position
设置为 relative
,并为 .parent
、.sibling
和 .child
元素分别设置不同的 z-index
值,我们可以确保 .child
元素不会出现在 .sibling
元素之下。
请注意,只有当元素的 position
值设置为 relative
、absolute
或 fixed
时,z-index
才能生效。