Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pseudo element on parent hidden behind child image on IE8

Why in IE8, is the background color of a pesudo element flowing behind children of the parent? The text flows in front, but the background-color does not. Z-index did not seem to help any.

I haven't been able to determine if this is a bug in IE8 or not. It seems like this would have been a pretty common use-case, but I couldn't find many blog posts or SO questions related to it.

http://jsfiddle.net/VAg2E/

    <div id="parent">
       <img src="http://placehold.it/200x200">
    </div>


    #parent{ padding: 20px; }
    #parent:before{
       content: 'Behind the image';
       position: absolute;
       top: 0;
       left: 0;   
       width: 100px;
       height: 100px;
       background-color: red;
    }

Edit : A related Stack Overflow Question about Stacking Order

like image 651
Robert Avatar asked Dec 13 '12 19:12

Robert


People also ask

How do I add a background image to pseudo element?

Approach: The ::before pseudo selector places the background image before the selected element and if the selected element has a background color associated with it, we can use the z-index property to make the background image visible. Example: HTML.

Does Z-Index working on pseudo element?

Pseudo-elements are treated as descendants of their associated element. That means by default (with no positioning, z-index or opacity applied) they sit above their parent in the stacking order.

Do not inherit opacity from parent?

Opacity is not inherited, but because the parent has opacity that applies to everything within it. You cannot make a child element less transparent than the parent, without some trickery. Values are a number from 0 to 1 representing the opacity of the channel (the “alpha” channel).

Why is my Z-Index not working?

You set z-index on a static element By default, every element has a position of static. z-index only works on positioned elements (relative, absolute, fixed, sticky) so if you set a z-index on an element with a static position, it won't work.


2 Answers

This is definitely a bug in IE8; since your :before pseudo-element is positioned, it should create a new stacking context and always be drawn on top of the img unless you give it a negative z-index (even then, the entire element should be drawn behind it, not just its background).

This issue also seems specific to stacking between :before and :after pseudo-elements and replaced elements like img. It looks like IE8 is treating replaced content differently in terms of stacking, but whatever it is doing, it's definitely not conforming to the spec.

As you're probably aware, this is fixed in IE9.

like image 96
BoltClock Avatar answered Oct 16 '22 09:10

BoltClock


Have your exact same issue, the only thing you can do is force the stacking order via CSS and z-index. The only catch is that z-index is placed on child element starting from parent element, so you wont be able to do a proper logic order as #parent-element {z-index: 2} and #child-element {z-index: 1}, the z-index for the #child-element will just be set to level 1 as a separate stack order inside the #parent-element.

You can still set z-index for the #child-element with a -1 value, it will just get back the whole #parent-element stacking order.

So to recap:

#parent-element { z-index: 99;} /* or any arbitrary number fitting */
#child-element {z-index: -1;}

Also remember to give both elements a position: relative/absolute to enable the stacking order fo z-index

like image 38
Gruber Avatar answered Oct 16 '22 07:10

Gruber