Transition to SVG icons - how to separate them from the code?

SVG icons have some advantages over font icons: they can be scaled to fit a variable-sized container, and you can theoretically change the color of individual paths. I also like that I can easily make them in Inkscape: P

But how do I transfer the SVG to a CSS file so that they can be reused on the same page, such as icon fonts, and still take advantage of these?

The background property supports SVG with background: url(#svg_element) , but that means I have to put the SVG in HTML: | If I put it as the string "data", how can I change the colors of the path in the same css file?

+5
source share
7 answers

But how do I transfer the SVG to a CSS file so that they can be reused on the same page, such as icon fonts, and still take advantage of these?

With svg templates

Allows you to create an svg template.

Template (html)

 <svg width="100px" height="100px" viewBox="0 0 100 100" class="hide"> <circle id="circle-tmp" cx="50" cy="50" r="50" /> </svg> 

His template, so we hide it. (but still in the DOM)

.hide { display: none;} //css

Use (html)

 <svg viewBox="0 0 100 100 class="circle-first"> <use xlink:href="#circle-tmp" /> </svg> 

This can be reused on the page.

How to change path colors in the same css file?

Easy, simple in style!

CSS

 .circle-first { fill: #12bb34; } 

Working example ? Here you are: Fiddle

Browser support? Not 100% sure, but svg support in all big browsers: CanIUseIt

+11
source

For an in-depth discussion, check out Sarah Suydan , who worked a lot on SVG. The recent article / conversation she gave may also contain a lot of information for you to digest.

Chris Coyer has some very useful things to say too.

+4
source

If you use SVG as a background image, you cannot change the colors and fill colors (AFAIK). However, you can use white SVGs and background colors to create Metro-ish icons. This is just what jQuery Mobile System is .

 .icon { display: inline-block; width: 80px; height: 80px; background-size: 80%; background-position: center; background-repeat: no-repeat; } .icon-1 { background-image: url("data:image/svg+xml;charset=US-ASCII,%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22iso-8859-1%22%3F%3E%3C!DOCTYPE%20svg%20PUBLIC%20%22-%2F%2FW3C%2F%2FDTD%20SVG%201.1%2F%2FEN%22%20%22http%3A%2F%2Fwww.w3.org%2FGraphics%2FSVG%2F1.1%2FDTD%2Fsvg11.dtd%22%3E%3Csvg%20version%3D%221.1%22%20id%3D%22Layer_1%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%20x%3D%220px%22%20y%3D%220px%22%20%20width%3D%2214px%22%20height%3D%2214px%22%20viewBox%3D%220%200%2014%2014%22%20style%3D%22enable-background%3Anew%200%200%2014%2014%3B%22%20xml%3Aspace%3D%22preserve%22%3E%3Cpolygon%20style%3D%22fill%3A%23FFFFFF%3B%22%20points%3D%2214%2C5%209%2C5%207%2C0%205%2C5%200%2C5%204%2C8%202.625%2C13%207%2C10%2011.375%2C13%2010%2C8%20%22%2F%3E%3C%2Fsvg%3E"); } .icon-bg-1 { background-color: #800080; } .icon-bg-2 { background-color: #DB7093; } .icon-bg-3 { background-color: #CD853F; } .icon-bg-4 { background-color: #B0E0E6; } 
 <span class="icon icon-1 icon-bg-1"></span> <span class="icon icon-1 icon-bg-2"></span> <span class="icon icon-1 icon-bg-3"></span> <span class="icon icon-1 icon-bg-4"></span> 

Another option is to use color SVG and use the CSS3 hue-rotate filter to change the color of the SVG. This property is not yet supported.

Note: the rotation of the hue affects only the "colors"; black, white and gray remain unchanged.

 .icon { display: inline-block; width: 80px; height: 80px; background-size: 80%; background-position: center; background-repeat: no-repeat; background-color: #EEEEEE; } .icon-1 { background-image: url("data:image/svg+xml;charset=US-ASCII,%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22iso-8859-1%22%3F%3E%3C!DOCTYPE%20svg%20PUBLIC%20%22-%2F%2FW3C%2F%2FDTD%20SVG%201.1%2F%2FEN%22%20%22http%3A%2F%2Fwww.w3.org%2FGraphics%2FSVG%2F1.1%2FDTD%2Fsvg11.dtd%22%3E%3Csvg%20version%3D%221.1%22%20id%3D%22Layer_1%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%20x%3D%220px%22%20y%3D%220px%22%20%20width%3D%2214px%22%20height%3D%2214px%22%20viewBox%3D%220%200%2014%2014%22%20style%3D%22enable-background%3Anew%200%200%2014%2014%3B%22%20xml%3Aspace%3D%22preserve%22%3E%3Cpolygon%20style%3D%22fill%3A%23FF0000%3B%22%20points%3D%2214%2C5%209%2C5%207%2C0%205%2C5%200%2C5%204%2C8%202.625%2C13%207%2C10%2011.375%2C13%2010%2C8%20%22%2F%3E%3C%2Fsvg%3E"); } .icon-hue-1 { -webkit-filter: hue-rotate(0deg); -moz-filter: hue-rotate(0deg); filter: hue-rotate(0deg); } .icon-hue-2 { -webkit-filter: hue-rotate(90deg); -moz-filter: hue-rotate(90deg); filter: hue-rotate(90deg); } .icon-hue-3 { -webkit-filter: hue-rotate(180deg); -moz-filter: hue-rotate(180deg); filter: hue-rotate(180deg); } .icon-hue-4 { -webkit-filter: hue-rotate(270deg); -moz-filter: hue-rotate(270deg); filter: hue-rotate(270deg); } 
 <span class="icon icon-1 icon-hue-1"></span> <span class="icon icon-1 icon-hue-2"></span> <span class="icon icon-1 icon-hue-3"></span> <span class="icon icon-1 icon-hue-4"></span> 
+3
source

To preserve the intricacies of SVG icons, there is currently no alternative to including <svg> in your HTML page.

Chris Coyey 's article describes common good practices for inclusion. This is something like:

  • Save the SVG icon definitions as <symbols> :

     <svg id='my-icons'> <symbol id='icon-puppy'> <!-- puppy icon goes here --> </symbol> <symbol id='icon-kitty'> <!-- kitty icon goes here --> </symbol> </svg> 
  • When you need to use the icon, refer to the description of the icon:

     <svg class='icon'> <use xlink:href='#icon-puppy'/> </svg> 

This article describes how you can dynamically include an SVG file in HTML.

+3
source

I have worked with SVG before, but not with SVG icons. However, based on the knowledge that I have and what I am looking at, this is what I came up with (I might not fully understand your question in this case, please write more questions).

But how to transfer the SVG to a CSS file so that they can be reused on the same page

but that means I have to put SVG in HTML

SVG is basically XML, so it’s not entirely clear why you would rather not put SVG in an HTML file. If you put the SVG in an HTML file, you can simply use external or internal CSS for the SVG style. Here is an example to just do it.

In the HTML file:

 <circle cx="100" cy="100" r="75" /> 

In the CSS file: (Add the CSS file to the HTML file)

 <style> circle { fill: deepPink; transition: fill .3s ease-out; } circle:hover { fill: #009966; } </style> 

Here is the second option for many others, use the <img/> . Here is an article explaining how to use SVG from a separate file using the <img/> . And here is the code that accompanies it. Also note that it uses the jQuery svginject plugin .

Here are a few other options to save SVG from HTML and their merits.

Hope this helps.

+1
source

It seems you want to leave the xml contents of the svg file from html codes.

To satisfy this, two scripts are available for embedding svg files in html :

  • Using the element <object data="~/img/file.svg" type="image/svg+xml"></object> , So xml will be separated from html ==> , the above representation will be a combination of html and xml , which can be created using css (perhaps a scoped for example, using <style scoped="scoped"></style> on the landing page).
  • using the element <img src="~/img/file.svg"/> , So xml will be separated from html ==> the given representation will be only html and css cannot execute the contents of the xml svg file.

Another way to use svg is to use the built-in technique, say using the <svg> tag so that you can use the built-in stylesheets for this, But you want to separate xml from html here :)

For more information: Using SVG and SVG Styling with CSS .

+1
source

You can also dig in the evil-icons project. You will learn some neat ways to use svg.

Their best idea is to merge all your icons into a single sprite.svg file. Each of your svg badges must be enclosed with an <symbol> with an identifier. Then you get access to it whenever you need via xlink, as commented on in other answers.

By downloading the entire sprite.svg, you get boot time and accuracy.

+1
source

Source: https://habr.com/ru/post/1211124/


All Articles