IE6 and IE7 absolute positioned div on top of multiple relative divs

Problem

Is it possible to make multiple absolute-positioned divs overlap multiple relative-positioned divs in IE6 & IE7?

See this jsFiddle for more information: http://jsfiddle.net/btker/1/

HTML

<div class="wrapper">
    <div class="relative_div">Relative div.
        <div class="absolute_div">This div have absolute position and is placed in a relative positioned div. This div should always be on top of all relative divs.</div>
    </div>
</div>

<div class="wrapper">
    <div class="relative_div">Relative div.
        <div class="absolute_div">This div have absolute position and is placed in a relative positioned div. This div should always be on top of all relative divs.</div>
    </div>
</div>

CSS

.wrapper{
height: 100%;
width: 100%;
}

.relative_div {
    height: 75px;
    width: 200px;
    border: 1px solid #000;
    padding: 10px;
    background: #e6e6e6;
    margin: 0 0 35px 0;
    position: relative;
}
.absolute_div {
    height: 100px;
    width: 250px;
    border: 1px solid #000;
    background: #c6c6c6;
    padding: 10px;
    position: absolute;
    top: 40px;
    left: 100px;
    z-index: 100;
}

There are two relative <div>s (placed in identical wrappers) containing each one a absolute <div> that overlap all the relative <div>s. This works great without any problems in updated versions of Chrome, Firefox etc, the absolute <div> with z-index is always placed on top.

In IE6 and IE7 this is not working. The different between this problem and the standard "dropdown in header display its menus behind the page content" is that in those situations its often fixed by give the parent element of that specific menu other z-index etc. In this case the both absolute <div>s are put in identical <div>s.

Can this be solved so the absolute <div>s are always on top of all relative <div>s in IE6 & IE7? Conditional comments for IE can be used to make the solution cross-browser.

Problem courtesy of: Vode

Solution

It is possible but only by decreasing the z-index of the second .wrapper or increasing the z-index of the first .wrapper.

On a simple level, each positioned element with a non-auto z-index creates a new stacking context, although there are other circumstances in which a new stacking context is created - see The stacking context.

The problem is one that affects IE <= 7, from quirksmode.org

In Internet Explorer positioned elements generate a new stacking context, starting with a z-index value of 0. Therefore z-index doesn't work correctly.

CSS

.wrapper{
    height: 100%;
    width: 100%;
}

.lower {
    position: relative;
    z-index: -1;
}

.higher {
    position: relative;
    z-index: 2;
}

.relative_div {
    height: 75px;
    width: 200px;
    border: 1px solid #000;
    padding: 10px;
    background: #e6e6e6;
    margin: 0 0 35px 0;
    position: relative;

}
.absolute_div {
    height: 100px;
    width: 250px;
    border: 1px solid #000;
    background: #c6c6c6;
    padding: 10px;
    position: absolute;
    top: 40px;
    left: 100px;
    z-index: 1;
}

HTML

<div class="wrapper"> <!-- add higher class here -->
  <div class="relative_div">Relative div.
      <div class="absolute_div">This div have absolute position and is placed in a relative positioned div. This div should always be on top of all relative divs.</div>
  </div>
</div>
<div class="wrapper"> <!-- or add lower class here -->
  <div class="relative_div">Relative div.
      <div class="absolute_div">This div have absolute position and is placed in a relative positioned div. This div should always be on top of all relative divs.</div>
  </div>
</div>
Solution courtesy of: andyb

Discussion

There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.