Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What has bigger priority: opacity or z-index in browsers?

I'm coding a "popup window" in JavaScript and I've come across an interesting thing:

Cropped screenshot demonstrating strange stacking behavior

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
like image 950
Martin Vseticka Avatar asked May 14 '10 19:05

Martin Vseticka


People also ask

Does opacity affect Z index?

If an element with opacity less than 1 is positioned, the 'z-index' property applies as described in [CSS21], except that 'auto' is treated as '0' since a new stacking context is always created.

Is Z index higher?

The z-index property determines the stack level of an HTML element. The “stack level” refers to the element's position on the Z axis (as opposed to the X axis or Y axis). A higher value means the element will be closer to the top of the stacking order.

What is the highest Zindex?

The maximum range is ±2147483647. In CSS code bases, you'll often see z-index values of 999, 9999 or 99999.


2 Answers

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):

Since an element with opacity less than 1 is composited from a single offscreen image, content outside of it cannot be layered in z-order between pieces of content inside of it. For the same reason, implementations must create a new stacking context for any element with opacity less than 1. If an element with opacity less than 1 is not positioned, implementations must paint the layer it creates, within its parent stacking context, at the same stacking order that would be used if it were a positioned element with ‘z-index: 0’ and ‘opacity: 1’. If an element with opacity less than 1 is positioned, the ‘z-index’ property applies as described in [CSS21], except that ‘auto’ is treated as ‘0’ since a new stacking context is always created. See section 9.9 and Appendix E of [CSS21] for more information on stacking contexts. The rules in this paragraph do not apply to SVG elements, since SVG has its own rendering model ([SVG11], Chapter 3).

like image 184
0b10011 Avatar answered Sep 22 '22 04:09

0b10011


It's not a problem of opacity being more important than z-index, rather than z-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 a position: relative; to the element that contain both your popup and your navy square, and probably add it a z-index: 1; . Seeing your screenshot it will probably be a top element such as a wrapper div.

like image 26
Guillaume Esquevin Avatar answered Sep 21 '22 04:09

Guillaume Esquevin