Here I'm trying to change the image svg color.
<button>
<img src="someImage.svg">
</button>
Is it possible to change the color of the svg image in the button in angular 5
Here I'm trying to change the image svg color.
<button>
<img src="someImage.svg">
</button>
Is it possible to change the color of the svg image in the button in angular 5
You can apply CSS to your SVG if the SVG is inline in the DOM. The easiest way to achieve this is with an SVG injector. It uses Javascript to load an external SVG file and replace an HTML element (usually an <img>
element) with the SVG markup.
If you use SVGInject ( https://github.com/iconfu/svg-inject/ ) your HTML will look like this:
<button>
<img src="someImage.svg" onload="SVGInject(this)">
</button>
The onload
attribute does the magic by replacing the <img>
element after the image is loaded. It should look the same, but you will be able to apply CSS to the SVG.
I took some code from: http://www.petercollingridge.co.uk/tutorials/svg/interactive/javascript/ You have to use the object element instead of the img element. You have to give your object element an id (in the example it is "svg-object"). You have to give the button inside your svg file an id (in the example it is external-1). Then you can change the style
var svgObject = document.getElementById('svg-object').contentDocument;
var svg = svgObject.getElementById('external-1');
svg.style.fill = "green"
Inlining your svg is a little easier, but it can be done.