简体   繁体   English

z索引和堆叠顺序-使孩子低于父母但高于叔叔

[英]z-index and stacking order - make child lower than parent but higher than uncle

Please see the code in jsbin 请参阅jsbin中的代码

Screenshot: 屏幕截图:

在此处输入图片说明

All I need is just to have blue on top, then white, then greens. 我所需要的只是在上面放蓝色,然后是白色,然后是绿色。 So ideally: 所以理想地:

在此处输入图片说明

I tried z-index , create stacking context... nothing worked. 我尝试了z-index ,创建了堆栈上下文...没有任何效果。

It might have something to do with negative margin in CSS 它可能与CSS中的负边距有关

I'm happy to change the HTML code or change the current CSS, as long as I can get the desired effect. 只要我能获得期望的效果,我很高兴更改HTML代码或更改当前的CSS。

 .left, .right { width: 200px; height: 60px; background-color: green; display: inline-block; } .bar { width: 20px; height: 60px; background-color: blue; display: inline-block; } .circle { height: 40px; width: 40px; background-color: white; border-radius: 50%; margin-left: -10px; margin-top: 10px; } 
 <div class="out"> <div class="left"></div> <div class="bar"> <div class="circle"></div> </div> <div class="right"></div> </div> 

Edit 编辑

I should have mentioned that my difficulty was mostly achieving the effect while keeping the current HTML setup (ie circle in bar). 我应该提到,我的困难主要是在保持当前HTML设置的同时(即以圆圈圈住)实现效果。 Turns out it doesn't seem possible, because 事实证明这似乎是不可能的,因为

  1. If no zindex on bar, can't make sure it's on top of circle 如果栏上没有zindex,则无法确保它位于圆圈顶部
  2. If set zindex on bar, then it creates new stacking context, then circle can't be on top of 2 greens. 如果将zindex设置为bar,则它将创建新的堆叠上下文,然后圆形不能位于2个果岭的顶部。 Because greens are on different stacking context 因为果岭位于不同的堆叠环境中

you can simplify this using just the div out with position + z-index 您可以只使用带有position + z-index的div out简化此操作

 .out { position: relative; width: 400px; height: 60px; background-color: green; } .bar { width: 20px; height: 60px; background-color: blue; display: inline-block; position: absolute; top: 0; left: 50%; z-index: 10 } .circle { height: 40px; width: 40px; background-color: white; border-radius: 50%; margin-left: -10px; margin-top: 10px; position: absolute; top: 0; left: 50%; z-index: 1 } 
 <div class="out"> <div class="circle"></div> <div class="bar"></div> </div> 

EDITED : edited my answer after reading more carefully :) sorry about that 编辑:仔细阅读后编辑了我的答案:)对此感到抱歉

see here > jsFiddle 看到这里> jsFiddle

or snippet below : 或以下代码段:

 .left, .right { width: 200px; height: 60px; background-color: green; display: inline-block; position:relative; z-index:1; } .bar { width: 20px; height: 60px; background-color: blue; display: inline-block; z-index:6; position:relative; } .circle { height: 40px; width: 40px; background-color: white; border-radius: 50%; top: 10px; position:absolute; left:0; right:0; margin:0 auto; z-index:5; } .out {width:420px;position:relative;} 
 <div class="out"> <div class="left"></div><div class="bar"></div><div class="circle"></div><div class="right"></div> </div> 

OR if you don't want different bg color for .left and .right just use one big div .out and position the bar and circle on top of it : 或者,如果您不希望.left.right使用不同的bg颜色, .right只需使用一个大div .out并将barcircle放置在其顶部即可:

 .out { position: relative; width: 420px; height: 60px; background-color: green; } .bar { width: 20px; height: 100%; background-color: blue; position: absolute; left: 0; right:0; margin:0 auto; z-index: 2 } .circle { height: 40px; width: 40px; background-color: white; border-radius: 50%; position: absolute; top: 10px; left: 0; right:0; margin:0 auto; z-index: 1 } 
 <div class="out"> <div class="bar"></div> <div class="circle"></div> </div> 

What if we just interchange .bar as child element of .circle . 如果我们只是将.bar替换为.circle子元素.circle And try as below, 然后尝试如下

 .left, .right { width: 200px; height: 60px; background-color: green; display: inline-block; } .bar { width: 20px; height: 60px; background-color: blue; margin:-10px 10px; } .circle { height: 40px; width: 40px; background-color: white; border-radius: 50%; display:inline-block; position:absolute; margin:10px -20px; } 
  <div class="out"> <div class="left"></div> <div class="circle"><div class="bar"></div></div> <div class="right"></div> </div> 

Use transform. 使用变换。

https://jsbin.com/geconefine/1/edit?html,css,output https://jsbin.com/geconefine/1/edit?html,css,输出

.out{
  position: relative;
  z-index: 0;
}

.left, .right {
  width: 200px;
  height: 60px;
  background-color: green;
  display: inline-block;
  position: relative;
  z-index: -2;
}

.bar {
  width: 20px;
  height: 60px;
  background-color: blue;
  display: inline-block;
  position: relative;
}
.circle {
  height: 40px;
  width: 40px;
  background-color: white;
  border-radius: 50%;
  transform: translateX(-10px);
  margin-top: 10px;
  position: relative;
  z-index: -1;
}

You could even further simplify your markup and utilize a pseudo selector instead of wrestling with stacking order, and order elements naturally. 您甚至可以进一步简化标记,并使用pseudo selector来代替堆叠顺序和自然排序元素。

 .out { width: 400px; padding: 10px 0; background: green; } .circle { height: 40px; width: 40px; background-color: white; border-radius: 100%; display: block; margin: 0 auto; position: relative; } .circle:after { content: ''; width: 20px; height: 60px; background-color: blue; display: block; margin: 0 auto; position: absolute; top: -10px; left: 10px; } 
 <div class="out"> <div class="left"></div> <div class="circle"></div> <div class="right"></div> </div> 

You need a position before z-index will do anything. 在z-index执行任何操作之前,您需要一个职位。 Since I don't see any applied in your current css that might be your issue. 由于我看不到您当前的CSS中有任何应用,可能是您遇到的问题。

.left, .right{
  position: relative;
  z-index: 1;
}
.circle{
  position: relative;
  z-index: 4;
}
.bar{
  position: relative;
  z-index: 5;
}

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

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