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
Why does z-index not work?
提问by Linkjuice57
So if I understand z-index
correctly, it would be perfect in this situation:
因此,如果我理解z-index
正确,在这种情况下将是完美的:
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-index
property only works on elements with a position
value 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: relative
on 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 static
but your element's z-index
still doesn't seem to work, it may be that some parent element has z-index
set.
如果您将位置设置为其他值,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-index
of el3
will be set, it will always be under el1
because it's parent has lower stacking context. You can imagine stacking order as levels where stacking order of el3
is actually 3.8which is lower than 5.
不管多大z-index
的el3
将被设置,它总是会在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));
回答by clem
Your elements need to have a position
attribute. (e.g. absolute
, relative
, fixed
) or z-index
won't work.
您的元素需要有一个position
属性。(例如absolute
, relative
, fixed
)或z-index
不起作用。
回答by Michael Benjamin
In many cases an element must be positioned for z-index
to work.
在许多情况下,必须定位元素才能z-index
工作。
Indeed, applying position: relative
to 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: absolute
and position: sticky
will also enable z-index
, but those values also change the layout. With position: relative
the layout isn't disturbed.
其实position: fixed
,position: 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-index
will work.
本质上,只要元素不是position: static
(默认设置),它就被认为是定位的并且z-index
可以工作。
Many answers to "Why isn't z-index working?"questions assert that z-index
onlyworks 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-index
even when position
is static
.
弹性项目或网格项目的元素z-index
即使在position
is时也可以使用static
。
From the specs:
从规格:
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-index
values other thanauto
create a stacking context even ifposition
isstatic
.5.4. Z-axis Ordering: the
z-index
propertyThe 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-index
values other thanauto
create a stacking context even ifposition
isstatic
.
Flex 项目的绘制与内联块完全相同,除了使用顺序修改的文档顺序代替原始文档顺序,以及创建堆叠上下文
z-index
以外的值,auto
即使position
是static
.网格项的绘制顺序与内联块完全相同,除了使用顺序修改的文档顺序代替原始文档顺序,以及创建堆叠上下文
z-index
以外的值,auto
即使position
是static
。
Here's a demonstration of z-index
working on non-positioned flex items: https://jsfiddle.net/m0wddwxs/
这是z-index
处理非定位弹性项目的演示:https: //jsfiddle.net/m0wddwxs/