bosconvertpro convert caseBosConvertPro

How can I change the color of an 'svg' element?

Abbos Nurgulshanov
18 February 2024
242 times viewed

I want to use this technique and change the SVG color, but so far I haven't been able to do so. I use 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>
Answers
Abbos Nurgulshanov
18 February 2024

The problem here is that CSS styles are applied to 'svg' elements only and not their children (in this case, the 'image' element). If you want to change the color of the 'image' element, you can use the CSS filter property.

.change-my-color-image {
filter: hue-rotate(120deg);
}



To change the color of the SVG image, replace 'hue-rotate' with 'saturate' and set the value to 0.

.change-my-color-image {
filter: saturate(0);
}



Alternatively, you can set a fill style directly in the SVG markup.

<svg>
  <image class="change-my-color" xlink:href="https://svgur.com/i/AFM.svg" width="96" height="96" src="ppngfallback.png" fill="green" />
</svg>



Abbos Nurgulshanov
18 February 2024

The CSS property fill applies to shapes in an SVG document, not images. To change the color of an image in an SVG document, you should use the filter property, with the hue-rotate filter function. For example, the CSS code below would make the image in your SVG document green:

.change-my-color {
filter: hue-rotate(120deg);
}

 

Abbos Nurgulshanov
18 February 2024

The fill property only works on shape elements in SVG. To change the color of an image, you have to use the filter property.

Here is an example of how you can change the color of an SVG image using a filter:

.change-my-color {
filter: invert(100%);
}



In this example, the filter property is set to invert(100%), which will invert the colors of the image. You can use other filter values to achieve different color effects. For example, you can use the hue-rotate filter to change the hue of the image, or the saturate filter to change the saturation of the image.

Abbos Nurgulshanov
18 February 2024

To change the color of an 'svg' element, you can use the 'fill' property. However, in your code, you are trying to change the color of an 'image' element inside an 'svg' element. To change the color of an 'image' element, you can use the 'filter' property.

Here is an example of how to change the color of an 'image' element inside an 'svg' element using the 'filter' property:



.change-my-color {
filter: invert(1);
}



This code will invert the colors of the 'image' element, making it appear green.

Note that the 'filter' property can be used to achieve a variety of effects, including changing the brightness, contrast, and saturation of an image. For more information on the 'filter' property, please see the following resources:

* [MDN Web Docs: filter](https://developer.mozilla.org/en-US/docs/Web/CSS/filter)
* [W3Schools: CSS Filters](https://www.w3schools.com/css/css3_filters.asp)

Abbos Nurgulshanov
18 February 2024

The reason the image is always black is because the fill property is not supported on the element in SVG. To change the color of an image in SVG, you need to use the filter property.

Here is an example of how you can change the color of an image using the filter property:

.change-my-color {
filter: hue-rotate(90deg);
}



This will change the color of the image to green. You can change the value of the hue-rotate() function to change the color to whatever you want.

Here is a list of some other values that you can use for the hue-rotate() function:

* hue-rotate(0deg): No change
* hue-rotate(30deg): Rotate the hue by 30 degrees
* hue-rotate(60deg): Rotate the hue by 60 degrees
* hue-rotate(90deg): Rotate the hue by 90 degrees
* hue-rotate(120deg): Rotate the hue by 120 degrees
* hue-rotate(180deg): Rotate the hue by 180 degrees
* hue-rotate(270deg): Rotate the hue by 270 degrees
* hue-rotate(360deg): Rotate the hue by 360 degrees

1