【问题标题】:CSS ::before pseudo-element not respecting z-index stacking context [duplicate]CSS ::before伪元素不尊重z-index堆叠上下文[重复]
【发布时间】:2021-11-28 15:16:58
【问题描述】:

为什么我的::before 伪元素不在它的父元素后面?我正在创建一个新的堆叠上下文并添加一个 -1 的 z-index

codepen

  .panda {
    position: relative;
    border: 1.5vmin solid black;
    width: 45vmin;
    height: 45vmin;
    border-radius: 50%;
    background: #fff;
    z-index: 2;

    &:before {
      content: '';
      position: absolute;
      z-index: -1;
      width: 15vmin;
      height: 15vmin;
      background: black;
      border-radius: 50%;
      top: -4vmin;
      left: -2.1vmin;
      box-shadow: 30vmin 0 #000;
    }
  }

【问题讨论】:

  • 你熊猫的眼睛和鼻子很好看
  • 谢谢,希望我能把耳朵放在他脑后。
  • 你能告诉我这条线border-radius: 50% 50% 48% 52% / 21% 21% 79% 79%;。哪个部分确实创造了耳朵
  • 创建耳朵的部分是.panda:before
  • 是的,发现解决方案是在 CSS 中使用 ::after(你了解 CSS),但你能告诉我 border-radius: 50% 50% 48% 52% / 21% 21% 79% 79%;

标签: css pseudo-element


【解决方案1】:

::after 将其内容插入它所应用的标签的末尾之前,但仍在标签内,z-index 不能低于标签的 z-index,因此您的方法将不起作用。但是您可以对父标签执行::after,在您的情况下,它具有类'circle'。由于插入的内容是相对于包装标签的,因此您应该调整与该标签相关的竞争位置。

查看代码笔示例:

Codepen Example

在此处查看 sn-p 示例:

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

.canvas {
    background: #573d0e;
    height: 100vh;
    width: 100vw;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
  }

  .circle {
    width: 70vmin;
    height: 70vmin;
    background: #ffffff;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    position: relative;
  }

  .circle:after {
      content: '';
      position: absolute;
      z-index: 1;
      width: 15vmin;
      height: 15vmin;
      background: black;
      border-radius: 50%;
      top: 10vmin;
      left: 12vmin;
      box-shadow: 30vmin 0 #000;
    }

.panda {
    position: relative;
    border: 1.5vmin solid black;
    width: 45vmin;
    height: 45vmin;
    border-radius: 50%;
    background: #fff;
    z-index: 2;

  }

  .eyes {
      position: relative;

    > * {
        position: absolute;
        width: 11vmin;
        height: 15vmin;
        background: #000;
        border-radius: 70px / 100px;
        left: 8.7vmin;
        top: 9vmin;
        transform: rotate(12deg);

        &:before,
        &:after {
          content: '';
          position: absolute;
        }

        &:before {
            width: 4vmin;
            height: 6vmin;
            background: white;
            border-radius: 76px / 100px;
            left: 5vmin;
            top: 3.2vmin;
            transform: rotate(348deg);
        }

        &:after {
          width: 2vmin;
          height: 3vmin;
          background: black;
          border-radius: 76px / 100px;
          left: 6.3vmin;
          top: 5vmin;
          transform: rotate(348deg);
        }
    }

    :last-child {
        transform: scale(-1, 1) rotate(12deg);
        left: 22.3vmin;
    }
  }

  .snout {
        position: absolute;
        width: 25vmin;
        height: 18vmin;
        top: 23vmin;
        left: 8.5vmin;
        bottom: 5vmin;
        background: #fff;
        border-radius: 50%;
        border: 1.5vmin solid black;

        &:before {
            content: '';
            position: absolute;
            width: 1.5vmin;
            height: 5vmin;
            left: 10vmin;
            top: 7vmin;
            background: black;
        }
  }

  .nose {
    position: absolute;
    width: 10vmin;
    height: 7vmin;
    left: 5.7vmin;
    top: 0.5vmin;
    background: black;
    border-radius: 50% 50% 48% 52% / 21% 21% 79% 79%;
  }

  .mouth {
        position: absolute;
        width: 9.6vmin;
        height: 5vmin;
        border-radius: 50%;
        border-bottom: 1.5vmin solid black;
        bottom: 1.6vmin;
        left: 6vmin;
  }
    <div class="canvas">
        <div class="circle">
            <div class="panda">
                <div class="eyes">
                    <div></div>
                    <div></div>
                </div>
                <div class="snout">
                    <div class="nose"></div>
                    <div class="mouth"></div>
                </div>
            </div>
        </div>
    </div>

【讨论】:

  • 谢谢。所以真的没有办法从直接父元素创建新的堆叠上下文?
  • 我认为在绝对定位的情况下没有办法......不是 100% 肯定,它可能适用于某些浏览器,但不会是一个稳定的解决方案
【解决方案2】:

.panda {
    position: relative;
    border: 1.5vmin solid black;
    width: 45vmin;
    height: 45vmin;
    border-radius: 50%;
    background: #fff;
    
    
  }
  .panda:after {
      content: '';
      position: absolute;
      z-index: -1;
      width: 15vmin;
      height: 15vmin;
      background: black;
      border-radius: 50%;
      top: -4vmin;
      left: -2.1vmin;
      box-shadow: 30vmin 0 #000;
    }
&lt;div class="panda"&gt;&lt;/div&gt;

【讨论】:

  • 很好,我只是这样做,但在 SCSS 中找不到方法(或 SASS 不知道)
  • 看起来它在这里的代码编辑器中工作,但不是在 codepen 中? codepen.io/afewgoodben/pen/OJjLaWg
  • 将 z-index:1 添加到您的 circle 类并从 panda 中删除 z-index-2
  • 并用绿色勾号表示这是正确答案
【解决方案3】:

元素总是使用父元素的 z-index。如果父 div 具有 z-index,则所有子 div 不能设置在较低的 z-index 上。最小 z-index 始终是父级。

对于你的熊猫,最好的解决方案是用“.panda”为耳朵创建一个特殊的 div:

CSS:

.ears{
    position: absolute;
  z-index: 1;
  width: 15vmin;
  height: 15vmin;
  background: black;
  border-radius: 50%;
  top: 8vmin;
  left: 12vmin;
  box-shadow: 30vmin 0 #000;
}

HTML:

   <div class="canvas">
        <div class="circle">
          <div class="ears"></div>
            <div class="panda">
                <div class="eyes">
                    <div></div>
                    <div></div>
                </div>
                <div class="snout">
                    <div class="nose"></div>
                    <div class="mouth"></div>
                </div>
            </div>
        </div>
    </div>

您可以在 .circle 上修复耳朵。实例:https://codepen.io/camillewemajin/pen/ExvYOBV

【讨论】:

  • 谢谢,但您应该能够通过将伪元素设置为 position: absolute; 来创建 :before 的新堆叠上下文;
  • 你是对的,将 .circle 设置为相对位置!现场示例:codepen.io/camillewemajin/pen/ExvYOBV
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-30
  • 2014-08-22
相关资源
最近更新 更多