Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is z-index not independent of stacking context?

Tags:

css

z-index

An element that has a high z-index may below another element that has a low z-index, only because the ancestors' z-index of the lower one provides a higher stacking context for this element.

Because of this, it's impossible to overlay an element from a lower stacking context without a major change in the CSS or HTML code. On the other hand, using the z-index property would be simples if it would be an "absolute" value (higher z-index -> higher position on the z axis).

My question is, why was this implemented this way in the browsers and the standards?

like image 834
Herbertusz Avatar asked Nov 10 '12 17:11

Herbertusz


People also ask

What is the Z Index What is the stacking context?

The stacking context is a three-dimensional conceptualization of HTML elements along an imaginary z-axis relative to the user, who is assumed to be facing the viewport or the webpage. HTML elements occupy this space in priority order based on element attributes.

Does Z index only work with absolute positioning?

An element with greater stack order is always in front of an element with a lower stack order. Note: z-index only works on positioned elements (position: absolute, position: relative, position: fixed, or position: sticky) and flex items (elements that are direct children of display:flex elements).

Does Z Index work without position?

z-index only works on positioned elements. If you try to set a z-index on a non-positioned element, it will do nothing. However, there is one exception - flex children can use z-index even if they're non-positioned.

Why Z index is not working in before?

z-index only works on positioned elements (relative, absolute, fixed, sticky) so if you set a z-index on an element with a static position, it won't work.


1 Answers

I think cwolves has the proper answer, and the way it is now it's probably the most logic. To help understanding you can think of z-index like Photoshop layers. If you have a folder of layers above another folder, the wrapped layers can't obviously be arranged outside their parents, unless you move them from a folder to another.

Or think of boxes: you can arrange things inside a box, but you can't arrange things from two different boxes, unless you move them from a box to another.

If you're running into the case where you need a different arrangement, you probably need to re-think your markup to allow this.

like image 198
gyo Avatar answered Sep 27 '22 20:09

gyo