drop-shadow and background-color on transparent image

510 Views Asked by At

Setting a background causes the drop-shadow filter to ignore the free form and applies it only to the images rectangular outline.

img {
  background: lightgreen;
  filter: drop-shadow(0 0 10px red);
}

See https://jsbin.com/zoseru/2/edit?css,output

1

There are 1 best solutions below

0
On

This can be achieved using SVG filters:

<!-- in your html -->
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" class="filterdef">
  <filter id="background" x="0" y="0" width="100%" height="100%">
    <feFlood flood-color="lightgreen" result="bg"></feFlood>
    <feComposite in2="bg" in="SourceGraphic"></feComposite>
  </filter>
</svg>

// css
img {
  filter: drop-shadow(0 0 10px red) url(#background);
}

Please note the order of the filters is important.

https://jsbin.com/zoseru/3/edit?html,css,output