130

Suppose I have this code:

<div class="parent">
    <div class="child">
        Hello world
    </div>
</div>

<div class="wholePage"></div>

This jsFiddle: http://jsfiddle.net/ZjXMR/

Now, I need to have<div class="child"> in above of <div class="wholePage"> but in the jsFiddle you can see that the child element rendered before <div class="wholePage">.

If you remove the parent class position or z-index, everything works fine. This is the correct behavior that I need: http://jsfiddle.net/ZjXMR/1/

How can I do that with z-index and without removing anything from page?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Afshin Mehrabani
  • 33,262
  • 29
  • 136
  • 201

6 Answers6

102

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.

Kyle
  • 65,599
  • 28
  • 144
  • 152
  • 1
    Thanks Kyle, but as I mentioned before, I have to solve the problem without removing anything. – Afshin Mehrabani Apr 17 '13 at 10:17
  • 12
    `z-index` isn't inherited. – BoltClock Apr 17 '13 at 10:20
  • @boltclock It is taken from the parent: http://jsfiddle.net/ykzEH/ – Kyle Apr 17 '13 at 10:46
  • 41
    That's not inheritance. The `z-index: 50` appears over `z-index: 100` because the 1 and 50 are in the base stacking context, whereas 100 is in the stacking context of 1. Since 50 is greater than 1, 50 appears above 1 and everything in 1's context, but the 100 still retains a `z-index` of 100 *within 1*. – BoltClock Apr 17 '13 at 10:50
  • 1
    Ok, here it is with 1, 2 and 3. http://jsfiddle.net/ykzEH/1/ I've always understood that the parent element sets the z-index of all its children (I knew it wasn't inherited though.. Dunno why I wrote that :D) – Kyle Apr 17 '13 at 10:52
  • 3
    @Kyle : If you don't know why you wrote that is not correct then edit the answer to be correct. – jimjim Feb 01 '17 at 05:13
  • just to note that if there are other css styles at play, then you'll want to unset the z-index of those rules, you can test that by using the * { z-index: unset; } if that works, you'll know you are on the right track. Always be aware of external forces at play, its easy to have this happen on more complicated pages where there are 2 or more developers at any given time, etc. Obviously rewriting/reparenting is desirable but if you dont have access to the source (is not your department or is read-only for some other reason) you are gonna be up against a wall, forcing zindexes can help or hurt! – osirisgothra Mar 15 '22 at 18:02
24

Nothing is impossible. Use the force.

.parent {
    position: relative;
}

.child {
    position: absolute;
    top:0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 100;
}
supermasher
  • 684
  • 9
  • 17
  • 33
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – gunr2171 Dec 08 '15 at 16:37
  • If we can't change the style of parent so this is a perfect solution to resolve problem. I tried and nice . thanks – huykon225 Jan 23 '19 at 04:42
  • 2
    > If you remove the parent class position or z-index, everything works fine. You've just removed the parent z-index, which I believe the OP couldn't modify. – EoghanM Oct 30 '19 at 09:22
  • position:relative changed the stacking order of parent. I think it is probably because relative make 'parent' step out of the current page structure. Check out this:https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Positioning/Understanding_z_index/The_stacking_context – MING WU Oct 24 '20 at 06:55
  • 2
    Alternatively, set parent z-index to `auto`. – thdoan Feb 23 '21 at 02:14
9

To achieve what you want without removing any styles you have to make the z-index of the '.parent' class bigger then the '.wholePage' class.

.parent {
    position: relative;
    z-index: 4; /*matters since it's sibling to wholePage*/
}

.child {
    position: relative;
    z-index:1; /*doesn't matter */
    background-color: white;
    padding: 5px;
}

jsFiddle: http://jsfiddle.net/ZjXMR/2/

MythThrazz
  • 1,629
  • 17
  • 25
8

Give the parent z-index: -1, or opacity: 0.99

Greg Benner
  • 668
  • 7
  • 15
  • 36
    If people are wondering what the opacity:0.99 is all about I suggest you read this link. http://philipwalton.com/articles/what-no-one-told-you-about-z-index/ – johnsnails Oct 21 '13 at 02:14
  • This seems not to work in IE 11 – Benedikt Sep 06 '17 at 12:07
  • 5
    EVERYBODY ON THIS PAGE SHOULD READ THE ARTICLE LINKED IN THE PREVIOUS COMMENT! Seriously, everything you need to know about z-index - very helpful stuff, with some real silver bullets sprinkled in the mix too. – Devin Rhode Dec 13 '18 at 19:23
  • This solved the problem for me when I had 3 parents divs and 3 child divs in each parent. So, all 9 divs are stacked together. But, made translateZ problematic on children. – Rehmat Jun 09 '22 at 11:54
  • 1
    CSS has now a dedicated property just for creating a new stacking context: `isolation` (https://developer.mozilla.org/en-US/docs/Web/CSS/isolation). – strarsis Apr 04 '23 at 17:48
5

Use non-static position along with greater z-index in child element:

.parent {
    position: absolute
    z-index: 100;
}

.child {
    position: relative;
    z-index: 101;
}
Priyanshu Chauhan
  • 5,297
  • 5
  • 35
  • 34
  • 1
    To elaborate a little on the "Use non-static position", the default behavior for elements that have no position property specified will default to "static." [Reading about Static](https://drafts.csswg.org/css-position-3/#valdef-position-static). This lacks functionality around things like the left, right, bottom and top until given a position, the same applies for z-index. – Clay Aug 21 '18 at 17:35
5

Try using this code, it worked for me:

z-index: unset;
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
Carlos Aleman
  • 101
  • 3
  • 11