Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

html

css

colors

svg

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.

like image 261
user1762639 Avatar asked Oct 21 '12 18:10

user1762639


People also ask

How do you inherit colors in SVG?

You can use fill="currentColor" . Show activity on this post. 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.

Can SVG images have color?

You're largely limited to a single color with icon fonts in a way that SVG isn't, but still, it is appealingly easy to change that single color with color . Using inline SVG allows you to set the fill , which cascades to all the elements within the SVG, or you can fill each element separately if needed.

Does SVG support color?

Note: As a presentation attribute, color can be used as a CSS property. See CSS color for further information. As a presentation attribute, it can be applied to any element, but as noted above, it has no direct effect on SVG elements.


2 Answers

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"

like image 57
Robert Longson Avatar answered Sep 25 '22 21:09

Robert Longson


You can use fill="currentColor".

<a href="#" style="color:red"> <svg fill="currentColor"> ...</svg> </a> 
like image 44
Коля Міщанчук Avatar answered Sep 25 '22 21:09

Коля Міщанчук