Search code examples
svgcolorsstroke

Shadow Root? Calling out stroke color


I am calling out a stroke in an SVG, it works if I don't put any class on the style. But I need to put it there because needed to be fixable for the end user to pick any color they want.

symbol#icon-arrow{
  stroke: #ff6600;
  } /*this is working*/
  
 
 .icon-orange symbol#icon-arrow{
  stroke: #99CA3D;
  } /*this is not working, but this is what I need*/
  
  
<div id="icon-load" style="display:none;"></div>
              <svg xmlns="http://www.w3.org/2000/svg" width="0" height="0">
                  <symbol id="icon-arrow" viewBox="0 0 24 24" fill="none" stroke="#000000" stroke-width="3" stroke-linecap="square" stroke-linejoin="arcs" >
                      <path class="arrow" d="M12 19V6M5 12l7-7 7 7"/>
                  </symbol>
              </svg>
              
              

              <a href="#" class="icon">
                  <svg class="icon icon-orange"><use xlink:href="#icon-arrow"></use></svg>
              </a>


Solution

  • As @enxaneta said, you have to style the <use> element, and let the colour percolate down to the symbol.

    But you'll first need to remove the stroke attribute from the symbol. Otherwise that presentation attribute will override the colour you want it to inherit.

    .icon-orange use {
      stroke: #ff6600;
    }
      
    .icon-green use {
      stroke: #99CA3D;
    }
    <div id="icon-load" style="display:none;"></div>
    
    <svg xmlns="http://www.w3.org/2000/svg" width="0" height="0">
        <symbol id="icon-arrow" viewBox="0 0 24 24" fill="none" stroke-width="3" stroke-linecap="square" stroke-linejoin="arcs" >
            <path class="arrow" d="M12 19V6M5 12l7-7 7 7"/>
        </symbol>
    </svg>
                  
    <a href="#" class="icon">
        <svg class="icon icon-orange"><use xlink:href="#icon-arrow"></use></svg>
    </a>
    
    <a href="#" class="icon">
        <svg class="icon icon-green"><use xlink:href="#icon-arrow"></use></svg>
    </a>