Opacity of div creates unexpected results and blends content with image
Asked Answered
D

2

4

Can someone please explain why the second & third divs in the following snippet appear to blend with the image? For clarity, the question here is about why the opacity of the content element seems to affect layer order.

The image is an animated gif with no transparency. In the upper div everything seems to behave as expected. The image swirls around with it's white background blocking the text content of the div below it.

In the lower two divs I've decreased opacity and somehow this creates the effect of the text being superimposed over the image. This is further illustrated in the third div where I've set a background color on the wrapper. The whole thing is just confusing and unexpected for me.

div.wrapper {
  position: relative;
  padding: 15px;
  border: 1px solid #ddd;
}
img.loader {
  position: absolute;
  left: calc(50% - 32px);
  top: calc(50% - 32px);
}
div.content-b,
div.content-c {
  opacity: .5;
}
div.wrapper-c {
  background-color: #bbb;
}
<div class="wrapper">
  <img class="loader" src="//zuma-design.com/shared/so/loading-large.gif" />
  <div class="content-a">Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas
    sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora
    incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate
    velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</div>
</div>
<div class="wrapper">
  <img class="loader" src="//zuma-design.com/shared/so/loading-large.gif" />
  <div class="content-b">Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas
    sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora
    incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate
    velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</div>
</div>
<div class="wrapper wrapper-c">
  <img class="loader" src="//zuma-design.com/shared/so/loading-large.gif" />
  <div class="content-c">Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas
    sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora
    incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate
    velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</div>
</div>
Digestif answered 21/12, 2016 at 5:40 Comment(6)
it's happened because of your opacityJessiajessica
@dreamhunter, the question is about why opacity seems to affect layer order.Digestif
try giving background color in rgb, like this: div.wrapper-c {background-color: rgba(0,0,0,0.3);}Vorous
sorry I think I didn't understand your question wellVorous
@billynoah not clear what you would like to achieve ,can you show some image or can you explore more?Jessiajessica
@dreamhunter - This question is not about "achieving" anything, it is seeking an explanation for unexpected behavior.Digestif
L
4

The stacking along the z-axis rules say that:

  1. The order of sibling - based on which comes is first taken into consideration

  2. A positioned sibling element is stacked over the static element - so the img should be over the div ideally.

  3. Other properties like position which affects stacking context includes opacity, transform.

See these excerpts from MDN: stacking context

A stacking context is formed, anywhere in the document, by any element which is either

  • the root element (HTML),

  • positioned (absolutely or relatively) with a z-index value other than "auto",

  • a flex item with a z-index value other than "auto",that is the parent element display: flex|inline-flex,

  • elements with an opacity value less than 1. (See the specification for opacity),

  • elements with a transform value other than "none",

  • elements with a mix-blend-mode value other than "normal",

  • elements with a filter value other than "none",

  • elements with a perspective value other than "none",

  • elements with isolation set to "isolate",

  • position: fixed

  • specifying any attribute above in will-change even if you don't specify values for these attributes directly (See this post)

  • elements with -webkit-overflow-scrolling set to "touch"

You can fix it by putting the img below the div - see demo below:

div.wrapper {
  position: relative;
  padding: 15px;
  border: 1px solid #ddd;
}
img.loader {
  position: absolute;
  left: calc(50% - 32px);
  top: calc(50% - 32px);
}
div.content-b,
div.content-c {
  opacity: .5;
}
div.wrapper-c {
  background-color: #bbb;
}
<div class="wrapper">
  
  <div class="content-a">Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas
    sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora
    incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate
    velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</div>
  <img class="loader" src="//zuma-design.com/shared/so/loading-large.gif" />
</div>
<div class="wrapper">

  <div class="content-b">Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas
    sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora
    incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate
    velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</div>
    <img class="loader" src="//zuma-design.com/shared/so/loading-large.gif" />
</div>
<div class="wrapper wrapper-c">

  <div class="content-c">Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas
    sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora
    incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate
    velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</div>
    <img class="loader" src="//zuma-design.com/shared/so/loading-large.gif" />
</div>
Lineal answered 21/12, 2016 at 5:49 Comment(2)
Thanks. Yes it's confusing - and again, I'm not looking to change anything about the result - just want an explanation. I can't understand why changing opacity seems change the layer order.Digestif
Wow.. I really had no idea that stacking order was affecting by so much more than position and z-index. Thanks for the explanation and authoritative link.Digestif
X
3

You should give higher z-index to img then it sit above all.

z-index: 9999;

If an element with opacity less than 1 is not positioned, implementations must paint the layer it creates, within its parent stacking context, at the same stacking order that would be used if it were a positioned element with ‘z-index: 0’ and ‘opacity: 1’.

Xylidine answered 21/12, 2016 at 5:47 Comment(2)
Why should I do that? Getting the image to sit on top is not my goal.Digestif
thanks @Yonas. That helps explain it. Upvoted yours but accepting the more thorough answer.Digestif

© 2022 - 2024 — McMap. All rights reserved.