简体   繁体   English

Z-Index不起作用

[英]Z-Index is not working

https://codepen.io/anon/pen/veBQYy?editors=1100 https://codepen.io/anon/pen/veBQYy?editors=1100

I have a div with the class filter. 我有一个带有类过滤器的div。 That div is to make the background image lighter. 该div是为了使背景图像更亮。 I am trying to make the div with the class inner go on top of the filter. 我正在尝试使带有内部类的div放在过滤器的顶部。 So I put z index: 9999 on the inner div but its not going to the top 所以我将z索引:9999放在了内部div上,但它没有到达顶部

 html, body { height: 100%; } .outer { display: table; height: 100%; width: 100%; background: url('https://static.pexels.com/photos/56875/tree-dawn-nature-bucovina-56875.jpeg'); background-size: cover; } .middle { display: table-cell; width: 100%; vertical-align: middle; text-align: center; // Center everything in div } /* To dim background */ .filter { position: absolute; width: 100%; height: 100%; top: 0; background-color: black; opacity: 0.5; } /* This is not working. Z index not bringing it to the top */ .inner { z-index: 9999; } h1 { color: white; } 
 <section class="outer"> <div class="middle"> <div class="filter"></div> <div class="inner"> <h1>Need</h1> <h1>This to go on top zindex 9999 not working</h1> </div> </div> </section> 

You need to make the .inner div position relative 您需要使.inner div位置相对

.inner {
  z-index: 9999;  
  position: relative;
}

z-index only works on positioned elements (position:absolute, position:relative, or position:fixed). z-index仅适用于定位的元素(position:absolute,position:relative或position:fixed)。

https://www.w3schools.com/cssref/pr_pos_z-index.asp https://www.w3schools.com/cssref/pr_pos_z-index.asp

The div.filter absolute positioned block is rendered outside of the normal flow. div.filter绝对定位的块呈现在正常流之外。 div.inner is in the normal flow. div.inner正常。 In this case you need to change div.filter. 在这种情况下,您需要更改div.filter。

When you introduce the position property into the mix, any positioned elements (and their children) are displayed in front of any non-positioned elements. 当您将position属性引入混合时,所有已定位元素(及其子元素)都会显示在所有未定位元素的前面。 (To say an element is “positioned” means that it has a position value other than static, eg, relative, absolute, etc.). (说一个元素是“定位的”意味着它的位置值不是静态的,例如相对值,绝对值等)。

These positioned blocks depart from the stacking order of the non-positioned blocks. 这些已定位的块与未定位的块的堆叠顺序不同。 IE z-index:9999; IE z-index:9999; does not put the non-positioned block on top. 不会将未定位的块放在顶部。 But, you can put the positioned blocks under the non-positioned ones. 但是,您可以将已定位的块放在未定位的块下面。

.filter {   
  z-index:-1;
}

 html, body { height: 100%; } .outer { display: table; height: 100%; width: 100%; background: url('https://static.pexels.com/photos/56875/tree-dawn-nature-bucovina-56875.jpeg'); background-size: cover; } .middle { display: table-cell; width: 100%; vertical-align: middle; text-align: center; // Center everything in div } /* To dim background */ .filter { position: absolute; width: 100%; height: 100%; top: 0; background-color: black; opacity: 0.5; z-index:-1; } /* This is not working. Z index not bringing it to the top */ .inner { z-index: 9999; } h1 { color: white; } 
 <section class="outer"> <div class="middle"> <div class="filter"></div> <div class="inner"> <h1>Need</h1> <h1>This to go on top zindex 9999 not working</h1> </div> </div> </section> 

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

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