I'm experimenting (on current MacOS Firfox) with this retro effect on images, but I'm running into a problem with background colors and z-index: -1
.
Here's a stripped down example:
.retro {
-webkit-box-shadow: inset 0px 0px 100px rgba(0, 0, 20, 1);
background: -webkit-linear-gradient(top, rgba(255, 145, 0, 0.2) 0%, rgba(255, 230, 48, 0.2) 60%), -webkit-linear-gradient(20deg, rgba(255, 0, 0, 0.5) 0%, rgba(255, 0, 0, 0) 35%);
display: table;
}
.retro img {
filter: sepia(20%) brightness(90%) contrast(130%);
position: relative;
z-index: -1;
width: 100%;
}
.background {
background-color: blue;
}
<html>
<body>
<section class="background">
<picture class="retro">
<img src="https://cdn2.wanderlust.co.uk/media/1028/cropped-shutterstock_497799013.jpg" />
</picture>
</section>
</body>
</html>
Apparently, the z-index: -1
stacks the img
behind the section
and not behind the picture
(but in front of section
).
This only happens when section
has a background color assigned, once removed, things look the way they should:
.retro {
-webkit-box-shadow: inset 0px 0px 100px rgba(0, 0, 20, 1);
background: -webkit-linear-gradient(top, rgba(255, 145, 0, 0.2) 0%, rgba(255, 230, 48, 0.2) 60%), -webkit-linear-gradient(20deg, rgba(255, 0, 0, 0.5) 0%, rgba(255, 0, 0, 0) 35%);
display: table;
}
.retro img {
filter: sepia(20%) brightness(90%) contrast(130%);
position: relative;
z-index: -1;
width: 100%;
}
.background {
background-color: blue;
}
<html>
<body>
<section>
<picture class="retro">
<img src="https://cdn2.wanderlust.co.uk/media/1028/cropped-shutterstock_497799013.jpg" />
</picture>
</section>
</body>
</html>
I've gone over the z-index
basics with stacking context etc, but I don't see how any of this applies here.
Any ideas? Thanks a bunch!
UPDATE
It works if I force a new stacking context on section
, but not when a new stacking context is forced on picture
. Why is that?
And why does the z-index: -1
on img
have any effect to begin with even though according to the MDN docs the presence of filter
should force a new stacking context as well?
.retro {
box-shadow: inset 0px 0px 100px rgba(0, 0, 20, 1);
background: -webkit-linear-gradient(top, rgba(255, 145, 0, 0.2) 0%, rgba(255, 230, 48, 0.2) 60%), -webkit-linear-gradient(20deg, rgba(255, 0, 0, 0.5) 0%, rgba(255, 0, 0, 0) 35%);
display: flex;
width: 300px;
}
.retro img {
filter: sepia(20%) brightness(90%) contrast(130%);
z-index: -1;
width: inherit;
}
.background {
background-color: blue;
position: relative;
z-index: 0;
}
<html>
<body>
<section class="background">
<picture class="retro">
<img src="https://cdn2.wanderlust.co.uk/media/1028/cropped-shutterstock_497799013.jpg" />
</picture>
</section>
</body>
</html>