can I override z-index inheritance from parent element?
I believe z-index is relative to the nearest positioned element. So, if you had two divs inside the "1111" div, they could be z-index'd relative to each other, but since 2222 is a child of 1111, it cannot be z-indexed relative to 0000, and will always be above 1111.
How to override inherited z-indexes?
You can't give a child higher z-index
than its parent. You may want to rethink your design if this is the case, or consider popping the child out of the parent temporarily to show it on a higher index than its parent.
The physical order of the divs in this case can help as well. If you move the first down after the last, you'll get the preferred rendering. But this may not be ideal for your situation.
How to make child element higher z-index than parent?
This is impossible as a child's z-index
is set to the same stacking index as its parent.
You have already solved the problem by removing the z-index from the parent, keep it like this or make the element a sibling instead of a child.
Z-index of the pseudo-element of the parent element, of the child element and the child itself
I removed the z-index everywhere and added it to the h1 like this:
.wrapper {
position: relative;
background: #000;
overflow: hidden;
}
.wrapper::after {
content: "";
position: absolute;
width: 650px;
height: 1500px;
background: rgb(244,116,31);
background: -moz-linear-gradient(top, rgba(244,116,31,1) 0%, rgba(218,14,6,1) 100%);
background: -webkit-linear-gradient(top, rgba(244,116,31,1) 0%,rgba(218,14,6,1) 100%);
background: linear-gradient(to bottom, rgba(244,116,31,1) 0%,rgba(218,14,6,1) 100%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#f4741f', endColorstr='#da0e06',GradientType=0 );
transform: rotate(40deg);
opacity: .6;
right: 100px;
top: -200px;
}
.text-block {
position: relative;
width: 70%;
margin: 0 auto;
padding: 50px;
}
.text-block::after {
content: "";
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
background: #2b2525;
}
h1 {
position: relative;
color: #fff;
z-index: 1;
}
fiddle
Why can't an element with a z-index value cover its child?
There are two important things you need to know: the painting order and the stacking context. If you refer to the specification, you can find how and when elements are painted.
- Stacking contexts formed by positioned descendants with negative z-indices (excluding 0) in z-index order (most negative first) then tree order.
All positioned, opacity or transform descendants, in tree order that fall into the following categories:
- All positioned descendants with 'z-index: auto' or 'z-index: 0', in tree order.
- Stacking contexts formed by positioned descendants with z-indices greater than or equal to 1 in z-index order (smallest first) then tree order.
It's clear from this that we first paint elements with negative z-index
at step (3), then the one with z-index
equal to 0 at step (8), and finally the ones with positive z-index
at step (9), which is logical. We can also read in another part of the specification:
Each box belongs to one stacking context. Each box in a given stacking context has an integer stack level, which is its position on the z-axis relative to other boxes in the same stacking context. Boxes with greater stack levels are always formatted in front of boxes with lower stack levels. Boxes may have negative stack levels. Boxes with the same stack level in a stacking context are stacked bottom-to-top according to document tree order.
To understand when each element will be painted you need to know its stacking context and its stack level inside this stacking context (defined by z-index
). You also need to know whether that element establishes a stacking context. This is the tricky part, because setting z-index
will do this:
For a positioned box, the z-index property specifies:
- The stack level of the box in the current stacking context.
- Whether the box establishes a stacking context
Values have the following meanings:
<integer>
This integer is the stack level of the generated box in the current stacking context. The box also establishes a new stacking context.
auto
The stack level of the generated box in the current stacking context is 0. The box does not establish a new stacking context unless it is the root element.
Now we have all the information to better understand each case. If the parent element has a z-index
value of something other than auto
, then it will create a stacking context, thus the child element will be painted inside whatever their z-index
is (negative or positive). The z-index
of the child element will simply tell us the order of painting inside the parent element (this covers your second point).
Now, if only the child element has a positive z-index
and we set nothing on the parent element, then considering the painting order, the child will be painted later (in step (9)) and the parent in step (8). The only logical way to paint the parent above is to increase the z-index
, but doing this will make us fall into the previous case where the parent will establish a stacking context and the child element will belong to it.
There is no way to have the parent above a child element when setting a positive z-index
to the child. Also there is no way to have the parent above the child if we set a z-index
to the parent element different from auto
(either positive or negative).1
The only case where we can have a child below its parent is to set a negative z-index
on the child element and keep the parent at z-index: auto
, thus this one will not create a stacking context and following the painting order the child will be painted first.
In addition to z-index
, there are other properties that create a stacking context. In case you face an expected stacking order, you need to consider those properties, too, in order to see if there is a stacking context created.
Some important facts that we can conclude from the above:
- Stacking contexts can be contained in other stacking contexts, and together create a hierarchy of stacking contexts.
- Each stacking context is completely independent of its siblings: only descendant elements are considered when stacking is processed.
- Each stacking context is self-contained: after the element's contents are stacked, the whole element is considered in the stacking order of the parent stacking context. ref
1: there is some hacky ways if we consider the use of 3D transformation.
Example with an element going under its parent element even if this one has a z-index
specified.
.box {
position:relative;
z-index:0;
height:80px;
background:blue;
transform-style: preserve-3d; /* This is important */
}
.box > div {
margin:0 50px;
height:100px;
background:red;
z-index:-1; /* this will do nothing */
transform:translateZ(-1px); /* this will do the magic */
}
<div class="box">
<div></div>
</div>
stacking with z-index, child element on top over parent sibling
I removed the position absolute from the yellow div and removed the z-index from the green div. Maybe this is something as you said.
.parent {
background-color: yellow;
top: 0;
left: 0;
height: 200px;
width: 200px;
z-index: 1;
}
.child {
background-color: red;
position: relative;
top: 10px;
left: 20px;
height: 50px;
width: 150px;
z-index: 2;
}
.other-guy {
background-color: green;
position: absolute;
top: 40px;
left: 100px;
height: 200px;
width: 200px;
}
<div class="parent">Chillin in the background
<div class="child">I really want to be on top.</div>
</div>
<div class="other-guy">I want to be in the middle!</div>
How can I get a child of an element with lower z-index on front of a child of another element with higher z-index?
In short: you can't. It's not possible to have a child of a parent element with a z-index
lower than a sibling, and give that child a higher z-index value than the parent's sibling, as the inherited z-index is relative to the parent (see this question for someone coming up against the same thing).
However, depending on your setup, you can remove the z-indices completely, and let the browser place your top div
above the one below. You could then play around with giving just the children a z-index
value.
Josh Comeau has a great article going into depth on the problem and various solutions/workarounds.
Related Topics
Why Do I Have to Put Media Queries at the Bottom of the Stylesheet
Backside-Visibility Not Working in Ie10 - Works Fine in Webkit
How to Recreate Perspective-Origin Effect by Transforming Child Elements
Is There Any Equivalent to Ie Conditional Comment for Chrome and Safari
How to Link a CSS File from HTML File
Line-Height as a Percentage Not Working
CSS Selectors Ul Li a {...} VS Ul > Li > a {...}
How to Customize Twitter Widget Style
Hover on Element and Highlight All Elements with the Same Class
Text with Image Background in Svg or CSS
Alternative for Nth-Child for Older Ie Browsers
How to Position a Div in the Middle of the Screen When the Page Is Bigger Than the Screen
Removing the Ie10 Select Element Arrow
How to Create "Collapsed" Borders Around Flex Items and Their Container