1

In the following code, I have 3 divs all are position: absolute.

I want div1 between div2 and div3. To achieve this I have given z-index as follows: div1 (0), div2 (-1), div3(1) but after doing this, I am getting div1 on the top instead of middle. I understand this is happening because of how stacking context work and div3 is stacking wrt div2.

My question is: how can I put div1 between div2 and div3.

Pls note I can't change following things:

  1. I can't change HTML code
  2. all divs must be position: absolute

div {
    height: 50px;
    position: absolute;
}

.div1 {
    background: red;
    z-index: 0;
    width: 50px;
}

.div2 {
    width: 100px;
    background: green;
    z-index: -1;
}

.div3 {
    top: 20%;
    width: 75px;
    background: blue;
    z-index: 1;
}
<div class="div1">
    div1
</div>

<div class="div2">
    div2

    <div class="div3">
        div3
    </div>
</div>
s.kuznetsov
  • 14,870
  • 3
  • 10
  • 25
dasfdsa
  • 7,102
  • 8
  • 51
  • 93

0 Answers0