Dani Dani - 23 days ago 10
CSS Question

Gradients hidden using SVG symbols

I am using SVG symbols this way, but the

display:none
of the SVG is hidden the gradients of the graphic. Any idea?

In the example below, there should be two circles, but the red one is hidden:



<svg xmlns="http://www.w3.org/2000/svg" style='display:none' >
<defs>
<style>.red-gradient{fill:url(#gradient)}</style>
<linearGradient id="gradient" x1="0" x2="0" y1="0" y2="1">
<stop offset="0%" stop-color="red"/>
<stop offset="100%" stop-color="darkred"/>
</linearGradient>
</defs>
<symbol id="mysymbol" viewBox="0 0 100 100">
<circle class="red-gradient" cx="0" cy="50" r="50" />
<circle fill="green" cx="100" cy="50" r="50" />
</symbol>
</svg>

<svg><use xlink:href="#mysymbol" /></svg>




Answer

Instead of display: none, you can just use width="0" height="0".

<svg xmlns="http://www.w3.org/2000/svg" width="0" height="0">
  <defs>
    <style>.red-gradient{fill:url(#gradient)}</style>
    <linearGradient id="gradient" x1="0" x2="0" y1="0" y2="1">
       <stop offset="0%" stop-color="red"/>
       <stop offset="100%" stop-color="darkred"/>
    </linearGradient>
  </defs>
  <symbol id="mysymbol" viewBox="0 0 100 100">
    <circle class="red-gradient" cx="0" cy="50" r="50" />
    <circle fill="green" cx="100" cy="50" r="50" />
  </symbol>
 </svg>

<svg><use xlink:href="#mysymbol" /></svg>

Comments