How do I have an SVG image inherit colors from the HTML document?

HtmlCssColorsSvg

Html Problem Overview


I have a bunch of SVG images that I want to embed in an HTML page, which is styled with CSS.

I want to be able to have elements in the SVG have their color inherited from the parent HTML element's color attribute.

I tried setting style="stroke: none; fill: inherit" but this doesn't work.

Html Solutions


Solution 1 - Html

HTML uses color whereas SVG uses fill and stroke. You can get fill or stroke to use the value of the color CSS property by using the value currentColor e.g. fill="currentColor"

Solution 2 - Html

You can use fill="currentColor".

<a href="#" style="color:red">
<svg fill="currentColor"> ...</svg>
</a>

Solution 3 - Html

stroke="currentColor" this worked for me

Solution 4 - Html

Define the fill: of the whole SVG as currentColor in the CSS:

svg {
  fill: currentColor;
}

This makes the whole SVG to inherit the normal CSS color: of the surrounding element. Just make sure that all SVG child elements don't have any fill defined.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1762639View Question on Stackoverflow
Solution 1 - HtmlRobert LongsonView Answer on Stackoverflow
Solution 2 - HtmlКоля МіщанчукView Answer on Stackoverflow
Solution 3 - HtmlJudith loboView Answer on Stackoverflow
Solution 4 - HtmlfloriView Answer on Stackoverflow