浏览器中的不透明度和 z-index 哪个优先级更高?
我正在用 JavaScript 编写一个“弹出窗口”,我遇到了一件有趣的事情:
弹出窗口下方的海军蓝方块是可见的,尽管我希望它被隐藏。弹出窗口是在方块之后添加的,因此它应该位于顶部。
海军蓝方块的 CSS opacity
属性为 0.3
。根据我的尝试,似乎区间 (0,1)
中的每个数字都会产生相同的结果。如果我将其更改为 1
,那么它的行为将按预期进行(即弹出窗口下方的正方形部分被隐藏)。
我尝试将正方形的 z-index
属性设置为 10
,将弹出窗口的 100
设置为 100
,但它没有改变任何东西。
我缺少什么?为什么显示部分正方形?
测试的浏览器:
- Firefox 3.6.x
- Chrome 4
I'm coding a "popup window" in JavaScript and I've come across an interesting thing:
The navy square under the popup window is visible even though I would expect it to be hidden. The popup was added after the square, so it should be on the top.
CSS opacity
property of the navy square is 0.3
. From what I've tried, it seems that every number from the interval (0,1)
would yield the same result. If I change it to 1
, then it behaves as expected (i.e. the part of the square under the popup is hidden).
I've tried to set the z-index
property to 10
for the square and 100
for the popup, but it doesn't change anything.
What am I missing? Why is part of square displayed?
Tested browsers:
- Firefox 3.6.x
- Chrome 4
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
可能需要示例代码来调试此问题。
您可以将
overflow:hidden
和可能的position:relative
放在包围所有编辑器对象的DIV
中,以尝试强制元素仅在在该DIV
内绘制,例如:作为最后的手段,您还可以尝试在两个元素之间使用
iframe
来阻止它们渗透。Example code might be needed to debug this problem.
You might put
overflow: hidden
and possiblyposition: relative
in aDIV
which surrounds all the editor objects to try to force the elements to only be drawn within thatDIV
, e.g:As a last resort, you could also try a
iframe
in between the two elements to try to stop them seeping through.您可以尝试使用
!important
设置弹出窗口的 DIV,这样样式在应用新样式或类时不会改变:然后,创建新的 CSS 类:
并将类添加到窗口,例如:
我的猜测是,这会起作用,因为应用 CSS 属性没有优先级......据我所知。 =)
You might try to set the popup window's DIV like this using
!important
so the style doesn't change on applying new style or class:Then, make new CSS class:
And add class to all elements in the window, like this for example:
My guess is that this would work, since there is no priority in applying CSS attributes... as far as I know. =)
我有同样的问题。使用 rgba 而不是颜色/不透明度解决了我的问题。使用 LESS(在 Bootstrap 框架中),fade() 函数为我完成了转换。
I had the same issue. Using rgba instead of color/opacity solved my problem. Working with LESS (in the Bootstrap framework), the fade() function did the conversion for me.
虽然 @Guillaume Esquevin 已经给出了一个很好的答案,但我会尝试扩展它,以防有人忽略堆叠上下文是什么(就像我所做的那样)。
正如您可以在此处阅读的那样,是一种称为堆栈上下文的东西,它指的是一组共享父元素并在堆栈中一起移动的元素。一个例子是 div 及其所有子项。
有三种方法可以创建堆叠上下文:在文档的根目录(html 元素)中,通过定位父元素,以及将父元素的不透明度更改为低于 1 的值。
然后,如果您有一个 div如果不透明度低于 1,并且您希望该 div 的某个同级元素出现在其(及其子级)后面,则可以通过将其位置设置为相对位置或更改其不透明度来在此类同级元素上创建新的堆叠上下文。
Although @Guillaume Esquevin already gave a great answer, I will try to expand on it in case someone ignores what a stacking context is (like I did).
As you can read here, there is something called stacking context, which refers to a group of elements sharing a parent that move together in the stack. An example could be a div and all its children.
There are three ways to create a stacking context: in the root of the document (the html element), by positioning the parent element, and by changing the opacity of the parent to something lower than 1.
Then, if you have a div with opacity lower than 1 and you want some sibling element of this div to appear behind it (and its children), you can create a new stacking context on such sibling by setting its position to relative or by changing its opacity as well.
这不是一个错误,实际上它应该如何工作。这有点令人困惑,因为堆叠上下文的详细描述没有提及任何相关内容。但是,视觉格式化模块链接到颜色模块,可以在其中找到这个特定的问题(强调我的):
This is not a bug and is actually how it's supposed to work. It's a bit confusing as the elaborate description of Stacking Contexts doesn't mention anything about it. However, the visual formatting module links to the color module where this particular gotcha can be found (emphasis mine):
这不是
opacity
比z-index
更重要的问题,而不是z-index
相对于其堆叠上下文的问题(请参阅z-index)。换句话说,z-index 仅在定位祖先的上下文中才有意义(无论是相对、绝对还是固定)。要解决问题,您需要做的是将
position:relative;
添加到包含弹出窗口和海军方块的元素,并可能添加它z-index: 1;
。看到你的屏幕截图,它可能是一个顶部元素,例如包装 div。It's not a problem of
opacity
being more important thanz-index
, rather thanz-index
being relative to their stacking context (see z-index in the CSS2 specification).In other words,
z-index
are only significant within the context of a positioned ancestor (whether its relative, absolute or fixed). What you need to do to fix your problem is add aposition: relative;
to the element that contain both your popup and your navy square, and probably add it az-index: 1;
. Seeing your screenshot it will probably be a top element such as a wrapper div.两个元素(例如 div)的解决方法:向顶部元素添加
0.99
不透明度,然后重新建立两个元素的顺序。Workaround for two elements, like divs: add a
0.99
opacity to your top element, and the order of both is reestablished.使用不透明度的另一种方法是使用透明颜色(带有 alpha 值),
因此,
您可以尝试
使用透明颜色,它并不相同,但我遇到了与您遇到的相同问题,上面的方法修复了它。
An alternative to using opacity, is to use a transparent colour (with an alpha value)
So, rather than using
You could try
It isn't identical, but I was encountering the same issue you were having, and the above fixed it.