Home > Back-end >  Change part of SVG color with css
Change part of SVG color with css

Time:09-28

I have an SVG image and I'm looking to change part of its color. The SVG will always consist of 2 colors, i.e. black & yellow

I'm looking to change the yellow color with a css class, so I could switch to another color theme easily without creating all the svg buttons in the yellow version.

Is this at all possible? I can't seem to find much online about this..

The SVG is set on span/div's using a class with background-image

If i implement the tag with SVG then I can change the color with css. But I'm looking to use it as a class if possible (and the svg should not be directly in the html)

<svg>
 <use xlink:href="#robot" id="robot-1" />
 </svg>

Turorial: https://tympanus.net/codrops/2015/07/16/styling-svg-use-content-css/

JsFiddle: https://jsfiddle.net/wahjvmnq/

CodePudding user response:

You can change all the paths using --secondary-color to be fill: currentColor and then wrap the SVG with an HTML element that has a CSS class or styles applied to it that updates the color property.

After updating the path's in the SVG that currently use --secondary-color:

<div style="color:red;">
  <svg>
    <use xlink:href="#robot" id="robot-1" />
  </svg>
</div>

Here is an example on jsFiddle where the yellow has been changed to red.

You can not override the SVG colors with CSS unless it is part of the HTML document.

  • Related