Html 为什么 z-index 不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9191803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 22:22:07  来源:igfitidea点击:

Why does z-index not work?

htmlcssz-index

提问by Linkjuice57

So if I understand z-indexcorrectly, it would be perfect in this situation:

因此,如果我理解z-index正确,在这种情况下将是完美的:

enter image description here

在此处输入图片说明

I want to place the bottom image (the tag/card) below the div above it. So you can't see the sharp edges. How do I do this?

我想将底部图像(标签/卡片)放在其上方的 div 下方。所以你看不到锋利的边缘。我该怎么做呢?

z-index:-1 // on the image tag/card

or

或者

z-index:100 // on the div above

doesn't work either. Neither does a combination of anything like this. How come?

也不起作用。像这样的组合也没有。怎么来的?

回答by Evert

The z-indexproperty only works on elements with a positionvalue other than static(e.g. position: absolute;, position: relative;, or position: fixed).

z-index属性仅适用于position值不是static(例如position: absolute;position: relative;、 或position: fixed)的元素。

There is also position: sticky;that is supported in Firefox, is prefixed in Safari, worked for a time in older versions of Chrome under a custom flag, and is under consideration by Microsoft to add to their Edge browser.

还有position: sticky;Firefox 支持,在 Safari 中带有前缀,在旧版本的 Chrome 中使用自定义标志,微软正在考虑将其添加到他们的 Edge 浏览器中。

Important
For regular positioning, be sure to include position: relativeon the elements where you also set the z-index. Otherwise, it won't take effect.

重要
对于常规定位,请确保position: relative在您还设置z-index. 否则不会生效。

回答by Buksy

If you set position to other value than staticbut your element's z-indexstill doesn't seem to work, it may be that some parent element has z-indexset.

如果您将位置设置为其他值,static但您的元素z-index似乎仍然不起作用,则可能是某些父元素已z-index设置。

The stacking contexts have hierarchy, and each stacking context is considered in the stacking order of the parent's stacking context.

堆叠上下文具有层次结构,每个堆叠上下文都按照父级堆叠上下文的堆叠顺序进行考虑。

So with following html

所以用下面的html

div { border: 2px solid #000; width: 100px; height: 30px; margin: 10px; position: relative; background-color: #FFF; }
#el3 { background-color: #F0F; width: 100px; height: 60px; top: -50px; }
<div id="el1" style="z-index: 5"></div>
<div id="el2" style="z-index: 3">
  <div id="el3" style="z-index: 8"></div>
</div>

no matter how big the z-indexof el3will be set, it will always be under el1because it's parent has lower stacking context. You can imagine stacking order as levels where stacking order of el3is actually 3.8which is lower than 5.

不管多大z-indexel3将被设置,它总是会在el1,因为它的母公司具有较低的堆叠内容。您可以将堆叠顺序想象为堆叠顺序el3实际上是3.8 的级别,低于5

If you want to check stacking contexts of parent elements, you can use this:

如果要检查父元素的堆叠上下文,可以使用:

var el = document.getElementById("#yourElement"); // or use ##代码## in chrome;
do {
    var styles = window.getComputedStyle(el);
    console.log(styles.zIndex, el);
} while(el.parentElement && (el = el.parentElement));

There is a great article about stacking contexts on MDN

MDN 上有一篇关于堆叠上下文的好文章

回答by clem

Your elements need to have a positionattribute. (e.g. absolute, relative, fixed) or z-indexwon't work.

您的元素需要有一个position属性。(例如absolute, relative, fixed)或z-index不起作用。

回答by Michael Benjamin

In many cases an element must be positioned for z-indexto work.

在许多情况下,必须定位元素才能z-index工作。

Indeed, applying position: relativeto the elements in the question would likely solve the problem (but there's not enough code provided to know for sure).

事实上,应用position: relative到问题中的元素可能会解决问题(但没有提供足够的代码来确定)。

Actually, position: fixed, position: absoluteand position: stickywill also enable z-index, but those values also change the layout. With position: relativethe layout isn't disturbed.

其实position: fixedposition: absolute并且position: sticky还将使z-index,但这些值也改变了布局。随着position: relative布局不被打扰。

Essentially, as long as the element isn't position: static(the default setting) it is considered positioned and z-indexwill work.

本质上,只要元素不是position: static(默认设置),它就被认为是定位的并且z-index可以工作。



Many answers to "Why isn't z-index working?"questions assert that z-indexonlyworks on positioned elements. As of CSS3, this is no longer true.

“为什么 z-index 不起作用?”的许多答案 问题断言z-index适用于定位元素。从 CSS3 开始,这不再是事实。

Elements that are flex itemsor grid itemscan use z-indexeven when positionis static.

弹性项目网格项目的元素z-index即使在positionis时也可以使用static

From the specs:

从规格:

4.3. Flex Item Z-Ordering

Flex items paint exactly the same as inline blocks, except that order-modified document order is used in place of raw document order, and z-indexvalues other than autocreate a stacking context even if positionis static.

5.4. Z-axis Ordering: the z-indexproperty

The painting order of grid items is exactly the same as inline blocks, except that order-modified document order is used in place of raw document order, and z-indexvalues other than autocreate a stacking context even if positionis static.

4.3. Flex 项目 Z 排序

Flex 项目的绘制与内联块完全相同,除了使用顺序修改的文档顺序代替原始文档顺序,以及创建堆叠上下文z-index以外的值,auto即使positionstatic.

5.4. Z轴排序:z-index属性

网格项的绘制顺序与内联块完全相同,除了使用顺序修改的文档顺序代替原始文档顺序,以及创建堆叠上下文z-index以外的值,auto即使 positionstatic

Here's a demonstration of z-indexworking on non-positioned flex items: https://jsfiddle.net/m0wddwxs/

这是z-index处理非定位弹性项目的演示:https: //jsfiddle.net/m0wddwxs/