SVG icons have some advantages over font icons: they can be scaled to fit a variable-sized container element, and you can theoretically change color of individual paths. I also like the fact that I can easily make them in Inkscape :P
But how do I move the SVG in the CSS file so they can be reused on the same page, like icon fonts, and still benefit from these advantages?
The background property supports SVG, with background: url(#svg_element)
, but that means I have to put the SVG in the HTML :|
If I put it as a "data" string, how do I change path colors in the same css file?
For in-depth discussion, check out Sara Soueidan who has done a lot of work on SVG. A recent article/talk she gave may also have lots of info for you to digest.
Chris Coyer has some very useful things to say too.
I've worked with SVG before but not with SVG icons specifically. However based on the knowledge I have and doing some looking around, here is what I came up with (I might not have understood your question fully in that case please post more questions).
SVG is basically XML, so not exactly sure why you prefer not to put SVG in HTML file. If you put SVG in HTML file you could just use external or internal CSS to style SVG. Here is a sample to just do that.
In HTML file:
In a CSS file: (Include CSS file in you HTML file)
Here is the second option of many other, use
<img/>
tag. Here is an article which explains how to use SVG from a separate file using<img/>
tag. And here is the code accompanying it. Also note that it uses jQuery plugin svginject.Here are few other options to keep SVG out of HTML and their merits.
Hope this helps.
To meet this, Two scenarios are available to embed
svg
files into thehtml
:<object data="~/img/file.svg" type="image/svg+xml"></object>
element, Soxml
will be separated fromhtml
==> the resulted view would be combination ofhtml
andxml
that can be styled withcss
(perhaps ascoped
one e.g. using<style scoped="scoped"></style>
at the target page).<img src="~/img/file.svg"/>
element, Soxml
will be separated fromhtml
==> the resulted view would be onlyhtml
andcss
can't perform onxml
contents ofsvg
file.Another way to employ
svg
is to use inline technique, say, using<svg>
tag so you can use inline stylesheets too for this, But you wanna separatexml
fromhtml
here:)For more information: Using SVG and SVG Styling with CSS.
With svg templates
Lets create a svg template.
Template (html)
Its a template so we hide it. (but still in the DOM)
.hide { display: none;} //css
Use (html)
This can be reused anywhere on the page.
Easy, just style it!
css
Working example? Here you go: Fiddle
Browser support? Not 100% sure, but svg support in all big browsers: CanIUseIt
You could also do some digging into the evil-icons project. You'll find out some neat ways of using svg.
Their nicest idea is to merge all of your icons into a single file "sprite.svg". Each of your svg icons needs to be enclosed by a
<symbol>
tag with an id. Then you access it whenever you need through an xlink, as commented in other answers.By loading the entire sprite.svg you'll gain in load time and neatness.
If you use SVG as background image then you cannot alter path and fill colors (AFAIK). However, you can use white SVG and background colors to create Metro-ish icons. This is exactly what jQuery Mobile theming system does.
Another option is to use a colored SVG and use CSS3 hue-rotate filter to change the color of SVG. This property is not widely supported yet.
Note: Hue rotation only affects "colors"; black, white and gray remain unaffected.