Adding Class to SVG - css

I grabbed an SVG file, a map of NY State, and would like to add classes to various elements (the counties).
I tried adding a class via javascript. The class was added but visually nothing happened. I tried inserting the class directly. That didn't work. I tried adding a fill to an existing ID but that didn't do anything.
The existing code looks something like:
<polygon id="St_Lawrence" class="highlight" points="404.480957,7.098633
As said I tried adding a class, adding fill information to the existing ID (St_Lawrence) but that didn't work. However adding a style inline worked (see below)
<polygon id="St_Lawrence" style="fill:#FF0000;" points="404.480957,7.098633
That, unfortunately, doesn't really help.
The basic code is below:
<svg xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/"
width="633.475098" height="475.573242" viewBox="0 0 633.475098 475.573242"
style="overflow:visible;enable-background:new 0 0 633.475098 475.573242" xml:space="preserve">
<g id="Layer_1">
<g fill="#FFFFFF" stroke="#000000" stroke-width="0.25">
<polygon id="St_Lawrence" points="404.480957,7.098633 404.581543,7.098633 404.581543,7.098633
406.171387,7.099609 406.171387,7.099609 406.218262,7.109375 406.218262,7.109375 406.313965,7.109375 408.001465,7.123047
408.001465,7.123047 408.050293,7.123047 408.050293,7.123047 410.122559,7.138672 410.591309,11.53125 410.591309,11.53125
EDIT:
I worked with another SVG file and got classes to work as they should. Don't know SVG well enough to decipher why this one works while the first one doesn't.

You can use CSS styles (whether based on class, id, or some other CSS selector) in SVG. It's a little tricky to mix inline attributes (such as individual fill="blue"), the style attribute (e.g. style="fill: blue;"), and stylesheets, because SVG doesn't interpret them in the order of precedence you might expect (or at least, I expect).
In the above map, id-based selectors are probably going to be more convenient, because the counties are named by id.
Here's an example of CSS styling by id:
<svg xmlns="&ns_svg;" xmlns:xlink="&ns_xlink;" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/"
width="633.475098" height="475.573242" viewBox="0 0 633.475098 475.573242"
style="overflow:visible;enable-background:new 0 0 633.475098 475.573242" xml:space="preserve">
<style type="text/css">
<![CDATA[
.st0{fill:#FFFFFF;stroke:#000000;stroke-width:0.25;}
.st1{fill:none;stroke:#000044;stroke-width:0.5;}
#St_Lawrence {
fill: blue; fill-opacity: 0.3;
}
#Queens {
fill: orange; fill-opacity: 0.9;
}
]]>
</style>
(with the rest of the document the same)
This highlights the two counties with fill colors:

Works fine with the following:
<style type="text/css">
<![CDATA[
.st0{fill:#FFFFFF;stroke:#000000;stroke-width:0.25;}
.st1{fill:none;stroke:#000044;stroke-width:0.5;}
.highlight{fill:#00ff00;stroke:#000044;stroke-width:0.5;}
]]>
</style>
<g fill="#FFFFFF" stroke="#000000" stroke-width="0.25">
<polygon id="St_Lawrence" class="highlight" points="404.480957,7.098633
However, <polygon> already has a class="st0" which has a fill:#FFFFFF, so you probably want to change or remove the st0 class.
As a test you could just add .st0:hover{fill:red;} which will highlight the county when the mouse is moved over it:
<style type="text/css">
<![CDATA[
.st0{fill:#FFFFFF;stroke:#000000;stroke-width:0.25;}
.st0:hover{fill:red;}
.st1{fill:none;stroke:#000044;stroke-width:0.5;}
]]>
</style>

You need to apply the style to the icon enclosed in the <path> tag: style="stroke:var(--button-bg-color)"
<svg width="32" height="62" viewBox="0 0 32 62" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M25 37L31 31C31 31 27.3431 27.3431 25 25" style="stroke:var(--button-bg-color)" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
<path d="M31 61C14.4315 61 1 47.5685 1 31C1 14.4315 14.4315 1 31 1" stroke="#C4C4C4"/>
</svg>

Related

SVG don't change color in hover

I've been mapping my information and the problem occurs when I want to change the color of an SVG when the hover event occurs. This is my code:
{DataBanner.map((d, index) => (
<div>
<img
src={d.icon}
className="icon-image"
/>
</div>
))}
css styles:
.icon-image:hover {
stroke: white !important;
}
this is an example of how the SVG comes:
<svg width="39" height="57" viewBox="0 0 39 57" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M29.2799 47.7309V55.874H28.9356M10.1257 8.61963H1.29419V28.2326M1.29419 28.2326V55.874H28.9356M1.29419 28.2326L28.9356 55.874M10.8137 37.6377V1.39404H37.882V47.6161H20.7923M31.0791 13.4712V19.6147M28.0073 16.543H34.1509M25.467 14.7114C25.467 17.191 23.6909 19.2012 21.5 19.2012C19.3091 19.2012 17.533 17.191 17.533 14.7114C17.533 12.2318 19.3091 10.2217 21.5 10.2217C23.6909 10.2217 25.467 12.2318 25.467 14.7114ZM28.657 27.3237C28.657 31.1247 25.4527 31.3704 21.5 31.3704C17.5473 31.3704 14.343 31.1247 14.343 27.3237C14.343 23.5227 17.5473 20.4414 21.5 20.4414C25.4527 20.4414 28.657 23.5227 28.657 27.3237Z" stroke="black"/>
</svg>
This would not work because the SVG is now imported as an image. To do this usually, you would have to paste the SVG code manually into your markup or import as a component.
I think you should go through this resource: https://www.sanity.io/guides/import-svg-files-in-react. It explains what you are trying to do and the solution I mentioned as well.
Example of SVG in JSX attached. Attributes should be in camel case

A Part of the SVG image is not showing up on WordPress website (Using Elementor)

I'm having trouble with an SVG file that I uploaded to my WordPress website using Elementor. The file seems normal in the media library but when I add it to the content, most of it is gone.
SVG image Before adding to the content
SVG image After
.
**Note: I added 12 other SVG files but none of them had this issue.
I'm not a programmer, but I read in other posts that you need to do something with the source code of the SVG, but I couldn't really understand anything. I tried reading the other posts, but the code wasn't similar in the least.
Here's the SVG file's code:
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 26.3.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="300px" height="300px" viewBox="0 0 300 300" style="enable-background:new 0 0 300 300;" xml:space="preserve">
<style type="text/css">
.st0{fill:#5BBC96;}
.st1{fill:url(#SVGID_1_);}
</style>
<g>
<path class="st0" d="M56.39,243.9c-2.15,0-4.21-1.22-5.17-3.3c-1.33-2.85-0.09-6.24,2.76-7.57c0.77-0.36,77.98-36.67,120.21-85.04
c46.73-53.54,73.12-94.48,88.24-136.9c1.06-2.97,4.31-4.52,7.29-3.45c2.97,1.06,4.51,4.32,3.46,7.29
c-15.63,43.84-42.66,85.88-90.4,140.56c-43.84,50.22-120.74,86.37-123.99,87.88C58.01,243.73,57.2,243.9,56.39,243.9z"/>
<linearGradient id="SVGID_1_" gradientUnits="userSpaceOnUse" x1="0.8632" y1="23.4607" x2="276.5694" y2="299.167">
<stop offset="5.327789e-09" style="stop-color:#5BBC96"/>
<stop offset="1" style="stop-color:#515BA8"/>
</linearGradient>
<path class="st1" d="M298.74,277l-23.77,23l0.05-16.72c0-1.13-0.92-2.05-2.06-2.05l-253.27-0.78h-0.37V26.12
c0-1.13-0.92-2.04-2.04-2.04H0.56L23.63,0.69c0.91-0.92,2.4-0.92,3.3,0l23.08,23.38H33.29c-1.13,0-2.04,0.91-2.04,2.04v242.44
L273,269.3c1.14,0,2.07-0.91,2.08-2.04l0.05-16.72l23.62,23.15C299.68,274.61,299.67,276.09,298.74,277z M90.19,234.83
c-15.27,0-27.68-12.42-27.68-27.68c0-15.27,12.42-27.68,27.68-27.68c15.26,0,27.68,12.42,27.68,27.68
C117.86,222.42,105.45,234.83,90.19,234.83z M90.19,193.73c-7.4,0-13.43,6.02-13.43,13.43c0,7.4,6.02,13.42,13.43,13.42
c7.4,0,13.42-6.02,13.42-13.42C103.61,199.75,97.59,193.73,90.19,193.73z M169.61,166.78c-15.27,0-27.68-12.42-27.68-27.68
s12.42-27.68,27.68-27.68c15.26,0,27.68,12.42,27.68,27.68S184.87,166.78,169.61,166.78z M169.61,125.67
c-7.4,0-13.43,6.02-13.43,13.43c0,7.4,6.02,13.43,13.43,13.43c7.4,0,13.42-6.02,13.42-13.43
C183.04,131.7,177.01,125.67,169.61,125.67z M184.4,90.31c-8.81,0-15.98-7.17-15.98-15.98c0-8.81,7.17-15.98,15.98-15.98
c8.81,0,15.98,7.17,15.98,15.98C200.37,83.14,193.21,90.31,184.4,90.31z M184.4,69.75c-2.52,0-4.58,2.05-4.58,4.58
c0,2.52,2.05,4.57,4.58,4.57c2.52,0,4.58-2.05,4.58-4.57C188.97,71.81,186.92,69.75,184.4,69.75z M237.13,141.58
c-8.81,0-15.98-7.17-15.98-15.98c0-8.81,7.17-15.98,15.98-15.98c8.81,0,15.98,7.17,15.98,15.98
C253.11,134.41,245.94,141.58,237.13,141.58z M237.13,121.03c-2.52,0-4.58,2.05-4.58,4.58c0,2.52,2.05,4.57,4.58,4.57
s4.58-2.05,4.58-4.57C241.71,123.08,239.65,121.03,237.13,121.03z M154.58,213.32c-8.81,0-15.98-7.17-15.98-15.98
c0-8.81,7.17-15.98,15.98-15.98c8.81,0,15.98,7.17,15.98,15.98C170.56,206.16,163.39,213.32,154.58,213.32z M154.58,192.77
c-2.52,0-4.58,2.05-4.58,4.58c0,2.52,2.05,4.58,4.58,4.58c2.52,0,4.58-2.05,4.58-4.58C159.15,194.82,157.1,192.77,154.58,192.77z
M104.69,170.56c-8.81,0-15.98-7.17-15.98-15.98c0-8.81,7.17-15.98,15.98-15.98s15.98,7.17,15.98,15.98
C120.67,163.39,113.51,170.56,104.69,170.56z M104.69,150c-2.52,0-4.58,2.05-4.58,4.58s2.05,4.58,4.58,4.58
c2.52,0,4.58-2.05,4.58-4.58S107.21,150,104.69,150z M249.38,100.26c-15.26,0-27.68-12.42-27.68-27.68s12.42-27.68,27.68-27.68
c15.27,0,27.68,12.42,27.68,27.68S264.65,100.26,249.38,100.26z M249.38,59.16c-7.4,0-13.42,6.02-13.42,13.43
c0,7.4,6.02,13.43,13.42,13.43c7.4,0,13.43-6.02,13.43-13.43C262.81,65.18,256.79,59.16,249.38,59.16z"/>
</g>
</svg>
Any help would be wonderful. The website is NumXL if it might help in any way. If the information is not enough, please let me know and I'll immediately try to provide it.
I hope this makes sense, Thank you!

AndroidSVG: How to dynamically change Inkscape svg color

AndroidSVG allows for dynamic color change as explained here.
The following code works for 95% of my icon library ...
SVGImageView svgIV;
......
String color = "#FEFBDE"
svgIV.setCSS(String.format("* { fill:%s; }", color););
... but fails on Inkscape SVGs such as this:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
version="1.1"
width="64"
height="64"
id="svg2"
style="display:inline">
<defs
id="defs4" />
<g
transform="translate(403.45398,-1106.6063)"
id="layer1"
style="display:inline">
<g
transform="matrix(7.6594323,0,0,7.6594323,2702.0005,-7795.5738)"
id="g3766">
<path
d="m -401.264,1167.506 0,2.8346 1.41732,0"
id="path4668"
style="fill:none;stroke:#000000;stroke-width:0.53149605;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none" />
<path
d="m -401.264,1162.5164 c 0,0 -0.5132,-0.012 -0.5132,0.6511 0,0.3318 0,0.1926 0,0.5423 0,0.3989 -0.64122,0.3626 -0.64122,1.0261 0,0.5996 0.047,0.4425 0,0.8135 -0.047,0.371 -0.76981,0.356 -0.76981,1.1045 0,0.9955 0.64122,0.8521 0.64122,0.8521 l 1.28301,0"
id="path4711-1"
style="fill:none;stroke:#000000;stroke-width:0.53149605;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;display:inline" />
<path
d="m -401.264,1162.5164 c 0,0 0.5132,-0.012 0.5132,0.6511 0,0.3318 0,0.1926 0,0.5423 0,0.3989 0.64122,0.3626 0.64122,1.0261 0,0.5996 -0.047,0.4425 0,0.8135 0.047,0.371 0.76981,0.356 0.76981,1.1045 0,0.9955 -0.64122,0.8521 -0.64122,0.8521 l -1.28301,0"
id="path4711-1-3"
style="fill:#000000;fill-opacity:1;stroke:#000000;stroke-width:0.53149605;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;display:inline" />
</g>
</g>
</svg>
What, if any, is the necessary CSS?
On reflection, this may be a more general CSS question:
How to override part of a style?
Normally (in CSS) the way you would achieve this would be via use of the !important flag.
svgIV.setCSS(String.format("* { fill:%s !important; }", color););
Unfortunately, the CSS system in AndroidSVG does not yet support !important. It's on my TODO list.
In the meantime, you will need to convert those SVGs to use either attributes (fill="whatever") or CSS. You can easily convert those files to use attribute form, by loading them into Inkscape and save as "Optimized SVG".
This fork was created in response to the question.
It allows full color control over any .svg via a user-defined callback and is great for users who do not wish to manually convert 2,735 Inkscape definitions to conform with the current restriction.

How to style svg image with outer css [duplicate]

I have several SVG graphics I'd like to modify the colors of via my external style sheets - not directly within each SVG file. I'm not putting the graphics in-line, but storing them in my images folder and pointing to them.
I have implemented them in this way to allow tooltips to work, and I also wrapped each in an <a> tag to allow a link.
<a href='http://youtube.com/...' target='_blank'><img class='socIcon' src='images/socYouTube.svg' title='View my videos on YouTube' alt='YouTube' /></a>
And here is the code of the SVG graphic:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="stylesheets/main.css" type="text/css"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 56.69 56.69">
<g>
<path d="M28.44......./>
</g>
</svg>
I put the following in my external CSS file (main.css):
.socIcon g {fill:red;}
Yet it has no effect on the graphic. I also tried .socIcon g path {} and .socIcon path {}.
Something isn't right, perhaps my implementation doesn't allow external CSS modifications, or I missed a step? I'd really appreciate your help! I just need the ability to modify the colors of the SVG graphic via my external stylesheet, but I cannot lose the tooltip and link ability (I may be able to live without tooltips though).
Your main.css file would only have an effect on the content of the SVG if the SVG file is included inline in the HTML:
https://developer.mozilla.org/en/docs/SVG_In_HTML_Introduction
<html>
<body>
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 56.69 56.69">
<g>
<path d="M28.44......."/>
</g>
</svg>
</html>
If you want to keep your SVG in files, the CSS needs to be defined inside of the SVG file.
You can do it with a style tag:
http://www.w3.org/TR/SVG/styling.html#StyleElementExample
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1"
width="50px" height="50px" viewBox="0 0 50 50">
<defs>
<style type="text/css"><![CDATA[
.socIcon g {
fill:red;
}
]]></style>
</defs>
<g>
<path d="M28.44......./>
</g>
</svg>
You could use a tool on the server side to update the style tag depending on the active style. In ruby you could achieve this with Nokogiri. SVG is just XML. So there are probably many XML libraries available that can probably achieve this.
If you're not able to do that, you will have to just have to use them as though they were PNGs; creating a set for each style, and saving their styles inline.
You can do what you want, with one (important) caveat: the paths within your symbol can't be styled independently via external CSS -- you can only set the properties for the entire symbol with this method. So, if you have two paths in your symbol and want them to have different fill colors, this won't work, but if you want all your paths to be the same, this should work.
In your html file, you want something like this:
<style>
.fill-red { fill: red; }
.fill-blue { fill: blue; }
</style>
<a href="//www.example.com/">
<svg class="fill-red">
<use xlink:href="images/icons.svg#example"></use>
</svg>
</a>
And in the external SVG file you want something like this:
<svg xmlns="http://www.w3.org/2000/svg">
<symbol id="example" viewBox="0 0 256 256">
<path d="M120...." />
</symbol>
</svg>
Swap the class on the svg tag (in your html) from fill-red to fill-blue and ta-da... you have blue instead of red.
You can partially get around the limitation of being able to target the paths separately with external CSS by mixing and matching the external CSS with some in-line CSS on specific paths, since the in-line CSS will take precedence. This approach would work if you're doing something like a white icon against a colored background, where you want to change the color of the background via the external CSS but the icon itself is always white (or vice-versa). So, with the same HTML as before and something like this svg code, you'll get you a red background and a white foreground path:
<svg xmlns="http://www.w3.org/2000/svg">
<symbol id="example" viewBox="0 0 256 256">
<path class="background" d="M120..." />
<path class="icon" style="fill: white;" d="M20..." />
</symbol>
</svg>
You can include in your SVG files link to external css file using:
<link xmlns="http://www.w3.org/1999/xhtml" rel="stylesheet" href="mystyles.css" type="text/css"/>
You need to put this after opening tag:
<svg>
<link xmlns="http://www.w3.org/1999/xhtml" rel="stylesheet" href="mystyles.css" type="text/css"/>
<g>
<path d=.../>
</g>
</svg>
It's not perfect solution, because you have to modify svg files, but you modify them once and than all styling changes can be done in one css file for all svg files.
It is possible to style an SVG by dynamically creating a style element in JavaScript and appending it to the SVG element. Hacky, but it works.
<object id="dynamic-svg" type="image/svg+xml" data="your-svg.svg">
Your browser does not support SVG
</object>
<script>
var svgHolder = document.querySelector('object#dynamic-svg');
svgHolder.onload = function () {
var svgDocument = svgHolder.contentDocument;
var style = svgDocument.createElementNS("http://www.w3.org/2000/svg", "style");
// Now (ab)use the #import directive to load make the browser load our css
style.textContent = '#import url("/css/your-dynamic-css.css");';
var svgElem = svgDocument.querySelector('svg');
svgElem.insertBefore(style, svgElem.firstChild);
};
</script>
You could generate the JavaScript dynamically in PHP if you want to - the fact that this is possible in JavaScript opens a myriad of possibilities.
One approach you can take is just to use CSS filters to change the appearance of the SVG graphics in the browser.
For example, if you have an SVG graphic that uses a fill color of red within the SVG code, you can turn it purple with a hue-rotate setting of 180 degrees:
#theIdOfTheImgTagWithTheSVGInIt {
filter: hue-rotate(180deg);
-webkit-filter: hue-rotate(180deg);
-moz-filter: hue-rotate(180deg);
-o-filter: hue-rotate(180deg);
-ms-filter: hue-rotate(180deg);
}
Experiment with other hue-rotate settings to find the colors you want.
To be clear, the above CSS goes in the CSS that is applied to your HTML document. You are styling the img tag in the HTML code, not styling the code of the SVG.
And note that this won’t work with graphics that have a fill of black or white or gray. You have to have an actual color in there to rotate the hue of that color.
It should be possible to do by first inlining the external svg images. The code below comes from replace all SVG images with inline SVG by Jess Frazelle.
$('img.svg').each(function(){
var $img = $(this);
var imgID = $img.attr('id');
var imgClass = $img.attr('class');
var imgURL = $img.attr('src');
$.get(imgURL, function(data) {
// Get the SVG tag, ignore the rest
var $svg = $(data).find('svg');
// Add replaced image's ID to the new SVG
if (typeof imgID !== 'undefined') {
$svg = $svg.attr('id', imgID);
}
// Add replaced image's classes to the new SVG
if (typeof imgClass !== 'undefined') {
$svg = $svg.attr('class', imgClass+' replaced-svg');
}
// Remove any invalid XML tags as per http:validator.w3.org
$svg = $svg.removeAttr('xmlns:a');
// Replace image with new SVG
$img.replaceWith($svg);
});
});
A very quick solution to have dynamic style with an external css stylesheet, in case you are using the <object> tag to embed your svg.
This example will add a class to the root <svg> tag on click on a parent element.
file.svg :
<?xml-stylesheet type="text/css" href="../svg.css"?>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="">
<g>
<path/>
</g>
</svg>
html :
<a class="parent">
<object data="file.svg"></object>
</a>
Jquery :
$(function() {
$(document).on('click', '.parent', function(){
$(this).find('object').contents().find('svg').attr("class","selected");
}
});
on click parent element :
<svg xmlns="http://www.w3.org/2000/svg" viewBox="" class="selected">
then you can manage your css
svg.css :
path {
fill:none;
stroke:#000;
stroke-miterlimit:1.41;
stroke-width:0.7px;
}
.selected path {
fill:none;
stroke:rgb(64, 136, 209);
stroke-miterlimit:1.41;
stroke-width:0.7px;
}
For External styles
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 56.69 56.69">
<style>
#import url(main.css);
</style>
<g>
<path d="M28.44......./>
</g>
</svg>
For Internal Styles
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 56.69 56.69">
<style>
.socIcon g {fill:red;}
</style>
<g>
<path d="M28.44......./>
</g>
</svg>
Note: External Styles will not work if you include SVG inside <img> tag. It will work perfectly inside <div> tag
When used in an <image> tag SVG must be contained in a single file for privacy reasons. This bugzilla bug has more details on exactly why this is so. Unfortunately you can't use a different tag such as an <iframe> because that won't work as a link so you'll have to embed the CSS in a <style> tag within the file itself.
One other way to do this would be to have the SVG data within the main html file i.e.
<a href='http://youtube.com/...' target='_blank'>
<svg id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 56.69 56.69">
<g>
<path d="M28.44......./>
</g>
</svg>
</a>
You could style that with an external CSS file using the HTML <link> tag.
What works for me: style tag with #import rule
<defs>
<style type="text/css">
#import url("svg-common.css");
</style>
</defs>
#leo here is the angularJS version, thanks again
G.directive ( 'imgInlineSvg', function () {
return {
restrict : 'C',
scope : true,
link : function ( scope, elem, attrs ) {
if ( attrs.src ) {
$ ( attrs ).each ( function () {
var imgID = attrs.class;
var imgClass = attrs.class;
var imgURL = attrs.src;
$.get ( imgURL, function ( data ) {
var $svg = $ ( data ).find ( 'svg' );
if ( typeof imgID !== 'undefined' ) {
$svg = $svg.attr ( 'id', imgID );
}
if ( typeof imgClass !== 'undefined' ) {
$svg = $svg.attr ( 'class', imgClass + ' replaced-svg' );
}
$svg = $svg.removeAttr ( 'xmlns:a' );
elem.replaceWith ( $svg );
} );
} );
}
}
}
} );
In my case, I have applied display:block in outer class.
Need to experiment, where it fits.
Inside inline svg adding class and style does not even remove the above white-space.
See: where the display:block gets applied.
<div class="col-3 col-sm-3 col-md-2 front-tpcard"><a class="noDecoration" href="#">
<img class="img-thumbnail img-fluid"><svg id="Layer_1"></svg>
<p class="cardtxt">Text</p>
</a>
</div>
The class applied
.front-tpcard .img-thumbnail{
display: block; /*To hide the blank whitespace in svg*/
}
This worked for me.
Inner svg class did not worked
I know its an old post, but just to clear this problem... you're just using your classes at the wrong place :D
First of all you could use
svg { fill: red; }
in your main.css to get it red. This does have effect. You could probably use node selectors as well to get specific paths.
Second thing is, you declared the class to the img-tag.
<img class='socIcon'....
You actually should declare it inside your SVG. if you have different paths you could define more of course.
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="stylesheets/main.css" type="text/css"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 56.69 56.69">
<g>
<path class="myClassForMyPath" d="M28.44......./>
</g>
</svg>
Now you could change the color in your main.css like
.myClassForMyPath {
fill: yellow;
}
As described in answers here and in other related questions, stylesheets only apply to the current DOM. As such, you need to make the svg part of the document's DOM, by inlining it inside the html, or including it inside the DOM using javascript.
That's what I ended up doing there:
<object type="image/svg+xml" data="illustration.svg"
onload="this.parentNode.replaceChild(this.contentDocument.documentElement, this);">
</object>
While that solution works really well for me, only use it on documents you control, as inline loading an svg from an untrusted source gives that source the ability to include at least arbitrary scripts, css and other elements inside your HTML, breaking the sandbox.
I haven't investigated how well caching works with this, but it should work as well as with img tags, given that the javascript function is ran after the element loads. Feel free to edit this.
If javascript is disabled, the svg is not included into the DOM, and the style is not applied, so make sure the default style is usable. CSS custom properties (variables) with fallbacks work quite well for that use-case.
Not fully tested yet, but Ive found a nice way to treat external svg files like inlines. No Javascript, no server-code - just a document root-id:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" id="svgroot">...
After this we're allowed to use it with an SVG2-Tag which seems to be supported by Chrome/Edge - Blink; Firefox - Gecko; Safari - Webkit:
<html>
<head>
<style>
.primary-text {
color: green;
}
.icon {
height: 100px;
width: 100px;
fill: currentColor;
}
</style>
</head>
<body>
<h3>svg Fill currentColor</h3>
<svg class="primary-text icon">
<use href="example.svg#svgroot"></use>
</svg>
</body>
</html>
Going further, you're able to create a resource-dictionary/sprite file:
<svg xmlns="http://www.w3.org/2000/svg">
<svg viewBox="0 0 512 512" id="apple">...
<svg viewBox="0 0 512 512" id="peach">...
</svg>
Honestly, this seems too good to be true... :D
// Edit:
This seems to be a well respected approach:
https://icons.getbootstrap.com/#usage
"I am actually going to change the colors of these images based on what color scheme the user has chosen for my site." - Jordan 10 hours ago
I suggest you to use PHP for this. There's really no better way to do this without icon fonts, and if you resist using them, you could try this:
<?php
header('Content-Type: image/svg+xml');
echo '<?xml version="1.0" encoding="utf-8"?>';
$color = $_GET['color'];
?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 56.69 56.69">
<g>
<path fill="<?php echo $color; ?>" d="M28.44..."/>
</g>
</svg>
And later you could use this file as filename.php?color=#ffffff to get the svg file in the desired color.
This method will work if the svg is viewed within a web browser but as soon as this code is uploaded to the sever and the class for the svg icon is coded as if it was a background image the color is lost and back to the default color. Seems like the color can not be changed from the external style sheet even though both the svg class for the color and the top layer class for the display and position of the svg are both mapped to the same directory.

SVG spritesheet targeting with CSS

I've been trying to find a solution for handling SVG elements used as background images via CSS:
.icon.arrow-down
{
background-image: url( 'images/spritesheet.svg#arrow-down' );
}
I'm using :target directly in the SVG file in order to target a particular layer (or "group") within the combined SVG spritesheet.
<?xml version="1.0" encoding="utf-8" ?>
<svg class="icon" id="icon" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="50px" height="50px" viewBox="0 0 50 50">
<defs>
<style>
rect, line { shape-rendering: crispEdges; }
svg .icon { display: none; }
svg .icon:target { display: inline; }
</style>
</defs>
<!-- Arrows -->
<g class="icon" id="arrow-down" transform="translate(0,12.5)">
<path fill="#F00" d="M 0,0 50,0 25,25 z" />
</g>
<g class="icon" id="arrow-up" transform="translate(0,12.5)">
<path fill="#F00" d="M 0,25 50,25 25,0 z" />
</g>
...
</svg>
This works fine for Firefox and IE9+, but in Chrome it seems to be ignoring the #profile part. However, going to the SVG sheet directly in the browser, with the target id, yields the correct image.
Is this a bug in the way Chrome is handling :target in background images?
I'm trying to avoid having to separate everything into their own files, so only one resource is downloaded, but I don't know that it is possible yet.
Notice how the icons are not shown in Chrome, but are in other browsers: http://jsfiddle.net/sYL5F/1/
It's a known issue and is specific to using it as a background and apparently won't be fixed because of security concerns (Opera also doesn't display it). If you view the SVG directly, it works as you would expect.
https://code.google.com/p/chromium/issues/detail?id=128055#c6
SVG stacks will not be supported for CSS properties taking CSS Image
values. This includes, but is not limited to, background-image,
mask-image, border-image.
This is a resolution of the SVG and CSS WG to differ between resources
(like SVG gradients, masks, clipPath) and image values during parse
time of CSS. This is a security requirement to protect the users
privacy and safety.
See following discussions for further information:
http://lists.w3.org/Archives/Public/www-style/2012Oct/0406.html
http://lists.w3.org/Archives/Public/www-style/2012Oct/0765.html
You're just going to handle your SVG the same way you would an old fashioned sprite map.
For my latest project, I've implemented my own way of creating custom SVG parameters using a PHP MVC framework I've been working on. Essentially, I created a controller for linking to icons:
/icon/NAME_OF_ICON.svg?color=F00
My icon controller takes the filename and injects the GET parameters into the SVG file.
//define( ROOT, "path/to/webroot" );
//$filename = ...get filename from URL...;
$svg = simplexml_load_file( ROOT . "/assets/icons/{$filename}" );
if( isset( $_GET['color'] ) )
{
$svg->path->addAttribute( 'fill', '#' . $_GET['color'] );
}
header( "Content-type: image/svg+xml" );
echo $svg->asXML( );
I'll be adding code to cache the queried custom SVG's, eventually.

Resources