9

Is it possible to blend only the drop-shadow of an element with the color of the element it is overlapping?

For example: I have one element overlapping the other. The element on top has a light-grey drop shadow. The element below is black. I don't want any blend applied to either of the elements themselves but want the drop-shadow of the overlapping element to blend with color of the element below, creating a darker grey where the shadow falls on the overlapped element.

If I wanted to apply the effect to the entire element including the drop-shadow then this can be achieved using mix-blend-mode: multiply. I effectively want to use mix-blend-mode on the drop-shadow only - can this be done?

Sivaprakash B
  • 163
  • 1
  • 15
mannystar
  • 93
  • 1
  • 1
  • 5
  • In your question, you stated that you want the grey shadow to make darker grey where it overlaps with black. I just wanted to point out that `black` * anything = `black`. – laptou Oct 16 '18 at 15:20
  • Not strictly black underneath, just phrased that way for simplicity but technically it's an extremely dark grey rather than black – mannystar Oct 16 '18 at 15:42

1 Answers1

12

You can't do this, but what you can do is apply the drop-shadow to a pseudo-element that is the same size and uses a multiply blend mode.

.above {
    position: absolute;
    background: green;
    width: 100px;
    height: 100px;
}

.above::before {
    box-shadow: 3pt 3pt 0 0 dodgerblue;
    mix-blend-mode: multiply;
    content: "";

    position: absolute;
    top: 0;
    right: 0;
    left: 0;
    bottom: 0;
}

.below {
    position: absolute;
    top: 50px;
    left: 50px;

    width: 100px;
    height: 100px;

    background: red;
}
<div class="below">
</div>
<div class="above">
</div>

I used these colours for illustrative purposes, but you can replace them with your own.

BarryCap
  • 326
  • 3
  • 11
laptou
  • 6,389
  • 2
  • 28
  • 59
  • 1
    Awesome, that works perfectly - thank you very much @laptou! – mannystar Oct 16 '18 at 15:48
  • +1 this seems to work with color **names** but not hex values. i.e. `box-shadow: grey` works, but `box-shadow: #dddddd` does *not*. Is that a known limitation? (I know it's an old question.) – Scott Oct 12 '22 at 21:56