Using SVG inline I can make the current color inherit using the value currentColor
for property fill
. In this way I can make my image adapt to the color of the element that contains it. For example:
.rojo {
color:red;
}
.verde {
color:green;
}
<div class="rojo">
<svg width="10" height="10" viewBox="0 0 10 10" xmlns="http://www.w3.org/2000/svg">
<g id="dibujo">
<circle cx="5" cy="5" r="4" fill="currentColor" />
</g>
</svg>
Hola
</div>
<div class="verde">
<svg width="10" height="10" viewBox="0 0 10 10" xmlns="http://www.w3.org/2000/svg">
<g id="dibujo">
<circle cx="5" cy="5" r="4" fill="currentColor" />
</g>
</svg>
Adiós
</div>
That's great because it works: the circles look different in color even when the code for the SVGs is the same in both cases. The problem is that I do not want to have the same inline code many times.
I'd prefer it to be in an external file, like this one that I'll call my Image-svg:
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="10" height="10" viewBox="0 0 10 10" xmlns="http://www.w3.org/2000/svg">
<g id="dibujo">
<circle cx="5" cy="5" r="4" fill="currentColor" />
</g>
</svg>
and then you can easily add it multiple times:
<div class="rojo">
<img src="./miImagen.svg" alt="Mi SVG en rojo" />
Hola
</div>
<div class="verde">
<img src="./miImagen.svg" alt="Mi SVG en verde" />
Adiós
</div>
But if I do that, currentColor
stops working and you see the circles in black (I add capture of the result because it does not allow me to upload SVG):
How can I do to include an "external" SVG and inherit the value of currentColor
?
Note: the image I'm going to work with is bigger and more complex than just a circle that changes color, it would have multiple colors and lines that would change color with
currentColor
. I had planned to put it in a font and include it as the glyphicons of Bootstrap or FontAwesome, but being large and multicolored, I do not know if that option is feasible.