4

I want to have a drop shadow on a transparent SVG element.

I have tried using all different kinds of filters but to no avail. css3 filters on the svg element(filter: drop-shadow(0 -6mm 4mm rgb(160, 0, 210));), the new dropshadow filter(<feDropShadow>), the old filters:

<filter xmlns="http://www.w3.org/2000/svg" id="dropshadow" height="130%">
      <feGaussianBlur in="SourceAlpha" stdDeviation="3"/> 
      <feOffset dx="2" dy="2" result="offsetblur"/>
      <feComponentTransfer>
        <feFuncA type="linear" slope="0.2"/>
      </feComponentTransfer>
      <feMerge> 
        <feMergeNode/>
        <feMergeNode in="SourceGraphic"/> 
      </feMerge>
    </filter>

This can be achieved using css3 box-shadow Codepen

I expect to have a dropshadow on a transparent element but the actual transparent element clips over the dropshadow(so the element itself is transparent, but has an neon like outer glow)

I want to be able to control:

  • blur
  • spread
  • color

Any help will be appreciated :)

2 Answers 2

5

You can't do this if the original is a fully transparent shape - because of reasons - but you can do this starting from an almost completely transparent original shape and end up with a fully transparent shape surrounded by a normal drop shadow.

Draw your shapes with 1% fill-opacity. When you pull those into a filter, multiply their alpha by 100 using a colormatrix - and use that as the basis for your dropshadow. You won't end up using the original 1% opacity shape in your final version because if you use the "out" operator - this discards the contents of anything that overlaps with the original (processed) shape.

svg {
  background: #33D; 
}
<svg width="500px" height="400px">
<defs>
  <filter id="trans-shadow">
  <feColorMatrix type="matrix" values="1 0 0 0 0 
                                       0 1 0 0 0 
                                       0 0 1 0 0 
                                       0 0 0 100 0"
                                       result="boostedInput"/>
                                       
  <feGaussianBlur stdDeviation="5"/>
  <feComposite operator="out" in2="boostedInput"/>
  </filter>
</defs>


<circle filter="url(#trans-shadow)" x="100" y="100" r="050" cx="150" cy="150" fill="black" fill-opacity="0.01" />


</svg>

I'm assuming that these shapes are not always drop-shadowed, so you want their non drop-shadowed versions to be as transparent as possible. If these shapes are never displayed without a drop-shadow then you can skip a step and just draw these shapes in black originally and still use the "out" to discard them. Like so:

svg {
  background: #33D; 
}
<svg width="500px" height="400px">
<defs>
  <filter id="trans-shadow">                                          
  <feGaussianBlur stdDeviation="5"/>
  <feComposite operator="out" in2="SourceGraphic"/>
  </filter>
</defs>


<circle filter="url(#trans-shadow)" x="100" y="100" r="050" cx="150" cy="150" fill="black" />


</svg>

1

I made a codepen for you that may be helpfull I played around with it myself and i think you can work from here with what you want yourself. here is the html:

    <div id="background">
     <div id="mybox">
<svg viewBox="0 0 142.71 92.903" enable-background="new 0 0 142.71 92.903" xml:space="preserve">
<path fill="none" stroke="#000000" stroke-width="3" stroke-miterlimit="10" d="M141.348,91.677H1.71v-90h139.638v58
    c0,0,0,6.194,0,9S141.348,91.677,141.348,91.677z"/>
</svg>
  </div>
</div>

and the css:

    #mybox{
  position: absolute;
  top: 20%;
  left: 40%;
}
svg{
box-shadow: 0 0 341px 71px rgb(137, 105, 148, 0.8);
width: 80%;
cursor: move;
}


#background {
  margin-left: 10%;
  max-width: 100%;
  height: 600px;
  background-image: url('https://images.unsplash.com/photo-1519681393784-d120267933ba?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80');
  background-repeat: no-repeat;
2
  • First of all I really want to thank you. It looked like you spend a lot of time on the codepen example. But I need the box shadow on individual SVG elements(path, rect e.g.) not on the svg root element. But I still thank you so much!
    – Lukas Chen
    Commented Mar 26, 2019 at 4:07
  • You're welcome, im sorry it wasn't any help. Feel free to use the code as you like!
    – Yilmaz
    Commented Mar 28, 2019 at 15:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.