Add a wrapper element
If editing the HTML source code is an option, add a wrapper element around the side content, and place it before the main text content. Then add position:relative
to the wrapper element rather than adding it to .listitem
. This moves the local stacking context (created at an inconvenient time in IE7) to an element where it does no harm.
HTML
<div class="container">
<div class="listitem">
<div class="wrapper">
<div class="layer">I'm a layer</div>
</div>
Some long content here bla bla blaaaaaaa baaaaaaaaaaa
</div>
....
</div>
CSS
.listitem {
/* position: relative; */ /* Removed here */
....
}
.wrapper {
position: relative; /* Added here */
height: 0;
}
jQuery
If editing the HTML source code isn't an option, there are a few options available using jQuery.
Take the basic approach presented by @avrahamcool (setting the z-index of each
.listitem
to a decreasingly small value). The downside to this approach is that it reverses the layering order of the side elements relative to one another, which may or may not be acceptable.Use jQuery to edit the HTML DOM so that the HTML is as shown above, using the related CSS changes as well. The downside to this approach is that the styling of the content won't render correctly until after the jQuery code has run. So the content may briefly appear broken when the page first loads.
Set
.container
toposition:relative
, rather than.listitem
. Use jQuery to calculate the relative position of each of.listitem
element within.container
, and set thetop
position of each side element based on this. This approach has the same downside as solution #2.In an extreme case, if even editing the JavaScript isn't an option, you could simulate solution #1 by adding a series of increasingly long CSS selectors with decreasing z-index values (as shown below). I don't recommend this approach, as it's wildly impractical, but it could be used as a last resort.
CSS
.listitem {z-index:99;}
.listitem + .listitem {z-index:98;}
.listitem + .listitem + .listitem {z-index:97;}
...
Conclusion
In short, the current HTML doesn't support this type of layout in IE7. The only elegant solution is to change the HTML source code to something that does support it. Any jQuery solution will tend to be awkward in one way or another.