Change svg color
I want to use this technique http://css-tricks.com/svg-fallbacks/ and change the svg color but so far I haven't been able to do so. I put this in the css but my image is always black, no matter what. My code:
.change-my-color {
fill: green;
}<svg>
<image class="change-my-color" xlink:href="https://svgur.com/i/AFM.svg" width="96" height="96" src="ppngfallback.png" />
</svg>css svg
add a comment |
I want to use this technique http://css-tricks.com/svg-fallbacks/ and change the svg color but so far I haven't been able to do so. I put this in the css but my image is always black, no matter what. My code:
.change-my-color {
fill: green;
}<svg>
<image class="change-my-color" xlink:href="https://svgur.com/i/AFM.svg" width="96" height="96" src="ppngfallback.png" />
</svg>css svg
I'm no svg expert, but have you tried changing fill to background-color?
– Meg
Mar 7 '14 at 14:29
@Megan in svg background-color is specified with the 'fill' property and the border with 'stroke' (as you would do in Illustrator). w3.org/TR/SVG/propidx.html
– Barbara
Mar 7 '14 at 14:36
1
What you're trying to do is not possible. The image is a separate document and you can't override any fill colours.
– Robert Longson
Mar 7 '14 at 14:38
3
CSS from your HTML document will not apply to SVG elements within <img />
– pawel
Mar 7 '14 at 14:38
add a comment |
I want to use this technique http://css-tricks.com/svg-fallbacks/ and change the svg color but so far I haven't been able to do so. I put this in the css but my image is always black, no matter what. My code:
.change-my-color {
fill: green;
}<svg>
<image class="change-my-color" xlink:href="https://svgur.com/i/AFM.svg" width="96" height="96" src="ppngfallback.png" />
</svg>css svg
I want to use this technique http://css-tricks.com/svg-fallbacks/ and change the svg color but so far I haven't been able to do so. I put this in the css but my image is always black, no matter what. My code:
.change-my-color {
fill: green;
}<svg>
<image class="change-my-color" xlink:href="https://svgur.com/i/AFM.svg" width="96" height="96" src="ppngfallback.png" />
</svg>.change-my-color {
fill: green;
}<svg>
<image class="change-my-color" xlink:href="https://svgur.com/i/AFM.svg" width="96" height="96" src="ppngfallback.png" />
</svg>.change-my-color {
fill: green;
}<svg>
<image class="change-my-color" xlink:href="https://svgur.com/i/AFM.svg" width="96" height="96" src="ppngfallback.png" />
</svg>css svg
css svg
edited Dec 31 '18 at 10:19
A-312
7,16742955
7,16742955
asked Mar 7 '14 at 14:15
BarbaraBarbara
1,42932241
1,42932241
I'm no svg expert, but have you tried changing fill to background-color?
– Meg
Mar 7 '14 at 14:29
@Megan in svg background-color is specified with the 'fill' property and the border with 'stroke' (as you would do in Illustrator). w3.org/TR/SVG/propidx.html
– Barbara
Mar 7 '14 at 14:36
1
What you're trying to do is not possible. The image is a separate document and you can't override any fill colours.
– Robert Longson
Mar 7 '14 at 14:38
3
CSS from your HTML document will not apply to SVG elements within <img />
– pawel
Mar 7 '14 at 14:38
add a comment |
I'm no svg expert, but have you tried changing fill to background-color?
– Meg
Mar 7 '14 at 14:29
@Megan in svg background-color is specified with the 'fill' property and the border with 'stroke' (as you would do in Illustrator). w3.org/TR/SVG/propidx.html
– Barbara
Mar 7 '14 at 14:36
1
What you're trying to do is not possible. The image is a separate document and you can't override any fill colours.
– Robert Longson
Mar 7 '14 at 14:38
3
CSS from your HTML document will not apply to SVG elements within <img />
– pawel
Mar 7 '14 at 14:38
I'm no svg expert, but have you tried changing fill to background-color?
– Meg
Mar 7 '14 at 14:29
I'm no svg expert, but have you tried changing fill to background-color?
– Meg
Mar 7 '14 at 14:29
@Megan in svg background-color is specified with the 'fill' property and the border with 'stroke' (as you would do in Illustrator). w3.org/TR/SVG/propidx.html
– Barbara
Mar 7 '14 at 14:36
@Megan in svg background-color is specified with the 'fill' property and the border with 'stroke' (as you would do in Illustrator). w3.org/TR/SVG/propidx.html
– Barbara
Mar 7 '14 at 14:36
1
1
What you're trying to do is not possible. The image is a separate document and you can't override any fill colours.
– Robert Longson
Mar 7 '14 at 14:38
What you're trying to do is not possible. The image is a separate document and you can't override any fill colours.
– Robert Longson
Mar 7 '14 at 14:38
3
3
CSS from your HTML document will not apply to SVG elements within <img />
– pawel
Mar 7 '14 at 14:38
CSS from your HTML document will not apply to SVG elements within <img />
– pawel
Mar 7 '14 at 14:38
add a comment |
10 Answers
10
active
oldest
votes
You can't change the color of an image that way. If you load SVG as an image, you can't change how it is displayed using CSS or Javascript in the browser.
If you want to change your SVG image, you have to load it using <object>, <iframe> or using <svg> inline.
If you want to use the techniques in the page, you need the Modernizr library, where you can check for SVG support and conditionally display or not a fallback image. You can then inline your SVG and apply the styles you need.
See :
#time-3-icon {
fill: green;
}
.my-svg-alternate {
display: none;
}
.no-svg .my-svg-alternate {
display: block;
width: 100px;
height: 100px;
background-image: url(image.png);
}<svg width="96px" height="96px" viewBox="0 0 512 512" enable-background="new 0 0 512 512" xml:space="preserve">
<path id="time-3-icon" d="M256,50C142.229,50,50,142.229,50,256c0,113.77,92.229,206,206,206c113.77,0,206-92.23,206-206
C462,142.229,369.77,50,256,50z M256,417c-88.977,0-161-72.008-161-161c0-88.979,72.008-161,161-161c88.977,0,161,72.007,161,161
C417,344.977,344.992,417,256,417z M382.816,265.785c1.711,0.297,2.961,1.781,2.961,3.518v0.093c0,1.72-1.223,3.188-2.914,3.505
c-37.093,6.938-124.97,21.35-134.613,21.35c-13.808,0-25-11.192-25-25c0-9.832,14.79-104.675,21.618-143.081
c0.274-1.542,1.615-2.669,3.181-2.669h0.008c1.709,0,3.164,1.243,3.431,2.932l18.933,119.904L382.816,265.785z"/>
</svg>
<image class="my-svg-alternate" width="96" height="96" src="ppngfallback.png" />You can inline your SVG, tag your fallback image with a class name (my-svg-alternate):
<svg width="96px" height="96px" viewBox="0 0 512 512" enable-background="new 0 0 512 512" xml:space="preserve">
<path id="time-3-icon" .../>
</svg>
<image class="my-svg-alternate" width="96" height="96" src="ppngfallback.png" />
And in CSS use the no-svg class from Modernizr (CDN: http://ajax.aspnetcdn.com/ajax/modernizr/modernizr-2.7.2.js ) to check for SVG support. If there is no SVG support the SVG block will be ignored and the image will be displayed, otherwise the image will be removed from the DOM tree (display: none):
.my-svg-alternate {
display: none;
}
.no-svg .my-svg-alternate {
display: block;
width: 100px;
height: 100px;
background-image: url(image.png);
}
Then you can change the color of your inlined element:
#time-3-icon {
fill: green;
}
3
You cannot style embeddedobjectSVGs from the hosting document.
– Javier Rey
Nov 11 '18 at 4:27
@JavierRey you could inject the styling into the object tag's contents via javascript. But you're right that it does not apply if you just add it to the hosting document's stylesheet.
– Robert Longson
Dec 31 '18 at 11:21
I'm using the solution from @manish-menaria and it works perfectly.
– Ryan Ellis
Mar 1 at 12:51
add a comment |
To change the color of any SVG you can directly change the svg code by opening the svg file in any text editor. The code may look like the below code
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<!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" x="0px" y="0px"
width="500px" height="500px" viewBox="0 0 500 500" enable-background="new 0 0 500 500" xml:space="preserve">
<g>
<path d="M114.26,436.584L99.023,483h301.953l-15.237-46.416H114.26z M161.629,474.404h-49.592l9.594-29.225h69.223
C181.113,454.921,171.371,464.663,161.629,474.404z"/>
/*Some more code goes on*/
</g>
</svg>
You can observe that there are some XML tags like path, circle, polygon etc. There you can add your own color with help of style attribute. Look at the below example
<path style="fill:#AB7C94;" d="M114.26,436.584L99.023,483h301.953l-15.237-46.416H114.26z M161.629,474.404h-49.592l9.594-29.225h69.223
C181.113,454.921,171.371,464.663,161.629,474.404z"/>
Add the style attribute to all the tags so that you can get your SVG of your required color
28
Why not just using attributefilllike this:fill = "#AB7C94"? Not sure why thestyleattribute is needed
– bg17aw
Jan 27 '17 at 19:24
1
Hi Daniel, yeah it works. I didn't knew that fill can be used as an attribute. Sorry for not noticing your comment so long @bg17aw
– sushant047
Apr 30 '18 at 9:08
add a comment |
Steps I follow to change any SVG's color:
- First, add the SVG image using img tags.
<img src="dotted-arrow.svg" class="filter-green"/>
- Use the following Codepen(Click Here to open codepen) to convert any HEX color code to filter:
For example, output for #00EE00 is
filter: invert(42%) sepia(93%) saturate(1352%) hue-rotate(87deg) brightness(119%) contrast(119%);
- Create a new class and add this filter into this class.
.filter-green{
filter: invert(48%) sepia(79%) saturate(2476%) hue-rotate(86deg) brightness(118%) contrast(119%);
}
- Now Add this class to image tag and color of SVG will be changed to HEX we provided.
3
This should be the accepted answer 100000%
– Ryan Ellis
Mar 1 at 12:49
2
This without a doubt should be the proper answer!
– Federico Navarrete
Mar 3 at 9:06
Saved my butt too. 🙏🏻
– kevincoleman
Mar 8 at 4:09
This comes with the usual caveat about not being supported in older browser versions: developer.mozilla.org/en-US/docs/Web/CSS/…
– Kevin Wang
Mar 20 at 19:00
add a comment |
Added a test page - to color SVG via Filter settings:
E.G
filter: invert(0.5) sepia(1) saturate(5) hue-rotate(175deg)
Upload & Color your SVG - Jsfiddle
Took the idea from: https://blog.union.io/code/2017/08/10/img-svg-fill/
1
Thanks, you just save me from myself..custom-disabled > svg {filter:invert(0.2) sepia(1) saturte(0) hue-rotate(0);}did just the job I need to disabled icon.
– roger
Mar 18 '18 at 10:29
1
This would of been great if it worked for ie11
– Huangism
Sep 20 '18 at 20:45
add a comment |
the easiest way would be to create a font out of the SVG using a service like https://icomoon.io/app/#/select or such. upload your SVG, click "generate font", include font files and css into your side and just use and style it like any other text. I always use it like this because it makes styling much easier.
EDIT: As mentioned in the article commented by @CodeMouse92 icon fonts mess up screen readers (and are possibly bad for SEO). So rather stick to the SVGs.
4
It also messes up screen readers. See "Death to Icon Fonts" by Seren Davies
– CodeMouse92
Mar 31 '17 at 20:55
add a comment |
Only SVG with path information. you can't do that to the image.. as the path you can change strock and fill information and you are done. like Illustrator
so: via CSS you can overwrite path fill value
path { fill: orange; }
but if you want more flexible way as you want to change it with a text when having some hovering effect going on.. use
path { fill: currentcolor; }
body {
background: #ddd;
text-align: center;
padding-top: 2em;
}
.parent {
width: 320px;
height: 50px;
display: block;
transition: all 0.3s;
cursor: pointer;
padding: 12px;
box-sizing: border-box;
}
/*** desired colors for children ***/
.parent{
color: #000;
background: #def;
}
.parent:hover{
color: #fff;
background: #85c1fc;
}
.parent span{
font-size: 18px;
margin-right: 8px;
font-weight: bold;
font-family: 'Helvetica';
line-height: 26px;
vertical-align: top;
}
.parent svg{
max-height: 26px;
width: auto;
display: inline;
}
/**** magic trick *****/
.parent svg path{
fill: currentcolor;
}<div class='parent'>
<span>TEXT WITH SVG</span>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="128" height="128" viewBox="0 0 32 32">
<path d="M30.148 5.588c-2.934-3.42-7.288-5.588-12.148-5.588-8.837 0-16 7.163-16 16s7.163 16 16 16c4.86 0 9.213-2.167 12.148-5.588l-10.148-10.412 10.148-10.412zM22 3.769c1.232 0 2.231 0.999 2.231 2.231s-0.999 2.231-2.231 2.231-2.231-0.999-2.231-2.231c0-1.232 0.999-2.231 2.231-2.231z"></path>
</svg>
</div>add a comment |
You can change SVG coloring with css if you use some tricks.
I wrote a small script for that.
- go through a list of elements which do have an svg image
- load the svg file as xml
- fetch only svg part
- change color of path
- replace src with the modified svg as inline image
$('img.svg-changeable').each(function () {
var $e = $(this);
var imgURL = $e.prop('src');
$.get(imgURL, function (data) {
// Get the SVG tag, ignore the rest
var $svg = $(data).find('svg');
// change the color
$svg.find('path').attr('fill', '#000');
$e.prop('src', "data:image/svg+xml;base64," + window.btoa($svg.prop('outerHTML')));
});
});
the code above might not be working correctly, I've implemented this for elements with an svg background image which works nearly similar to this.
But anyway you have to modify this script to fit your case. hope it helped.
By the way: If you are a RoR developer, you can add a new method for the sass precompiler which can do the job too. This is much better because you will have the base64 encoded, correct colored image in your compiled css file. No JS needed anymore! Maybe I could provide the code I have written, have to talk to the CTO.
– cydoc
Jul 17 '15 at 7:01
1
+1 for providing a solution, rather than saying it can't be done. This answer is also relevant: stackoverflow.com/questions/11978995/…
– claytronicon
Nov 27 '17 at 18:59
add a comment |
To simply change the color of the svg :
Go to the svg file and under styles, mention the color in fill.
<style>.cls-1{fill:#FFFFFF;}</style>
add a comment |
Target the path within the svg:
<svg>
<path>....
</svg>
You can do inline, like:
<path fill="#ccc">
Or
svg{
path{
fill: #ccc
add a comment |
If you want to do this to an inline svg that is, for example, a background image in your css:
background: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='rgba(31,159,215,1)' viewBox='...'/%3E%3C/svg%3E");of course, replace the ... with your inline image code
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f22252472%2fchange-svg-color%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
10 Answers
10
active
oldest
votes
10 Answers
10
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can't change the color of an image that way. If you load SVG as an image, you can't change how it is displayed using CSS or Javascript in the browser.
If you want to change your SVG image, you have to load it using <object>, <iframe> or using <svg> inline.
If you want to use the techniques in the page, you need the Modernizr library, where you can check for SVG support and conditionally display or not a fallback image. You can then inline your SVG and apply the styles you need.
See :
#time-3-icon {
fill: green;
}
.my-svg-alternate {
display: none;
}
.no-svg .my-svg-alternate {
display: block;
width: 100px;
height: 100px;
background-image: url(image.png);
}<svg width="96px" height="96px" viewBox="0 0 512 512" enable-background="new 0 0 512 512" xml:space="preserve">
<path id="time-3-icon" d="M256,50C142.229,50,50,142.229,50,256c0,113.77,92.229,206,206,206c113.77,0,206-92.23,206-206
C462,142.229,369.77,50,256,50z M256,417c-88.977,0-161-72.008-161-161c0-88.979,72.008-161,161-161c88.977,0,161,72.007,161,161
C417,344.977,344.992,417,256,417z M382.816,265.785c1.711,0.297,2.961,1.781,2.961,3.518v0.093c0,1.72-1.223,3.188-2.914,3.505
c-37.093,6.938-124.97,21.35-134.613,21.35c-13.808,0-25-11.192-25-25c0-9.832,14.79-104.675,21.618-143.081
c0.274-1.542,1.615-2.669,3.181-2.669h0.008c1.709,0,3.164,1.243,3.431,2.932l18.933,119.904L382.816,265.785z"/>
</svg>
<image class="my-svg-alternate" width="96" height="96" src="ppngfallback.png" />You can inline your SVG, tag your fallback image with a class name (my-svg-alternate):
<svg width="96px" height="96px" viewBox="0 0 512 512" enable-background="new 0 0 512 512" xml:space="preserve">
<path id="time-3-icon" .../>
</svg>
<image class="my-svg-alternate" width="96" height="96" src="ppngfallback.png" />
And in CSS use the no-svg class from Modernizr (CDN: http://ajax.aspnetcdn.com/ajax/modernizr/modernizr-2.7.2.js ) to check for SVG support. If there is no SVG support the SVG block will be ignored and the image will be displayed, otherwise the image will be removed from the DOM tree (display: none):
.my-svg-alternate {
display: none;
}
.no-svg .my-svg-alternate {
display: block;
width: 100px;
height: 100px;
background-image: url(image.png);
}
Then you can change the color of your inlined element:
#time-3-icon {
fill: green;
}
3
You cannot style embeddedobjectSVGs from the hosting document.
– Javier Rey
Nov 11 '18 at 4:27
@JavierRey you could inject the styling into the object tag's contents via javascript. But you're right that it does not apply if you just add it to the hosting document's stylesheet.
– Robert Longson
Dec 31 '18 at 11:21
I'm using the solution from @manish-menaria and it works perfectly.
– Ryan Ellis
Mar 1 at 12:51
add a comment |
You can't change the color of an image that way. If you load SVG as an image, you can't change how it is displayed using CSS or Javascript in the browser.
If you want to change your SVG image, you have to load it using <object>, <iframe> or using <svg> inline.
If you want to use the techniques in the page, you need the Modernizr library, where you can check for SVG support and conditionally display or not a fallback image. You can then inline your SVG and apply the styles you need.
See :
#time-3-icon {
fill: green;
}
.my-svg-alternate {
display: none;
}
.no-svg .my-svg-alternate {
display: block;
width: 100px;
height: 100px;
background-image: url(image.png);
}<svg width="96px" height="96px" viewBox="0 0 512 512" enable-background="new 0 0 512 512" xml:space="preserve">
<path id="time-3-icon" d="M256,50C142.229,50,50,142.229,50,256c0,113.77,92.229,206,206,206c113.77,0,206-92.23,206-206
C462,142.229,369.77,50,256,50z M256,417c-88.977,0-161-72.008-161-161c0-88.979,72.008-161,161-161c88.977,0,161,72.007,161,161
C417,344.977,344.992,417,256,417z M382.816,265.785c1.711,0.297,2.961,1.781,2.961,3.518v0.093c0,1.72-1.223,3.188-2.914,3.505
c-37.093,6.938-124.97,21.35-134.613,21.35c-13.808,0-25-11.192-25-25c0-9.832,14.79-104.675,21.618-143.081
c0.274-1.542,1.615-2.669,3.181-2.669h0.008c1.709,0,3.164,1.243,3.431,2.932l18.933,119.904L382.816,265.785z"/>
</svg>
<image class="my-svg-alternate" width="96" height="96" src="ppngfallback.png" />You can inline your SVG, tag your fallback image with a class name (my-svg-alternate):
<svg width="96px" height="96px" viewBox="0 0 512 512" enable-background="new 0 0 512 512" xml:space="preserve">
<path id="time-3-icon" .../>
</svg>
<image class="my-svg-alternate" width="96" height="96" src="ppngfallback.png" />
And in CSS use the no-svg class from Modernizr (CDN: http://ajax.aspnetcdn.com/ajax/modernizr/modernizr-2.7.2.js ) to check for SVG support. If there is no SVG support the SVG block will be ignored and the image will be displayed, otherwise the image will be removed from the DOM tree (display: none):
.my-svg-alternate {
display: none;
}
.no-svg .my-svg-alternate {
display: block;
width: 100px;
height: 100px;
background-image: url(image.png);
}
Then you can change the color of your inlined element:
#time-3-icon {
fill: green;
}
3
You cannot style embeddedobjectSVGs from the hosting document.
– Javier Rey
Nov 11 '18 at 4:27
@JavierRey you could inject the styling into the object tag's contents via javascript. But you're right that it does not apply if you just add it to the hosting document's stylesheet.
– Robert Longson
Dec 31 '18 at 11:21
I'm using the solution from @manish-menaria and it works perfectly.
– Ryan Ellis
Mar 1 at 12:51
add a comment |
You can't change the color of an image that way. If you load SVG as an image, you can't change how it is displayed using CSS or Javascript in the browser.
If you want to change your SVG image, you have to load it using <object>, <iframe> or using <svg> inline.
If you want to use the techniques in the page, you need the Modernizr library, where you can check for SVG support and conditionally display or not a fallback image. You can then inline your SVG and apply the styles you need.
See :
#time-3-icon {
fill: green;
}
.my-svg-alternate {
display: none;
}
.no-svg .my-svg-alternate {
display: block;
width: 100px;
height: 100px;
background-image: url(image.png);
}<svg width="96px" height="96px" viewBox="0 0 512 512" enable-background="new 0 0 512 512" xml:space="preserve">
<path id="time-3-icon" d="M256,50C142.229,50,50,142.229,50,256c0,113.77,92.229,206,206,206c113.77,0,206-92.23,206-206
C462,142.229,369.77,50,256,50z M256,417c-88.977,0-161-72.008-161-161c0-88.979,72.008-161,161-161c88.977,0,161,72.007,161,161
C417,344.977,344.992,417,256,417z M382.816,265.785c1.711,0.297,2.961,1.781,2.961,3.518v0.093c0,1.72-1.223,3.188-2.914,3.505
c-37.093,6.938-124.97,21.35-134.613,21.35c-13.808,0-25-11.192-25-25c0-9.832,14.79-104.675,21.618-143.081
c0.274-1.542,1.615-2.669,3.181-2.669h0.008c1.709,0,3.164,1.243,3.431,2.932l18.933,119.904L382.816,265.785z"/>
</svg>
<image class="my-svg-alternate" width="96" height="96" src="ppngfallback.png" />You can inline your SVG, tag your fallback image with a class name (my-svg-alternate):
<svg width="96px" height="96px" viewBox="0 0 512 512" enable-background="new 0 0 512 512" xml:space="preserve">
<path id="time-3-icon" .../>
</svg>
<image class="my-svg-alternate" width="96" height="96" src="ppngfallback.png" />
And in CSS use the no-svg class from Modernizr (CDN: http://ajax.aspnetcdn.com/ajax/modernizr/modernizr-2.7.2.js ) to check for SVG support. If there is no SVG support the SVG block will be ignored and the image will be displayed, otherwise the image will be removed from the DOM tree (display: none):
.my-svg-alternate {
display: none;
}
.no-svg .my-svg-alternate {
display: block;
width: 100px;
height: 100px;
background-image: url(image.png);
}
Then you can change the color of your inlined element:
#time-3-icon {
fill: green;
}
You can't change the color of an image that way. If you load SVG as an image, you can't change how it is displayed using CSS or Javascript in the browser.
If you want to change your SVG image, you have to load it using <object>, <iframe> or using <svg> inline.
If you want to use the techniques in the page, you need the Modernizr library, where you can check for SVG support and conditionally display or not a fallback image. You can then inline your SVG and apply the styles you need.
See :
#time-3-icon {
fill: green;
}
.my-svg-alternate {
display: none;
}
.no-svg .my-svg-alternate {
display: block;
width: 100px;
height: 100px;
background-image: url(image.png);
}<svg width="96px" height="96px" viewBox="0 0 512 512" enable-background="new 0 0 512 512" xml:space="preserve">
<path id="time-3-icon" d="M256,50C142.229,50,50,142.229,50,256c0,113.77,92.229,206,206,206c113.77,0,206-92.23,206-206
C462,142.229,369.77,50,256,50z M256,417c-88.977,0-161-72.008-161-161c0-88.979,72.008-161,161-161c88.977,0,161,72.007,161,161
C417,344.977,344.992,417,256,417z M382.816,265.785c1.711,0.297,2.961,1.781,2.961,3.518v0.093c0,1.72-1.223,3.188-2.914,3.505
c-37.093,6.938-124.97,21.35-134.613,21.35c-13.808,0-25-11.192-25-25c0-9.832,14.79-104.675,21.618-143.081
c0.274-1.542,1.615-2.669,3.181-2.669h0.008c1.709,0,3.164,1.243,3.431,2.932l18.933,119.904L382.816,265.785z"/>
</svg>
<image class="my-svg-alternate" width="96" height="96" src="ppngfallback.png" />You can inline your SVG, tag your fallback image with a class name (my-svg-alternate):
<svg width="96px" height="96px" viewBox="0 0 512 512" enable-background="new 0 0 512 512" xml:space="preserve">
<path id="time-3-icon" .../>
</svg>
<image class="my-svg-alternate" width="96" height="96" src="ppngfallback.png" />
And in CSS use the no-svg class from Modernizr (CDN: http://ajax.aspnetcdn.com/ajax/modernizr/modernizr-2.7.2.js ) to check for SVG support. If there is no SVG support the SVG block will be ignored and the image will be displayed, otherwise the image will be removed from the DOM tree (display: none):
.my-svg-alternate {
display: none;
}
.no-svg .my-svg-alternate {
display: block;
width: 100px;
height: 100px;
background-image: url(image.png);
}
Then you can change the color of your inlined element:
#time-3-icon {
fill: green;
}
#time-3-icon {
fill: green;
}
.my-svg-alternate {
display: none;
}
.no-svg .my-svg-alternate {
display: block;
width: 100px;
height: 100px;
background-image: url(image.png);
}<svg width="96px" height="96px" viewBox="0 0 512 512" enable-background="new 0 0 512 512" xml:space="preserve">
<path id="time-3-icon" d="M256,50C142.229,50,50,142.229,50,256c0,113.77,92.229,206,206,206c113.77,0,206-92.23,206-206
C462,142.229,369.77,50,256,50z M256,417c-88.977,0-161-72.008-161-161c0-88.979,72.008-161,161-161c88.977,0,161,72.007,161,161
C417,344.977,344.992,417,256,417z M382.816,265.785c1.711,0.297,2.961,1.781,2.961,3.518v0.093c0,1.72-1.223,3.188-2.914,3.505
c-37.093,6.938-124.97,21.35-134.613,21.35c-13.808,0-25-11.192-25-25c0-9.832,14.79-104.675,21.618-143.081
c0.274-1.542,1.615-2.669,3.181-2.669h0.008c1.709,0,3.164,1.243,3.431,2.932l18.933,119.904L382.816,265.785z"/>
</svg>
<image class="my-svg-alternate" width="96" height="96" src="ppngfallback.png" />#time-3-icon {
fill: green;
}
.my-svg-alternate {
display: none;
}
.no-svg .my-svg-alternate {
display: block;
width: 100px;
height: 100px;
background-image: url(image.png);
}<svg width="96px" height="96px" viewBox="0 0 512 512" enable-background="new 0 0 512 512" xml:space="preserve">
<path id="time-3-icon" d="M256,50C142.229,50,50,142.229,50,256c0,113.77,92.229,206,206,206c113.77,0,206-92.23,206-206
C462,142.229,369.77,50,256,50z M256,417c-88.977,0-161-72.008-161-161c0-88.979,72.008-161,161-161c88.977,0,161,72.007,161,161
C417,344.977,344.992,417,256,417z M382.816,265.785c1.711,0.297,2.961,1.781,2.961,3.518v0.093c0,1.72-1.223,3.188-2.914,3.505
c-37.093,6.938-124.97,21.35-134.613,21.35c-13.808,0-25-11.192-25-25c0-9.832,14.79-104.675,21.618-143.081
c0.274-1.542,1.615-2.669,3.181-2.669h0.008c1.709,0,3.164,1.243,3.431,2.932l18.933,119.904L382.816,265.785z"/>
</svg>
<image class="my-svg-alternate" width="96" height="96" src="ppngfallback.png" />edited Dec 31 '18 at 10:24
A-312
7,16742955
7,16742955
answered Mar 7 '14 at 14:39
helderdarochahelderdarocha
17.6k32746
17.6k32746
3
You cannot style embeddedobjectSVGs from the hosting document.
– Javier Rey
Nov 11 '18 at 4:27
@JavierRey you could inject the styling into the object tag's contents via javascript. But you're right that it does not apply if you just add it to the hosting document's stylesheet.
– Robert Longson
Dec 31 '18 at 11:21
I'm using the solution from @manish-menaria and it works perfectly.
– Ryan Ellis
Mar 1 at 12:51
add a comment |
3
You cannot style embeddedobjectSVGs from the hosting document.
– Javier Rey
Nov 11 '18 at 4:27
@JavierRey you could inject the styling into the object tag's contents via javascript. But you're right that it does not apply if you just add it to the hosting document's stylesheet.
– Robert Longson
Dec 31 '18 at 11:21
I'm using the solution from @manish-menaria and it works perfectly.
– Ryan Ellis
Mar 1 at 12:51
3
3
You cannot style embedded
object SVGs from the hosting document.– Javier Rey
Nov 11 '18 at 4:27
You cannot style embedded
object SVGs from the hosting document.– Javier Rey
Nov 11 '18 at 4:27
@JavierRey you could inject the styling into the object tag's contents via javascript. But you're right that it does not apply if you just add it to the hosting document's stylesheet.
– Robert Longson
Dec 31 '18 at 11:21
@JavierRey you could inject the styling into the object tag's contents via javascript. But you're right that it does not apply if you just add it to the hosting document's stylesheet.
– Robert Longson
Dec 31 '18 at 11:21
I'm using the solution from @manish-menaria and it works perfectly.
– Ryan Ellis
Mar 1 at 12:51
I'm using the solution from @manish-menaria and it works perfectly.
– Ryan Ellis
Mar 1 at 12:51
add a comment |
To change the color of any SVG you can directly change the svg code by opening the svg file in any text editor. The code may look like the below code
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<!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" x="0px" y="0px"
width="500px" height="500px" viewBox="0 0 500 500" enable-background="new 0 0 500 500" xml:space="preserve">
<g>
<path d="M114.26,436.584L99.023,483h301.953l-15.237-46.416H114.26z M161.629,474.404h-49.592l9.594-29.225h69.223
C181.113,454.921,171.371,464.663,161.629,474.404z"/>
/*Some more code goes on*/
</g>
</svg>
You can observe that there are some XML tags like path, circle, polygon etc. There you can add your own color with help of style attribute. Look at the below example
<path style="fill:#AB7C94;" d="M114.26,436.584L99.023,483h301.953l-15.237-46.416H114.26z M161.629,474.404h-49.592l9.594-29.225h69.223
C181.113,454.921,171.371,464.663,161.629,474.404z"/>
Add the style attribute to all the tags so that you can get your SVG of your required color
28
Why not just using attributefilllike this:fill = "#AB7C94"? Not sure why thestyleattribute is needed
– bg17aw
Jan 27 '17 at 19:24
1
Hi Daniel, yeah it works. I didn't knew that fill can be used as an attribute. Sorry for not noticing your comment so long @bg17aw
– sushant047
Apr 30 '18 at 9:08
add a comment |
To change the color of any SVG you can directly change the svg code by opening the svg file in any text editor. The code may look like the below code
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<!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" x="0px" y="0px"
width="500px" height="500px" viewBox="0 0 500 500" enable-background="new 0 0 500 500" xml:space="preserve">
<g>
<path d="M114.26,436.584L99.023,483h301.953l-15.237-46.416H114.26z M161.629,474.404h-49.592l9.594-29.225h69.223
C181.113,454.921,171.371,464.663,161.629,474.404z"/>
/*Some more code goes on*/
</g>
</svg>
You can observe that there are some XML tags like path, circle, polygon etc. There you can add your own color with help of style attribute. Look at the below example
<path style="fill:#AB7C94;" d="M114.26,436.584L99.023,483h301.953l-15.237-46.416H114.26z M161.629,474.404h-49.592l9.594-29.225h69.223
C181.113,454.921,171.371,464.663,161.629,474.404z"/>
Add the style attribute to all the tags so that you can get your SVG of your required color
28
Why not just using attributefilllike this:fill = "#AB7C94"? Not sure why thestyleattribute is needed
– bg17aw
Jan 27 '17 at 19:24
1
Hi Daniel, yeah it works. I didn't knew that fill can be used as an attribute. Sorry for not noticing your comment so long @bg17aw
– sushant047
Apr 30 '18 at 9:08
add a comment |
To change the color of any SVG you can directly change the svg code by opening the svg file in any text editor. The code may look like the below code
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<!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" x="0px" y="0px"
width="500px" height="500px" viewBox="0 0 500 500" enable-background="new 0 0 500 500" xml:space="preserve">
<g>
<path d="M114.26,436.584L99.023,483h301.953l-15.237-46.416H114.26z M161.629,474.404h-49.592l9.594-29.225h69.223
C181.113,454.921,171.371,464.663,161.629,474.404z"/>
/*Some more code goes on*/
</g>
</svg>
You can observe that there are some XML tags like path, circle, polygon etc. There you can add your own color with help of style attribute. Look at the below example
<path style="fill:#AB7C94;" d="M114.26,436.584L99.023,483h301.953l-15.237-46.416H114.26z M161.629,474.404h-49.592l9.594-29.225h69.223
C181.113,454.921,171.371,464.663,161.629,474.404z"/>
Add the style attribute to all the tags so that you can get your SVG of your required color
To change the color of any SVG you can directly change the svg code by opening the svg file in any text editor. The code may look like the below code
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<!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" x="0px" y="0px"
width="500px" height="500px" viewBox="0 0 500 500" enable-background="new 0 0 500 500" xml:space="preserve">
<g>
<path d="M114.26,436.584L99.023,483h301.953l-15.237-46.416H114.26z M161.629,474.404h-49.592l9.594-29.225h69.223
C181.113,454.921,171.371,464.663,161.629,474.404z"/>
/*Some more code goes on*/
</g>
</svg>
You can observe that there are some XML tags like path, circle, polygon etc. There you can add your own color with help of style attribute. Look at the below example
<path style="fill:#AB7C94;" d="M114.26,436.584L99.023,483h301.953l-15.237-46.416H114.26z M161.629,474.404h-49.592l9.594-29.225h69.223
C181.113,454.921,171.371,464.663,161.629,474.404z"/>
Add the style attribute to all the tags so that you can get your SVG of your required color
answered Dec 4 '14 at 6:10
sushant047sushant047
1,41611016
1,41611016
28
Why not just using attributefilllike this:fill = "#AB7C94"? Not sure why thestyleattribute is needed
– bg17aw
Jan 27 '17 at 19:24
1
Hi Daniel, yeah it works. I didn't knew that fill can be used as an attribute. Sorry for not noticing your comment so long @bg17aw
– sushant047
Apr 30 '18 at 9:08
add a comment |
28
Why not just using attributefilllike this:fill = "#AB7C94"? Not sure why thestyleattribute is needed
– bg17aw
Jan 27 '17 at 19:24
1
Hi Daniel, yeah it works. I didn't knew that fill can be used as an attribute. Sorry for not noticing your comment so long @bg17aw
– sushant047
Apr 30 '18 at 9:08
28
28
Why not just using attribute
fill like this: fill = "#AB7C94" ? Not sure why the style attribute is needed– bg17aw
Jan 27 '17 at 19:24
Why not just using attribute
fill like this: fill = "#AB7C94" ? Not sure why the style attribute is needed– bg17aw
Jan 27 '17 at 19:24
1
1
Hi Daniel, yeah it works. I didn't knew that fill can be used as an attribute. Sorry for not noticing your comment so long @bg17aw
– sushant047
Apr 30 '18 at 9:08
Hi Daniel, yeah it works. I didn't knew that fill can be used as an attribute. Sorry for not noticing your comment so long @bg17aw
– sushant047
Apr 30 '18 at 9:08
add a comment |
Steps I follow to change any SVG's color:
- First, add the SVG image using img tags.
<img src="dotted-arrow.svg" class="filter-green"/>
- Use the following Codepen(Click Here to open codepen) to convert any HEX color code to filter:
For example, output for #00EE00 is
filter: invert(42%) sepia(93%) saturate(1352%) hue-rotate(87deg) brightness(119%) contrast(119%);
- Create a new class and add this filter into this class.
.filter-green{
filter: invert(48%) sepia(79%) saturate(2476%) hue-rotate(86deg) brightness(118%) contrast(119%);
}
- Now Add this class to image tag and color of SVG will be changed to HEX we provided.
3
This should be the accepted answer 100000%
– Ryan Ellis
Mar 1 at 12:49
2
This without a doubt should be the proper answer!
– Federico Navarrete
Mar 3 at 9:06
Saved my butt too. 🙏🏻
– kevincoleman
Mar 8 at 4:09
This comes with the usual caveat about not being supported in older browser versions: developer.mozilla.org/en-US/docs/Web/CSS/…
– Kevin Wang
Mar 20 at 19:00
add a comment |
Steps I follow to change any SVG's color:
- First, add the SVG image using img tags.
<img src="dotted-arrow.svg" class="filter-green"/>
- Use the following Codepen(Click Here to open codepen) to convert any HEX color code to filter:
For example, output for #00EE00 is
filter: invert(42%) sepia(93%) saturate(1352%) hue-rotate(87deg) brightness(119%) contrast(119%);
- Create a new class and add this filter into this class.
.filter-green{
filter: invert(48%) sepia(79%) saturate(2476%) hue-rotate(86deg) brightness(118%) contrast(119%);
}
- Now Add this class to image tag and color of SVG will be changed to HEX we provided.
3
This should be the accepted answer 100000%
– Ryan Ellis
Mar 1 at 12:49
2
This without a doubt should be the proper answer!
– Federico Navarrete
Mar 3 at 9:06
Saved my butt too. 🙏🏻
– kevincoleman
Mar 8 at 4:09
This comes with the usual caveat about not being supported in older browser versions: developer.mozilla.org/en-US/docs/Web/CSS/…
– Kevin Wang
Mar 20 at 19:00
add a comment |
Steps I follow to change any SVG's color:
- First, add the SVG image using img tags.
<img src="dotted-arrow.svg" class="filter-green"/>
- Use the following Codepen(Click Here to open codepen) to convert any HEX color code to filter:
For example, output for #00EE00 is
filter: invert(42%) sepia(93%) saturate(1352%) hue-rotate(87deg) brightness(119%) contrast(119%);
- Create a new class and add this filter into this class.
.filter-green{
filter: invert(48%) sepia(79%) saturate(2476%) hue-rotate(86deg) brightness(118%) contrast(119%);
}
- Now Add this class to image tag and color of SVG will be changed to HEX we provided.
Steps I follow to change any SVG's color:
- First, add the SVG image using img tags.
<img src="dotted-arrow.svg" class="filter-green"/>
- Use the following Codepen(Click Here to open codepen) to convert any HEX color code to filter:
For example, output for #00EE00 is
filter: invert(42%) sepia(93%) saturate(1352%) hue-rotate(87deg) brightness(119%) contrast(119%);
- Create a new class and add this filter into this class.
.filter-green{
filter: invert(48%) sepia(79%) saturate(2476%) hue-rotate(86deg) brightness(118%) contrast(119%);
}
- Now Add this class to image tag and color of SVG will be changed to HEX we provided.
edited Mar 8 at 5:48
answered Nov 16 '18 at 11:14
Manish MenariaManish Menaria
1,1921019
1,1921019
3
This should be the accepted answer 100000%
– Ryan Ellis
Mar 1 at 12:49
2
This without a doubt should be the proper answer!
– Federico Navarrete
Mar 3 at 9:06
Saved my butt too. 🙏🏻
– kevincoleman
Mar 8 at 4:09
This comes with the usual caveat about not being supported in older browser versions: developer.mozilla.org/en-US/docs/Web/CSS/…
– Kevin Wang
Mar 20 at 19:00
add a comment |
3
This should be the accepted answer 100000%
– Ryan Ellis
Mar 1 at 12:49
2
This without a doubt should be the proper answer!
– Federico Navarrete
Mar 3 at 9:06
Saved my butt too. 🙏🏻
– kevincoleman
Mar 8 at 4:09
This comes with the usual caveat about not being supported in older browser versions: developer.mozilla.org/en-US/docs/Web/CSS/…
– Kevin Wang
Mar 20 at 19:00
3
3
This should be the accepted answer 100000%
– Ryan Ellis
Mar 1 at 12:49
This should be the accepted answer 100000%
– Ryan Ellis
Mar 1 at 12:49
2
2
This without a doubt should be the proper answer!
– Federico Navarrete
Mar 3 at 9:06
This without a doubt should be the proper answer!
– Federico Navarrete
Mar 3 at 9:06
Saved my butt too. 🙏🏻
– kevincoleman
Mar 8 at 4:09
Saved my butt too. 🙏🏻
– kevincoleman
Mar 8 at 4:09
This comes with the usual caveat about not being supported in older browser versions: developer.mozilla.org/en-US/docs/Web/CSS/…
– Kevin Wang
Mar 20 at 19:00
This comes with the usual caveat about not being supported in older browser versions: developer.mozilla.org/en-US/docs/Web/CSS/…
– Kevin Wang
Mar 20 at 19:00
add a comment |
Added a test page - to color SVG via Filter settings:
E.G
filter: invert(0.5) sepia(1) saturate(5) hue-rotate(175deg)
Upload & Color your SVG - Jsfiddle
Took the idea from: https://blog.union.io/code/2017/08/10/img-svg-fill/
1
Thanks, you just save me from myself..custom-disabled > svg {filter:invert(0.2) sepia(1) saturte(0) hue-rotate(0);}did just the job I need to disabled icon.
– roger
Mar 18 '18 at 10:29
1
This would of been great if it worked for ie11
– Huangism
Sep 20 '18 at 20:45
add a comment |
Added a test page - to color SVG via Filter settings:
E.G
filter: invert(0.5) sepia(1) saturate(5) hue-rotate(175deg)
Upload & Color your SVG - Jsfiddle
Took the idea from: https://blog.union.io/code/2017/08/10/img-svg-fill/
1
Thanks, you just save me from myself..custom-disabled > svg {filter:invert(0.2) sepia(1) saturte(0) hue-rotate(0);}did just the job I need to disabled icon.
– roger
Mar 18 '18 at 10:29
1
This would of been great if it worked for ie11
– Huangism
Sep 20 '18 at 20:45
add a comment |
Added a test page - to color SVG via Filter settings:
E.G
filter: invert(0.5) sepia(1) saturate(5) hue-rotate(175deg)
Upload & Color your SVG - Jsfiddle
Took the idea from: https://blog.union.io/code/2017/08/10/img-svg-fill/
Added a test page - to color SVG via Filter settings:
E.G
filter: invert(0.5) sepia(1) saturate(5) hue-rotate(175deg)
Upload & Color your SVG - Jsfiddle
Took the idea from: https://blog.union.io/code/2017/08/10/img-svg-fill/
edited Nov 6 '18 at 12:03
answered Mar 1 '18 at 11:42
Yonatan AyalonYonatan Ayalon
1,3811118
1,3811118
1
Thanks, you just save me from myself..custom-disabled > svg {filter:invert(0.2) sepia(1) saturte(0) hue-rotate(0);}did just the job I need to disabled icon.
– roger
Mar 18 '18 at 10:29
1
This would of been great if it worked for ie11
– Huangism
Sep 20 '18 at 20:45
add a comment |
1
Thanks, you just save me from myself..custom-disabled > svg {filter:invert(0.2) sepia(1) saturte(0) hue-rotate(0);}did just the job I need to disabled icon.
– roger
Mar 18 '18 at 10:29
1
This would of been great if it worked for ie11
– Huangism
Sep 20 '18 at 20:45
1
1
Thanks, you just save me from myself.
.custom-disabled > svg {filter:invert(0.2) sepia(1) saturte(0) hue-rotate(0);} did just the job I need to disabled icon.– roger
Mar 18 '18 at 10:29
Thanks, you just save me from myself.
.custom-disabled > svg {filter:invert(0.2) sepia(1) saturte(0) hue-rotate(0);} did just the job I need to disabled icon.– roger
Mar 18 '18 at 10:29
1
1
This would of been great if it worked for ie11
– Huangism
Sep 20 '18 at 20:45
This would of been great if it worked for ie11
– Huangism
Sep 20 '18 at 20:45
add a comment |
the easiest way would be to create a font out of the SVG using a service like https://icomoon.io/app/#/select or such. upload your SVG, click "generate font", include font files and css into your side and just use and style it like any other text. I always use it like this because it makes styling much easier.
EDIT: As mentioned in the article commented by @CodeMouse92 icon fonts mess up screen readers (and are possibly bad for SEO). So rather stick to the SVGs.
4
It also messes up screen readers. See "Death to Icon Fonts" by Seren Davies
– CodeMouse92
Mar 31 '17 at 20:55
add a comment |
the easiest way would be to create a font out of the SVG using a service like https://icomoon.io/app/#/select or such. upload your SVG, click "generate font", include font files and css into your side and just use and style it like any other text. I always use it like this because it makes styling much easier.
EDIT: As mentioned in the article commented by @CodeMouse92 icon fonts mess up screen readers (and are possibly bad for SEO). So rather stick to the SVGs.
4
It also messes up screen readers. See "Death to Icon Fonts" by Seren Davies
– CodeMouse92
Mar 31 '17 at 20:55
add a comment |
the easiest way would be to create a font out of the SVG using a service like https://icomoon.io/app/#/select or such. upload your SVG, click "generate font", include font files and css into your side and just use and style it like any other text. I always use it like this because it makes styling much easier.
EDIT: As mentioned in the article commented by @CodeMouse92 icon fonts mess up screen readers (and are possibly bad for SEO). So rather stick to the SVGs.
the easiest way would be to create a font out of the SVG using a service like https://icomoon.io/app/#/select or such. upload your SVG, click "generate font", include font files and css into your side and just use and style it like any other text. I always use it like this because it makes styling much easier.
EDIT: As mentioned in the article commented by @CodeMouse92 icon fonts mess up screen readers (and are possibly bad for SEO). So rather stick to the SVGs.
edited Jun 8 '17 at 9:50
answered Apr 16 '15 at 14:55
Felix HagspielFelix Hagspiel
9791121
9791121
4
It also messes up screen readers. See "Death to Icon Fonts" by Seren Davies
– CodeMouse92
Mar 31 '17 at 20:55
add a comment |
4
It also messes up screen readers. See "Death to Icon Fonts" by Seren Davies
– CodeMouse92
Mar 31 '17 at 20:55
4
4
It also messes up screen readers. See "Death to Icon Fonts" by Seren Davies
– CodeMouse92
Mar 31 '17 at 20:55
It also messes up screen readers. See "Death to Icon Fonts" by Seren Davies
– CodeMouse92
Mar 31 '17 at 20:55
add a comment |
Only SVG with path information. you can't do that to the image.. as the path you can change strock and fill information and you are done. like Illustrator
so: via CSS you can overwrite path fill value
path { fill: orange; }
but if you want more flexible way as you want to change it with a text when having some hovering effect going on.. use
path { fill: currentcolor; }
body {
background: #ddd;
text-align: center;
padding-top: 2em;
}
.parent {
width: 320px;
height: 50px;
display: block;
transition: all 0.3s;
cursor: pointer;
padding: 12px;
box-sizing: border-box;
}
/*** desired colors for children ***/
.parent{
color: #000;
background: #def;
}
.parent:hover{
color: #fff;
background: #85c1fc;
}
.parent span{
font-size: 18px;
margin-right: 8px;
font-weight: bold;
font-family: 'Helvetica';
line-height: 26px;
vertical-align: top;
}
.parent svg{
max-height: 26px;
width: auto;
display: inline;
}
/**** magic trick *****/
.parent svg path{
fill: currentcolor;
}<div class='parent'>
<span>TEXT WITH SVG</span>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="128" height="128" viewBox="0 0 32 32">
<path d="M30.148 5.588c-2.934-3.42-7.288-5.588-12.148-5.588-8.837 0-16 7.163-16 16s7.163 16 16 16c4.86 0 9.213-2.167 12.148-5.588l-10.148-10.412 10.148-10.412zM22 3.769c1.232 0 2.231 0.999 2.231 2.231s-0.999 2.231-2.231 2.231-2.231-0.999-2.231-2.231c0-1.232 0.999-2.231 2.231-2.231z"></path>
</svg>
</div>add a comment |
Only SVG with path information. you can't do that to the image.. as the path you can change strock and fill information and you are done. like Illustrator
so: via CSS you can overwrite path fill value
path { fill: orange; }
but if you want more flexible way as you want to change it with a text when having some hovering effect going on.. use
path { fill: currentcolor; }
body {
background: #ddd;
text-align: center;
padding-top: 2em;
}
.parent {
width: 320px;
height: 50px;
display: block;
transition: all 0.3s;
cursor: pointer;
padding: 12px;
box-sizing: border-box;
}
/*** desired colors for children ***/
.parent{
color: #000;
background: #def;
}
.parent:hover{
color: #fff;
background: #85c1fc;
}
.parent span{
font-size: 18px;
margin-right: 8px;
font-weight: bold;
font-family: 'Helvetica';
line-height: 26px;
vertical-align: top;
}
.parent svg{
max-height: 26px;
width: auto;
display: inline;
}
/**** magic trick *****/
.parent svg path{
fill: currentcolor;
}<div class='parent'>
<span>TEXT WITH SVG</span>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="128" height="128" viewBox="0 0 32 32">
<path d="M30.148 5.588c-2.934-3.42-7.288-5.588-12.148-5.588-8.837 0-16 7.163-16 16s7.163 16 16 16c4.86 0 9.213-2.167 12.148-5.588l-10.148-10.412 10.148-10.412zM22 3.769c1.232 0 2.231 0.999 2.231 2.231s-0.999 2.231-2.231 2.231-2.231-0.999-2.231-2.231c0-1.232 0.999-2.231 2.231-2.231z"></path>
</svg>
</div>add a comment |
Only SVG with path information. you can't do that to the image.. as the path you can change strock and fill information and you are done. like Illustrator
so: via CSS you can overwrite path fill value
path { fill: orange; }
but if you want more flexible way as you want to change it with a text when having some hovering effect going on.. use
path { fill: currentcolor; }
body {
background: #ddd;
text-align: center;
padding-top: 2em;
}
.parent {
width: 320px;
height: 50px;
display: block;
transition: all 0.3s;
cursor: pointer;
padding: 12px;
box-sizing: border-box;
}
/*** desired colors for children ***/
.parent{
color: #000;
background: #def;
}
.parent:hover{
color: #fff;
background: #85c1fc;
}
.parent span{
font-size: 18px;
margin-right: 8px;
font-weight: bold;
font-family: 'Helvetica';
line-height: 26px;
vertical-align: top;
}
.parent svg{
max-height: 26px;
width: auto;
display: inline;
}
/**** magic trick *****/
.parent svg path{
fill: currentcolor;
}<div class='parent'>
<span>TEXT WITH SVG</span>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="128" height="128" viewBox="0 0 32 32">
<path d="M30.148 5.588c-2.934-3.42-7.288-5.588-12.148-5.588-8.837 0-16 7.163-16 16s7.163 16 16 16c4.86 0 9.213-2.167 12.148-5.588l-10.148-10.412 10.148-10.412zM22 3.769c1.232 0 2.231 0.999 2.231 2.231s-0.999 2.231-2.231 2.231-2.231-0.999-2.231-2.231c0-1.232 0.999-2.231 2.231-2.231z"></path>
</svg>
</div>Only SVG with path information. you can't do that to the image.. as the path you can change strock and fill information and you are done. like Illustrator
so: via CSS you can overwrite path fill value
path { fill: orange; }
but if you want more flexible way as you want to change it with a text when having some hovering effect going on.. use
path { fill: currentcolor; }
body {
background: #ddd;
text-align: center;
padding-top: 2em;
}
.parent {
width: 320px;
height: 50px;
display: block;
transition: all 0.3s;
cursor: pointer;
padding: 12px;
box-sizing: border-box;
}
/*** desired colors for children ***/
.parent{
color: #000;
background: #def;
}
.parent:hover{
color: #fff;
background: #85c1fc;
}
.parent span{
font-size: 18px;
margin-right: 8px;
font-weight: bold;
font-family: 'Helvetica';
line-height: 26px;
vertical-align: top;
}
.parent svg{
max-height: 26px;
width: auto;
display: inline;
}
/**** magic trick *****/
.parent svg path{
fill: currentcolor;
}<div class='parent'>
<span>TEXT WITH SVG</span>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="128" height="128" viewBox="0 0 32 32">
<path d="M30.148 5.588c-2.934-3.42-7.288-5.588-12.148-5.588-8.837 0-16 7.163-16 16s7.163 16 16 16c4.86 0 9.213-2.167 12.148-5.588l-10.148-10.412 10.148-10.412zM22 3.769c1.232 0 2.231 0.999 2.231 2.231s-0.999 2.231-2.231 2.231-2.231-0.999-2.231-2.231c0-1.232 0.999-2.231 2.231-2.231z"></path>
</svg>
</div>body {
background: #ddd;
text-align: center;
padding-top: 2em;
}
.parent {
width: 320px;
height: 50px;
display: block;
transition: all 0.3s;
cursor: pointer;
padding: 12px;
box-sizing: border-box;
}
/*** desired colors for children ***/
.parent{
color: #000;
background: #def;
}
.parent:hover{
color: #fff;
background: #85c1fc;
}
.parent span{
font-size: 18px;
margin-right: 8px;
font-weight: bold;
font-family: 'Helvetica';
line-height: 26px;
vertical-align: top;
}
.parent svg{
max-height: 26px;
width: auto;
display: inline;
}
/**** magic trick *****/
.parent svg path{
fill: currentcolor;
}<div class='parent'>
<span>TEXT WITH SVG</span>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="128" height="128" viewBox="0 0 32 32">
<path d="M30.148 5.588c-2.934-3.42-7.288-5.588-12.148-5.588-8.837 0-16 7.163-16 16s7.163 16 16 16c4.86 0 9.213-2.167 12.148-5.588l-10.148-10.412 10.148-10.412zM22 3.769c1.232 0 2.231 0.999 2.231 2.231s-0.999 2.231-2.231 2.231-2.231-0.999-2.231-2.231c0-1.232 0.999-2.231 2.231-2.231z"></path>
</svg>
</div>body {
background: #ddd;
text-align: center;
padding-top: 2em;
}
.parent {
width: 320px;
height: 50px;
display: block;
transition: all 0.3s;
cursor: pointer;
padding: 12px;
box-sizing: border-box;
}
/*** desired colors for children ***/
.parent{
color: #000;
background: #def;
}
.parent:hover{
color: #fff;
background: #85c1fc;
}
.parent span{
font-size: 18px;
margin-right: 8px;
font-weight: bold;
font-family: 'Helvetica';
line-height: 26px;
vertical-align: top;
}
.parent svg{
max-height: 26px;
width: auto;
display: inline;
}
/**** magic trick *****/
.parent svg path{
fill: currentcolor;
}<div class='parent'>
<span>TEXT WITH SVG</span>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="128" height="128" viewBox="0 0 32 32">
<path d="M30.148 5.588c-2.934-3.42-7.288-5.588-12.148-5.588-8.837 0-16 7.163-16 16s7.163 16 16 16c4.86 0 9.213-2.167 12.148-5.588l-10.148-10.412 10.148-10.412zM22 3.769c1.232 0 2.231 0.999 2.231 2.231s-0.999 2.231-2.231 2.231-2.231-0.999-2.231-2.231c0-1.232 0.999-2.231 2.231-2.231z"></path>
</svg>
</div>answered Apr 3 '18 at 10:05
Biskrem MuhammadBiskrem Muhammad
85811517
85811517
add a comment |
add a comment |
You can change SVG coloring with css if you use some tricks.
I wrote a small script for that.
- go through a list of elements which do have an svg image
- load the svg file as xml
- fetch only svg part
- change color of path
- replace src with the modified svg as inline image
$('img.svg-changeable').each(function () {
var $e = $(this);
var imgURL = $e.prop('src');
$.get(imgURL, function (data) {
// Get the SVG tag, ignore the rest
var $svg = $(data).find('svg');
// change the color
$svg.find('path').attr('fill', '#000');
$e.prop('src', "data:image/svg+xml;base64," + window.btoa($svg.prop('outerHTML')));
});
});
the code above might not be working correctly, I've implemented this for elements with an svg background image which works nearly similar to this.
But anyway you have to modify this script to fit your case. hope it helped.
By the way: If you are a RoR developer, you can add a new method for the sass precompiler which can do the job too. This is much better because you will have the base64 encoded, correct colored image in your compiled css file. No JS needed anymore! Maybe I could provide the code I have written, have to talk to the CTO.
– cydoc
Jul 17 '15 at 7:01
1
+1 for providing a solution, rather than saying it can't be done. This answer is also relevant: stackoverflow.com/questions/11978995/…
– claytronicon
Nov 27 '17 at 18:59
add a comment |
You can change SVG coloring with css if you use some tricks.
I wrote a small script for that.
- go through a list of elements which do have an svg image
- load the svg file as xml
- fetch only svg part
- change color of path
- replace src with the modified svg as inline image
$('img.svg-changeable').each(function () {
var $e = $(this);
var imgURL = $e.prop('src');
$.get(imgURL, function (data) {
// Get the SVG tag, ignore the rest
var $svg = $(data).find('svg');
// change the color
$svg.find('path').attr('fill', '#000');
$e.prop('src', "data:image/svg+xml;base64," + window.btoa($svg.prop('outerHTML')));
});
});
the code above might not be working correctly, I've implemented this for elements with an svg background image which works nearly similar to this.
But anyway you have to modify this script to fit your case. hope it helped.
By the way: If you are a RoR developer, you can add a new method for the sass precompiler which can do the job too. This is much better because you will have the base64 encoded, correct colored image in your compiled css file. No JS needed anymore! Maybe I could provide the code I have written, have to talk to the CTO.
– cydoc
Jul 17 '15 at 7:01
1
+1 for providing a solution, rather than saying it can't be done. This answer is also relevant: stackoverflow.com/questions/11978995/…
– claytronicon
Nov 27 '17 at 18:59
add a comment |
You can change SVG coloring with css if you use some tricks.
I wrote a small script for that.
- go through a list of elements which do have an svg image
- load the svg file as xml
- fetch only svg part
- change color of path
- replace src with the modified svg as inline image
$('img.svg-changeable').each(function () {
var $e = $(this);
var imgURL = $e.prop('src');
$.get(imgURL, function (data) {
// Get the SVG tag, ignore the rest
var $svg = $(data).find('svg');
// change the color
$svg.find('path').attr('fill', '#000');
$e.prop('src', "data:image/svg+xml;base64," + window.btoa($svg.prop('outerHTML')));
});
});
the code above might not be working correctly, I've implemented this for elements with an svg background image which works nearly similar to this.
But anyway you have to modify this script to fit your case. hope it helped.
You can change SVG coloring with css if you use some tricks.
I wrote a small script for that.
- go through a list of elements which do have an svg image
- load the svg file as xml
- fetch only svg part
- change color of path
- replace src with the modified svg as inline image
$('img.svg-changeable').each(function () {
var $e = $(this);
var imgURL = $e.prop('src');
$.get(imgURL, function (data) {
// Get the SVG tag, ignore the rest
var $svg = $(data).find('svg');
// change the color
$svg.find('path').attr('fill', '#000');
$e.prop('src', "data:image/svg+xml;base64," + window.btoa($svg.prop('outerHTML')));
});
});
the code above might not be working correctly, I've implemented this for elements with an svg background image which works nearly similar to this.
But anyway you have to modify this script to fit your case. hope it helped.
edited Apr 29 '15 at 7:00
answered Apr 28 '15 at 12:14
cydoccydoc
1215
1215
By the way: If you are a RoR developer, you can add a new method for the sass precompiler which can do the job too. This is much better because you will have the base64 encoded, correct colored image in your compiled css file. No JS needed anymore! Maybe I could provide the code I have written, have to talk to the CTO.
– cydoc
Jul 17 '15 at 7:01
1
+1 for providing a solution, rather than saying it can't be done. This answer is also relevant: stackoverflow.com/questions/11978995/…
– claytronicon
Nov 27 '17 at 18:59
add a comment |
By the way: If you are a RoR developer, you can add a new method for the sass precompiler which can do the job too. This is much better because you will have the base64 encoded, correct colored image in your compiled css file. No JS needed anymore! Maybe I could provide the code I have written, have to talk to the CTO.
– cydoc
Jul 17 '15 at 7:01
1
+1 for providing a solution, rather than saying it can't be done. This answer is also relevant: stackoverflow.com/questions/11978995/…
– claytronicon
Nov 27 '17 at 18:59
By the way: If you are a RoR developer, you can add a new method for the sass precompiler which can do the job too. This is much better because you will have the base64 encoded, correct colored image in your compiled css file. No JS needed anymore! Maybe I could provide the code I have written, have to talk to the CTO.
– cydoc
Jul 17 '15 at 7:01
By the way: If you are a RoR developer, you can add a new method for the sass precompiler which can do the job too. This is much better because you will have the base64 encoded, correct colored image in your compiled css file. No JS needed anymore! Maybe I could provide the code I have written, have to talk to the CTO.
– cydoc
Jul 17 '15 at 7:01
1
1
+1 for providing a solution, rather than saying it can't be done. This answer is also relevant: stackoverflow.com/questions/11978995/…
– claytronicon
Nov 27 '17 at 18:59
+1 for providing a solution, rather than saying it can't be done. This answer is also relevant: stackoverflow.com/questions/11978995/…
– claytronicon
Nov 27 '17 at 18:59
add a comment |
To simply change the color of the svg :
Go to the svg file and under styles, mention the color in fill.
<style>.cls-1{fill:#FFFFFF;}</style>
add a comment |
To simply change the color of the svg :
Go to the svg file and under styles, mention the color in fill.
<style>.cls-1{fill:#FFFFFF;}</style>
add a comment |
To simply change the color of the svg :
Go to the svg file and under styles, mention the color in fill.
<style>.cls-1{fill:#FFFFFF;}</style>
To simply change the color of the svg :
Go to the svg file and under styles, mention the color in fill.
<style>.cls-1{fill:#FFFFFF;}</style>
answered Apr 30 '18 at 10:42
Sethu SathyanSethu Sathyan
393
393
add a comment |
add a comment |
Target the path within the svg:
<svg>
<path>....
</svg>
You can do inline, like:
<path fill="#ccc">
Or
svg{
path{
fill: #ccc
add a comment |
Target the path within the svg:
<svg>
<path>....
</svg>
You can do inline, like:
<path fill="#ccc">
Or
svg{
path{
fill: #ccc
add a comment |
Target the path within the svg:
<svg>
<path>....
</svg>
You can do inline, like:
<path fill="#ccc">
Or
svg{
path{
fill: #ccc
Target the path within the svg:
<svg>
<path>....
</svg>
You can do inline, like:
<path fill="#ccc">
Or
svg{
path{
fill: #ccc
answered Feb 15 at 13:50
Michael PhilipsMichael Philips
645722
645722
add a comment |
add a comment |
If you want to do this to an inline svg that is, for example, a background image in your css:
background: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='rgba(31,159,215,1)' viewBox='...'/%3E%3C/svg%3E");of course, replace the ... with your inline image code
add a comment |
If you want to do this to an inline svg that is, for example, a background image in your css:
background: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='rgba(31,159,215,1)' viewBox='...'/%3E%3C/svg%3E");of course, replace the ... with your inline image code
add a comment |
If you want to do this to an inline svg that is, for example, a background image in your css:
background: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='rgba(31,159,215,1)' viewBox='...'/%3E%3C/svg%3E");of course, replace the ... with your inline image code
If you want to do this to an inline svg that is, for example, a background image in your css:
background: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='rgba(31,159,215,1)' viewBox='...'/%3E%3C/svg%3E");of course, replace the ... with your inline image code
background: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='rgba(31,159,215,1)' viewBox='...'/%3E%3C/svg%3E");background: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='rgba(31,159,215,1)' viewBox='...'/%3E%3C/svg%3E");answered Jun 7 '18 at 20:24
Vision HiveVision Hive
3,323287
3,323287
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f22252472%2fchange-svg-color%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I'm no svg expert, but have you tried changing fill to background-color?
– Meg
Mar 7 '14 at 14:29
@Megan in svg background-color is specified with the 'fill' property and the border with 'stroke' (as you would do in Illustrator). w3.org/TR/SVG/propidx.html
– Barbara
Mar 7 '14 at 14:36
1
What you're trying to do is not possible. The image is a separate document and you can't override any fill colours.
– Robert Longson
Mar 7 '14 at 14:38
3
CSS from your HTML document will not apply to SVG elements within <img />
– pawel
Mar 7 '14 at 14:38