Before starting the explanation, let me note that the z-index only affects the fact that the element displays the value position:relative , position:absolute or position:fixed (NOT static ), because each of them does this has its own stack context . This means that values ββsuch as initial or inherit may or may not work depending on the situation.
Also note that in this post I will use the 1.1.1 format to indicate that I select the first element of the first child of the first child. 2.1.1 will be the first first first child of the first element, etc.
I think z-index best explained using a subscription analogy. Let's start with the simplest example:
<div class="top-level"></div> <div class="top-level sibling"></div>
We can present this in terms of such a list:
- Top level
- Highest level sibling
Now, by default, those further in the list will be displayed on top of them in front of it. Thus, in this case 2 will be located on top of 1.
What z-index allows us to do is significantly change the order of this list (within certain limits). The higher the z-index, the further down the list is our item.
I use inline CSS here to show it easily, but you should definitely avoid inline CSS in production code.
<div class="top-level" style="z-index: 1;"></div> <div class="top-level sibling"></div>
Now this will change our optional list to look like this:
- Highest level sibling
- Top Level - z-index: 1
Excellent! Now the first element of our HTML will be displayed on top of our second.
If it becomes more complicated, this is when we are dealing with child (nested) elements.
An easier way to think about this situation is to think that when an element begins to be rendered, it will display all the children of this element before moving to any siblings .
Also keep in mind that sub-lists cannot change levels , which means that they cannot be at the same level as parents or their children.
This means that if we have the following:
<div class="top-level"> <div class="sub-level" style="z-index: 1;"></div> </div> <div class="top-level sibling"></div>
Our helper display lists will look like this:
- Top level
- Highest level sibling
Thus, we look at our top level and see which one is at the bottom of the list. In this case, 2.0, so that it will be above 1.0. Then we will see if he has any signatures (children). This is not so, so we move on to 1.0.
1.0 has a child 1.1, which will be visually higher than 1.0 (exactly the same as if we hadn't given it a z-index), but it will still be lower than 2.0 because 1.0 is lower than 2.0.
Thus, the z-index here does not help us, because 1.1 does not have a single brother and sister.
Take a slightly more complex example:
<div class="top-level"> <div class="sub-level" style="z-index: 2;"></div> <div class="sub-level sibling"></div> <div class="sub-level sibling" style="z-index: 1;"></div> </div> <div class="top-level sibling"> <div class="sub-level"></div> </div>
What is the optional list for this example?
I will give it to you, but it's good to try and do it yourself.
- Top level
- Junior Level
- Ancillary Level - z-index1
- Sub level - z-index2
- Summit brother
Thus, from the point of view of the visual representation from top to bottom, the order is 2.1, 2.0, 1.3, 1.2. 1.1, 1.0.
This is not so bad, is it?
This behavior is true no matter how deep or how many brothers and sisters there are.
The only exception to the rule that children appear above their parents is when children have a negative z-index . When a negative z-index is specified, it is placed below the parent element.
Thus, if we have the following:
<div class="top-level"> <div class="sub-level" style="z-index: -1;"></div> </div> <div class="top-level sibling"></div>
The subscription tree will look like this:
- Sub level - z-index-1
- Highest level sibling
And the top to bottom layer will be 2.0, 1.0, 1.1.
A slightly more complex example:
<div class="top-level"> <div class="sub-level" style="z-index: -1;"></div> <div class="sub-level sibling"></div> </div> <div class="top-level sibling"></div>
List view:
- Sub level - z-index-1
- Highest level sibling
But you should avoid negative z-index es . If you think you need them, it is likely that your HTML is structured incorrectly.
That's about it! If you are still interested in learning more, reading characteristics is always good.
Keep in mind that other properties , including but not limited to opacity , transform and will-change , create your own stacking context and may affect the order in which elements are displayed.
opacity works similarly to z-index - a child can only be opaque like its parent, but it cannot have negative values.