2

I am trying to create an overlay with the following html and css

div.relative {
  position: relative;
  width: 400px;
  height: 200px;
  border: 3px solid #73AD21;
}

div.absolute {
  position: absolute;
  top: 50%;
  right: 0;
  width: 200px;
  height: 100px;
  border: 3px solid #73AD21;
  opacity: 1;
  background-color: blue;
}

.overlay {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.95);
  color: white;
  z-index: 1;
}

.div1 {
  animation: 750ms 1 forwards ani;
}

@keyframes ani {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}
<h2>
  position: absolute;</h2>

<p>An element with position: absolute; is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport, like fixed):</p>

<div class="relative">
  This div element has position: relative;
  <div class="div1">
    This is a div
    <div class="overlay">
      This div element has position: fixed;
    </div>
  </div>

  <div class="absolute">
    This div element has position: absolute;
  </div>
</div>

I am trying to create an overlay which is supposed to cover the entire area. However, the problem is, when I add animation it brings the absolute element to the front despite the fact that the animation has not been applied to it

kperveen
  • 61
  • 1
  • 6
  • works fine. The 750ms duration means 750 milliseconds. I put 7500 to test and I can see the animation properly working. It is just a problem of too quick animation – Lelio Faieta Sep 13 '18 at 09:31
  • 1000ms = 1 second – Lelio Faieta Sep 13 '18 at 09:32
  • @LelioFaieta there is not any problem with the animation, the problem I am facing is how the absolute div appears at the top of overlay when I add the animation to the code. – kperveen Sep 13 '18 at 10:13
  • Unfortunately, that's the way stacking order works - https://stackoverflow.com/questions/32513540/understanding-z-index-stacking-order – Paulie_D Sep 13 '18 at 11:21

3 Answers3

2

move your fixed element to the bottom, something like this

<div class="relative">
  This div element has position: relative;
  <div class="div1">
    This is a div
  </div>

  <div class="absolute">
    This div element has position: absolute;
  </div>
</div>
<div class="overlay">
  This div element has position: fixed;
</div>
Erwin Sanders
  • 296
  • 1
  • 12
  • Could you explain a bit? – Sankar Sep 13 '18 at 09:38
  • It's just a matter of z indexing. Moving the element change the drawing order of the elements. You can also use the same html file and just set the absolute element z-index to 1, as stated in the question comments. – Cristiano Sep 13 '18 at 09:44
  • I cannot move it to the bottom. It belongs to a complex structure. @Cristiano I tried z-index with the absolute div and its still not working – kperveen Sep 13 '18 at 10:04
0

Since my last answer isn't the best practice, try this one. So add these style to the div with class div1:

.div1 {
  animation: 750ms 1 forwards ani;
  position: relative;
  z-index: 2;
}

z-index didn't work because the positioned absolute element is on a different level than the fixed element, you need to define the z-index of the same level of element (in this case div1 and absolute). z-index also needs to have position relative attribute for it to work..

Erwin Sanders
  • 296
  • 1
  • 12
0

(To everyone looking for an answer) I found out the problem with this code. When we apply opacity animation with values less than 1 it creates another stacking context for that particular element and its children and it was stacking the overlay with respect to that stacking context. I solved it by removing animation-fill-mode to none since it won't affect any CSS afterwards.

kperveen
  • 61
  • 1
  • 6