1

I can't work out why this z-index isn't working on my page.

Here is a working example: https://jsfiddle.net/daeyqv31/

#login_modal_wrapper {
  background: #eee;
  background: red;/*rgba(0,0,0,0.7);*/
  width: 100%; height: 100%;
  position: fixed; top: 0; left: 0;
  z-index: 10003;
}

#login_modal {
  z-index: 10002;
  background: #eee;
  border: 2px solid #092834;
  border: 2px solid rgba(9,40,52,0.6);
  border-radius: 20px;
  width: 700px;
  height: 240px;
  position: fixed;
  top: 50%;
  left: 50%;
  margin-left: -350px;
  margin-top: -120px;
}
#login_modal_wrapper.over {
    z-index: 100003; // this needs to go OVER login_modal
}
#ajax_spinner {
  width: 34px;
  height: 34px;
  position: fixed;
  top: 50%;
  left: 50%;
  margin-left: -17px;
  margin-top: -17px;
  z-index: 200000;
}

However, I then also want to be able to force the #login_modal_wrapper div to go OVER #login_modal. So, I've got this (just a basic example);

https://jsfiddle.net/daeyqv31/1/

So this:

<div id="ajax_spinner">
    loading icon
</div>
<div id="login_modal_wrapper">
  <div id="login_modal"></div>
</div>

becomes:

<div id="ajax_spinner">
    loading icon
</div>
<div id="login_modal_wrapper" class="over">
  <div id="login_modal"></div>
</div>

...yet the z-index doesn't seem to be honored (it still shows #login_modal on the top, when it should in fact be behind #login_modal_wrapper

UPDATE: As commented on below, there is no way to apply a higher z-index to a div that is a parent. To get around this, I just jiggled the HTML around a little:

<div id="ajax_spinner"><img src="/images/new/ajax-loader.gif" /></div>
<div id="login_modal"></div>
<div id="login_modal_wrapper"></div>

This now works exactly as expected :)

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
Andrew Newby
  • 4,941
  • 6
  • 40
  • 81
  • 1
    Possible duplicate of this : http://stackoverflow.com/questions/4456234/cant-get-z-index-to-work-for-div-inside-of-another-div – Vincent G Mar 30 '16 at 12:47
  • @VincentG - ah man, that was it! Pulling the #login_modal out of #login_modal_wrapper worked a charm. I wasn't aware of that limitation. Thanks! – Andrew Newby Mar 30 '16 at 12:53
  • 1
    This may help with your understanding of how `z-index` works: http://stackoverflow.com/a/32515284/3597276 – Michael Benjamin Mar 30 '16 at 16:19

1 Answers1

2

Because #login_modal_wrapper contains the other <div> element, z-index could not work. You could try:

<div id="login_modal_wrapper">
  <div class="div-above"></div>
  <div id="ajax_spinner">
    loading icon
  </div>
  <div id="login_modal"></div>
</div>

And the CSS:

.div-above {
  width: 100%;
  height: 100%;
  background: blue;
  position:fixed;
  z-index: 10;
}

Here is the updated fiddle.

Lulylulu
  • 1,254
  • 8
  • 17
  • Thanks - just tried that (after seeing Vincents link to another SO post), and that worked a charm :) Amazed I've never come across this issue before! – Andrew Newby Mar 30 '16 at 12:54