Change part of SVG color with css - html

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.

Related

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.

Is svg icon belong to style layer and view layer?

My team are shifting to svg icons.
We use to define the icons in css style classes, and are now considering whether SVG icons should be implemented as view component rendering svg markup (instead of html). This should allow for better reuse svg by changing dimensions, color etc. On the other hand this takes the icons out of the styling domain and any style change will cause code change (and not only css change).
Whats is the right way (if there any) to work with svg icons, control size and colors, and still save style layer and view layer?
SVG editors sometimes put style information inline, but style information for SVGs can also be provided inside <style> tags, as usual CSS.
If the SVG is in your DOM (the simplest way to do this, is to write the SVG directly into your HTML) the SVG can be formatted directly from your usual CSS file:
p {
color: #666;
}
.blueTriangle {
fill: lightblue;
stroke: #666;
stroke-width: 8;
}
<p> This is usual text in HTML p tags. </p>
<p> The next thing is embedded SVG, styled by usual CSS: </p>
<svg width="250" height="120">
<path class="blueTriangle" d="M150 20 L130 90 L170 90 Z" />
</svg>
With transform also scaling and rotations can be done, showing different components can be accomplished via display, visibility or opacity with different implications.
Changing paths themselves would be considered as changing the content of the file and can be done via JavaScript. Therefore the separation of style an content can still be preserved.

Change SVG icon color using inline css

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>

Recolor an SVG image via CSS "fill" doesn't work

I'm trying to recolor a simple SVG image with CSS (as I saw here http://codepen.io/chriscoyier/pen/evcBu ):
My HTML:
<img src="http://dl.dropbox.com/u/12091580/rwdicon/icon-menu.svg" class="myMenu" alt="menu">
My CSS:
.myMenu { fill: red; }
It's not working (see http://jsfiddle.net/sexyzane/1hojaccb/ )!
What am I doing wrong?
fill is used for svg element markup, you have an img element with an svg source, as such you cannot use fill to change the image color.
Instead, if you want to colorize the image, you may want to look into applying a CSS filter effect to the img tag, although this may not be able to achieve the exact result you're after.
Demo Fiddle

Add filter to clipped element in svg (combine clipPath and filter elements)

I wonder if it's possible to apply svg filter to clipped html content. All demos I found have clipping and filtering separate.
Here's an example: http://jsfiddle.net/B7593/1/. I want the yellow circle to drop a shadow.
Tried adding filter=url('#dropshadow') / style='filter:url(#dropshadow)' to circle / clipPath / div elements, but none of these worked.
I don't think you can do it the way you're approaching it - even if you could make the shadow a part of the clip, you wouldn't see it when you applied the clip because none of the colour is retained, just the opacity of the pixels is used to determine what shows through. What will work (in Firefox at least), is to apply both the clip and the filter to content within the SVG like this:
<g filter="url(#dropshadow)">
<foreignObject width="300" height="300" clip-path="url(#c1)">
<body>
<div id="target"></div>
</body>
</foreignObject>
</g>
Here's a full example.