A bona fide CSS trick from Kirupa Chinnathambi here. To match a colored shadow with the colors in the background-image
of an element, you inherit
the background in a pseudo-element, kick it behind the original, then blur and filter it.
.colorfulShadow {
position: relative;
}
.colorfulShadow::after {
content: "";
width: 100%;
height: 100%;
position: absolute;
background: inherit;
background-position: center center;
filter: drop-shadow(0px 0px 10px rgba(0, 0, 0, 0.50)) blur(20px);
z-index: -1;
}
Negative z-index
is always a yellow flag for me as that only works if there are no intermediary backgrounds. But the trick holds. There would always be some other way to layer the backgrounds (like a <span>
or whatever).
For some reason this made me think of a demo I saw (I can’t remember who to credit!). Emojis had text-shadow
on them, which really made them pop. And those shadows could also be colorized to a similar effect.
I did a similar trick using emoji here: https://stackoverflow.com/a/50012480/8620333 (using data-attribute to avoid duplicating the emoji)
PS: for the negative z-index trick, you can rely on both pseudo elements like this: https://codepen.io/t_afif/pen/RwpbgYK and you no more need the negative value.