I was trying to create a container with a pseudo-element (::before
, in this case) where on the parent element I would have a linear-gradient
background with some transparency and on the pseudo-element, I would have the color.
The main reason to that here is that I also wanted to have some backdrop-filter
applied to it and by doing in this way, I could apply an opacity
to the pseudo-element only (in this specific case, I couldn't use a background color with some alpha
, so that's why the whole workaround here).
I also wanted to only apply the opacity
when the browser actually supported backdrop-filter
, so these styles are contained within a @supports
block.
So, here is when things get weird. This element is position: fixed
, and I noticed that for the browsers not supporting backdrop-filter
, the linear-gradient
was not being applied.
Here's an example of what I mean (https://jsfiddle.net/8aeczvf2/). On this example I have some divs with the same style:
.container {
position: relative;
top: 0;
height: 100px;
width: 200px;
background-image: linear-gradient(rgba(0,0,0,0),rgba(0,0,0,0.5));
}
.container::before {
content: "";
position: absolute;
width: 100%;
height: 100%;
background-image: url('https://lorempixel.com/600/400/');
z-index: -1;
}
When .container
has position: relative
, it works as expected. But, if I change it to position: fixed
, then the pseudo-elements background seems to be applied on top of the parent's. Same goes if I only apply a
z-indexto
.container. It works again on the last example, but because
::before` has some opacity applied to it.
Is this expected by the browser to render the pseudo-elements differently depending on the type of position applied to the parent?