Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the color of an svg image using CSS [duplicate]

Tags:

html

css

svg

I have the following svg image in my html file:

<img class="svg" src="my-image-link.svg">

Now, I am trying to change the color using this css code:

.svg path {
    fill: black;
}

However, nothing changes. What is the correct way to change the color of an svg image's path using css? Thanks!

like image 650
darkhorse Avatar asked Oct 31 '16 13:10

darkhorse


People also ask

Can I change color of SVG in CSS?

You can't change the color of an image that way. If you load SVG as an image, you can't change how it is displayed using CSS or Javascript in the browser. If you want to change your SVG image, you have to load it using <object> , <iframe> or using <svg> inline.

Which property is used to change the color of an SVG using CSS?

The fill property is a presentation attribute used to set the color of a SVG shape.


1 Answers

You have to use an inline SVG, where the path element is inside the HTML, as opposed to linking to an external SVG.

Example: https://jsfiddle.net/fznuckb0/1/

.path-name {
    fill: tomato;
    fill-rule: evenodd;
}
like image 95
rorymorris89 Avatar answered Sep 30 '22 06:09

rorymorris89