Change SVG icon color using inline css - html

I am fairly new to SVG but how can I display a SVG icon and change its color using inline CSS? For example, if I wanted to change the color of example.svg to #FFF000 how would I do that?
I tried searching online but I couldn't find anything.

collinksmith did answer your question but didn't explain that you cannot change the colour of a .svg file with CSS. You need to add the SVG inline then you can apply CSS to it.

You need to use the fill property of the svg's path. Assuming an svg html element, with a class set on the svg element itself:
.class-name path {
fill: red;
}
EDIT Here's an example: https://jsfiddle.net/4447zb7o/
EDIT 2 To set the css inline, change the style attribute of the path element inside the svg:
<svg class="my-svg" height="210" width="400">
<path style="fill: green" d="M150 0 L75 200 L225 200 Z" />
</svg>

Related

Change part of SVG color with css

I have an SVG image and I'm looking to change part of its color.
The SVG will always consist of 2 colors, i.e. black & yellow
I'm looking to change the yellow color with a css class, so I could switch to another color theme easily without creating all the svg buttons in the yellow version.
Is this at all possible? I can't seem to find much online about this..
The SVG is set on span/div's using a class with background-image
If i implement the tag with SVG then I can change the color with css. But I'm looking to use it as a class if possible (and the svg should not be directly in the html)
<svg>
<use xlink:href="#robot" id="robot-1" />
</svg>
Turorial:
https://tympanus.net/codrops/2015/07/16/styling-svg-use-content-css/
JsFiddle:
https://jsfiddle.net/wahjvmnq/
You can change all the paths using --secondary-color to be fill: currentColor and then wrap the SVG with an HTML element that has a CSS class or styles applied to it that updates the color property.
After updating the path's in the SVG that currently use --secondary-color:
<div style="color:red;">
<svg>
<use xlink:href="#robot" id="robot-1" />
</svg>
</div>
Here is an example on jsFiddle where the yellow has been changed to red.
You can not override the SVG colors with CSS unless it is part of the HTML document.

SVG / CSS - fill different paths with different colours

I have a svg with different paths, ellipses etc. They have different fill colours. Say red & blue. Now, I put them all into a sprite, and would now like to modify fill colour with css on hover, so what I would normally do is remove the fills from the svg and do everything with css' fill property.
However, since I have different colours here, I cannot simply do fill:red, since everything will be red, but I want some of it to be blue.
You can add a different class to each of the paths:
<circle class="circleClass" cx="40" cy="50" r="26"/>
<square class="squareClass" cx="40" cy="50" r="26"/>
Then target those classes in your CSS:
.circleClass {
fill: red;
}
You can add a class to every path or you can use the nth-child.
Here is an easy example:
<path bla-bla-bla>
<path bla-bla-bla>
<path bla-bla-bla>
path:nth-child(1){
fill:red;
}
path:nth-child(2){
fill:blue;
}
path:nth-child(3){
fill:green;
}
What you are doing can't work. Anything referenced via a <use> is not present in the DOM under the <use> element.
.icon:hover .outer { }
won't work because the path with class outer is not a descendant of .icon. If you want to style the contents of a symbol, you need to do it by applying the ruules directly to the symbol. For example:
#btn-tester .outer { }
Unfortunately :hover events don't apply to symbols. So you can't do:
#btn-tester:hover .outer { }
Even if that worked, you may not want to do that anyway. If there were any other uses of the symbol on the page, it would change them also.
You probably are going to have to just inline the SVG on the page where you want it. Instead of using a symbol.

HTML SVG: show element on top of all elements

I created a svg chart, I added a tooltip that shows the corresponding value when the mouse hovers.
The problem is, since the path is rendered after the bars are created, then the tooltip is show behind the path http://take.ms/lljq3. what I need is that the tooltip is always in front of everything.
I try to add z-index:-1; position: relative; to the path and z-index:999; position: fixed; to the text but is not working.
See the example here: https://jsfiddle.net/sgcw8btx/
When creating svg elements, following rule applys:
3.3 Rendering Order
Elements in an SVG document fragment have an implicit drawing order, with the first elements in the SVG document fragment getting "painted" first. Subsequent elements are painted on top of previously painted elements.
Isn't it possible to place the tooltip element before the bar / line?
Otherwise you could try using use?
<use xlink:href="#id" />
And you could try to embed the HTML directly:
<svg xmlns="http://www.w3.org/2000/svg">
<g transform="translate(20,30)">
<rect width="200" height="150"/>
<foreignObject width="200" height="150">
<body xmlns="http://www.w3.org/1999/xhtml">
<div>
Hello, <b>World</b>!
</div>
</body>
</foreignObject>
</g>
</svg>

How to dynamically set color of an SVG created by background-image?

I am rendering an SVG via background image:
.svg_background
{
color:white;
background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 21 23"><ellipse fill="none" stroke="currentColor" stroke-width="2" cx="13" cy="8" rx="7" ry="7"/></svg>');
}
The purpose of this is to provide an image for a form element (no clean way to embed AFAIK), and to have the color of the image match text color (or a color set by LESS).
In the following fiddle, I see that the CSS correctly applies to an embedded SVG example, but not the SVG generated via background-image. Is there a way background-images are rendered that is causing this method to fail?
https://jsfiddle.net/9o28zee3/
The svg element in the url of background-image needs to have the color, like this:
background-image:url('data:image/svg+xml,<svg style="color:white" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 21 23"><ellipse fill="none" stroke="currentColor" stroke-width="2" cx="13" cy="8" rx="7" ry="7"/></svg>');
As far as making that dynamic, this post may be helpful for you: Modify SVG fill color when being served as Background-Image

Selecting subelements of an SVG template via CSS

I'm unsure of the proper markup to structure, select, and style individual SVG instances called from a common inline SVG "template" within an HTML file.
<!-- first instance to style -->
<svg id="instance1" viewBox="0 0 799.9 177.8">
<use xlink:href="#template">
</svg>
<!-- second instance, want different styles -->
<svg id="instance2" viewBox="0 0 799.9 177.8">
<use xlink:href="#template">
</svg>
In my SVG template, I have a group <g> of several paths, each of which is given a class name. I've given that SVG group an id of template, and am calling instances of it.
<!-- first instance to style -->
<svg id="instance1" viewBox="0 0 799.9 177.8">
<use xlink:href="#template">
</svg>
<!-- second instance, want to apply different styles -->
<svg id="instance2" viewBox="0 0 799.9 177.8">
<use xlink:href="#template">
</svg>
Now I'm attempting to select/style elements these instances in CSS as follows (though this syntax doesn't work!):
/* want to style each instance differently */
#instance1 .outline {
fill: red;
}
#instance2 .outline {
fill: green;
}
I assume this is easy, but I can't figure out the correct way to select/style these individual SVGs that <use> a common set of paths.
Any ideas? Thanks!
Code snippet available for review at http://codepen.io/edhebert/pen/GgmKOo
Contents of <use> are cloned into a shadow DOM and hence cannot be directly selected and styled using CSS the normal straightforward way that we know.
Before going further,note that you can apply styles to the contents of a <use> element by applying the styles to the <use> itself. For example, by doing this:
<svg id="instance1" viewBox="0 0 799.9 177.8">
<use xlink:href="#template" fill="maroon">
</svg>
.. the descendants of <use> will inherit the fill color from the <use> element. But all descendants will inherit this color.
If you want to apply a specific color to only one element inside <use>, for example, another technique can be used. This technique is, however, currently limited.
The technique works by taking advantage of the CSS currentColor variable. Fabrice Weinberg wrote about this technique in this blog post over at Codepen.
Basically, if you have an SVG element that you want to reuse, instead of specifying a fill color inside the "template" on each path, you do this instead:
<g>
<path fill="currentColor" d="..." />
</g>
And then, in your CSS, you can specify the color that you want, knowing that this color will be applied to the above path—because this is how currentColor works.
use#instance1 {
fill: deepPink; /* will be inherited by contents of `use` */
color: #eee; /* will be inherited by the path element */
}
Someone else took this technique further by using user-defined CSS variables to do the exact same thing. You can read about it here. The CSS variables technique is the same as Weinberg's technique, except that you can define as many variables as you want, use them inside your template, and then specify their values in your CSS—these values will then be used by these variables wherever you have defined them inside the template.
This technique works, but CSS variables are currently only supported in Firefox—so I don't suppose you would use it in production.
Hope this helps.