Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the color of an svg element?

Tags:

css

colors

svg

I want to use this technique and change the SVG color, but so far I haven't been able to do so. I put this in the CSS, but my image is always black, no matter what.

My code:

.change-my-color {   fill: green; }
<svg>     <image class="change-my-color" xlink:href="https://svgur.com/i/AFM.svg" width="96" height="96" src="ppngfallback.png" /> </svg>
like image 824
Barbara Avatar asked Mar 07 '14 14:03

Barbara


People also ask

How do I change the color of an SVG image?

To simply change the color of the svg : Go to the svg file and under styles, mention the color in fill.

Can you change color of SVG?

How to change SVG element color? Upload your SVG into the icon editor, and you'll see a variety of color options in the left-hand column. You only need to choose the element color you want to change. Then choose the color you want to replace it with.


2 Answers

2020 answer

CSS Filter works on all current browsers

To change any SVGs color

  1. Add the SVG image using an <img> tag.
<img src="dotted-arrow.svg" class="filter-green"/> 
  1. To filter to a specific color, use the following Codepen(Click Here to open codepen) to convert a hex color code to a CSS filter:

For example, output for #00EE00 is

filter: invert(42%) sepia(93%) saturate(1352%) hue-rotate(87deg) brightness(119%) contrast(119%); 
  1. Add the CSS filter into this class.
    .filter-green{         filter: invert(48%) sepia(79%) saturate(2476%) hue-rotate(86deg) brightness(118%) contrast(119%);     } 
like image 82
Manish Menaria Avatar answered Oct 23 '22 15:10

Manish Menaria


To change the color of any SVG you can directly change the svg code by opening the svg file in any text editor. The code may look like the below code

<?xml version="1.0" encoding="utf-8"?>     <!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->     <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">     <svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"          width="500px" height="500px" viewBox="0 0 500 500" enable-background="new 0 0 500 500" xml:space="preserve">     <g>         <path d="M114.26,436.584L99.023,483h301.953l-15.237-46.416H114.26z M161.629,474.404h-49.592l9.594-29.225h69.223             C181.113,454.921,171.371,464.663,161.629,474.404z"/>     /*Some more code goes on*/     </g>     </svg> 

You can observe that there are some XML tags like path, circle, polygon etc. There you can add your own color with help of style attribute. Look at the below example

<path fill="#AB7C94" d="M114.26,436.584L99.023,483h301.953l-15.237-46.416H114.26z M161.629,474.404h-49.592l9.594-29.225h69.223                 C181.113,454.921,171.371,464.663,161.629,474.404z"/> 

Add the style attribute to all the tags so that you can get your SVG of your required color

Edit: As per Daniel's comment, we can use fill attribute directly instead of fill element inside style attribute

like image 28
sushant047 Avatar answered Oct 23 '22 15:10

sushant047