Recently, I came across an interesting issue while working with CSS, particularly with negative margins to stack elements on top of each other.
I am aware of the natural stack order in which elements overlap, where the later element always appears on top without using relative or absolute positioning.
Here are a couple of questions that I encountered:
Question1: Why does the later element go under the image if the former element contains an image?
Question2: Furthermore, why does the later element go over the former element when it has an opacity value other than 1 (is it somehow resetting the natural order)?
Below is the HTML and SCSS code snippets for reference:
<div class="box sample1">
<img src="http://fillmurray.com/100/100" alt="">
</div>
<div class="box sample1-2">opacity: 1</div>
<div class="box sample1-3">
<img src="http://fillmurray.com/100/100" alt="">
</div>
<div class="box sample1-4">opacity: .9</div>
For a detailed explanation and a demo, please visit the following link: https://jsfiddle.net/nori2tae/4w62t746/8/
I would appreciate some insights or explanations on this matter. Thank you!