Alex
Alex

Reputation: 2800

z-index for pseudo :after not placed under parent element

I am trying to sandwich an element between ':before' and ':after' pseudo elements, however despite setting z-index to -1 on the ':after' it still appears on top of the parent.

In the following example red should be on top, green in the middle, blue on the bottom.

See this fiddle: https://jsfiddle.net/zL1yz86f/2/

HTML:

<div></div> 

CSS:

div {
        background: green;
        position: relative;
        z-index: 10;
        width: 30%;
        height: 200px;
        float: left;
        margin-right: 3%}

    div:before {
        content: "";
        height: 0;
        width: 0;
        border-top: 200px solid red;
        border-right: 200px solid transparent;
        position: absolute;
        top: -20px;
        left: -10px;
        z-index: 20;
    }

    div:after {
        content: "";
        height: 200px;
        width: 200px;
        background: blue;
        position: absolute;
        top: -20px;
        left: -10px;
        z-index: -1;
    }

Upvotes: 0

Views: 615

Answers (1)

mac
mac

Reputation: 858

Remove the z-index of the parent

That will give the pseudo elements will have their own stacking context (as you have position: absolute already applied). Here's an updated fiddle. Hope this helps!

Upvotes: 2

Related Questions