简体   繁体   English

Z-index 不适用于绝对定位元素

[英]Z-index not working with absolute positioned elements

I am trying to accomplish the design that's shown in the image.我正在尝试完成图中所示的设计。 However, z-index is not working on the absolute elements when I am trying to put them behind the cards.但是,当我试图将它们放在卡片后面时,z-index 对绝对元素不起作用。 What am I doing wrong here?我在这里做错了什么? Does it have to do with the flex parent or something?它与 flex 父级有关吗?

FYI - I am using styled components for styling仅供参考 - 我正在使用样式化组件进行样式化

设计实例


设计不工作

const CardWrapper = styled.div`
  display: flex;
  position: relative;
  z-index: 0;
`;

const CardWrapLeftIcon = styled.div`
  position: absolute;
  top: -80px;
  left: -230px;
  z-index: 1;
`;

const CardWrapRightIcon = styled.div`
  position: absolute;
  top: -220px;
  right: -300px;
  z-index: 1;
`;

const Card = styled.div`
  z-index: 2;
  position: relative;
`;
<CardWrapper>
  <CardWrapLeftIcon>
    <img src="/images/image.svg" alt="Card wrap left icon" height={320} width={460} />
  </CardWrapLeftIcon>
  <CardWrapRightIcon>
    <img src="/images/image.svg" alt="Card wrap right icon" height={480} width={421} />
  </CardWrapRightIcon>

  <Card>
    <h4>Card 1</h4>
  </Card>

  <Card>
    <h4>Card 2</h4>
  </Card>

  <Card>
    <h4>Card 3</h4>
  </Card>
</CardWrapper>

The problem here should be that it's not in the same z-index context (see more in this article .) You can wrap all the absolute elements in a div, and then make that parallel to the div containing the cards.这里的问题应该是它不在同一个 z-index 上下文中(请参阅本文中的更多信息。)您可以将所有绝对元素包装在一个 div 中,然后使其与包含卡片的 div 平行。 Set the z-index of that div to be lower than the div containing the cards.将该 div 的 z-index 设置为低于包含卡片的 div。

ie IE

<div style="z-index:1;">
   Cards.
</div>
<div style="z-index:0; position: absolute;">
   <img style="position: absolute";>
   ...
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM