zmccord
zmccord

Reputation: 2612

How to get mix-blend-mode to apply to only the box shadow of a sticky element?

I have a UI that has some elements with position: sticky; on them. They have box shadows so that they look like they're floating over the text beneath them. I want to apply a blend mode to the box shadows, and that's causing me some problems.

What I want is to make it so that shadows never really get darker when they overlap. Currently, in mildly pathological cases, the sticky elements can pile up on top of each other, and the overlapping shadows become unattractively dark; I'd like to fix this. (I am also trying to mitigate those pathological cases, but I still want to fix the shadows.)

I can get the shadows to look the way I want using mix-blend-mode: darken;, but it's not playing well with position: sticky;.

Here is a JSBin of a few things I've tried. Each of the six examples contains identical markup; only the CSS is different. Sorry about the painfully teal background — it makes the problem easier to identify.

My interpretation so far is that the sticky elements having their own stacking context means that, instead of the shadow blending with what's behind, the shadow blends with the element, and then the element + shadow blends with what's behind, which vitiates my mix-blend-mode: darken; on only the shadow since that mix-blend-mode: darken; is being applied to the blend of the shadow with the element, not the shadow to the background.

I think I understand the problem, but I don't see any great solutions. I suppose I could double all the sticky elements, having one with a shadow and mix-blend-mode: darken and then one in front of it with no shadow and mix-blend-mode: normal, but… is there a better way?

Upvotes: 0

Views: 92

Answers (0)

Related Questions