简体   繁体   English

z-index不是主导吗?

[英]Is z-index not a dominant?

What I'm trying to say is that WHY is z-index ignored when in this situation? 我要说的是在这种情况下为什么会忽略z-index

 #IAmGreater{ position:fixed; z-index: 999!important; background-color: green; height: 50px; width: 50px; top: 0; left: 0; } #parent{ background-color: blue; height: 200px; width: 200px; position: fixed; margin: 50px; } #IAmDown{ position: absolute; z-index: 0; background-color:red; height:100px; width: 100px; } 
 <div id="parent"> <span id="IAmGreater"></span> </div> <div id="IAmDown"> </div> 

#IAmGreater can only be on top of #IAmDown if I set the IAmDown to z-index: -1 but in this case the parent div will also be on top of IAmDown and I don't want that. #IAmGreater只能之上#IAmDown如果我设置IAmDownz-index: -1 ,但在这种情况下, parent的div也将顶部IAmDown ,我不希望出现这种情况。 All I want is that the Green box be on top of the Red box and the Red box is on top of the blue one. 我想要的是绿色框位于红色框的上方,红色框位于蓝色框的顶部。

Is there any solution to this aside from moving the Green box outside of it's parent, or EDIT the HTML? 除了将绿色框移到其父框之外或编辑 HTML之外,还有其他解决方案吗?

I'm down with using JavaScript (not really on JQuery) in manipulating this but i don't know how to. 我不喜欢使用JavaScript(不是真的在JQuery上)来操纵它,但是我不知道该怎么做。 Help? 救命?

This is one of the dreaded stacking index problems: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Positioning/Understanding_z_index/The_stacking_context Once you create an element with positioning: fixed, you're creating a new stacking context. 这是可怕的堆叠索引问题之一: https : //developer.mozilla.org/zh-CN/docs/Web/CSS/CSS_Positioning/Understanding_z_index/The_stacking_context一旦创建了具有定位:固定的元素,就在创建一个新的堆栈上下文。 You can't shuffle elements between stacking contexts. 您不能在堆叠上下文之间随机播放元素。

You need to change your markup, unfortunately. 不幸的是,您需要更改标记。

<div id="newParent">
 <div id="oldParent"></div>
 <span id="IAmGreater"></span>
</div>
 <div id="IAmDown">
</div>

This works. 这可行。 Made a change in html html了更改

 #IAmGreater{ position:fixed; z-index: 1000; background-color: green; height: 50px; width: 50px; top: 0; left: 0; } #parent{ background-color: blue; height: 200px; width: 200px; position: fixed; z-index: -1; margin: 50px; } #IAmDown{ position: absolute; background-color:red; height:100px; width: 100px; z-index: 0; } 
 <div id="parent"></div> <span id="IAmGreater"></span> <div id="IAmDown"> </div> 

Its parent ( #parent ) doesn't have a z-index, that's why. 其父级( #parent )没有z-index,这就是为什么。 If you add one which is greater than the one of #IAmDown , it changes as expected: 如果添加的值大于#IAmDown的值之一,则它会按预期更改:

 #IAmGreater{ position:fixed; z-index: 999!important; background-color: green; height: 50px; width: 50px; top: 0; left: 0; } #parent{ background-color: blue; height: 200px; width: 200px; position: fixed; margin: 50px; z-index:1; } #IAmDown{ position: absolute; z-index: 0; background-color:red; height:100px; width: 100px; } 
 <div id="parent"> <span id="IAmGreater"></span> </div> <div id="IAmDown"> </div> 

Here's a second answer that contains a solution as you requested in the comment to my first answer. 这是第二个答案,其中包含您在第一个答案的注释中要求的解决方案。 All elements have fixed position, and the green one is moved out of the red one: 所有元素都具有固定位置,绿色元素从红色元素移出:

 #IAmGreater{ position:fixed; z-index: 999!important; background-color: green; height: 50px; width: 50px; top: 0; left: 0; } #parent{ background-color: blue; height: 200px; width: 200px; position: fixed; margin: 50px; } #IAmDown{ position: fixed; z-index: 0; background-color:red; height:100px; width: 100px; } 
 <div id="parent"> </div> <span id="IAmGreater"></span> <div id="IAmDown"> </div> 

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

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