If I will use more number of SVG Sprite icons in web application, it is very tough to use as below mentioned code for every icons. Is there any other easy ways to implement on web applications for more number of icons?
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="16.3px"
height="26.9px" viewBox="0 0 16.3 26.9" enable-background="new 0 0 16.3 26.9" xml:space="preserve">
<g id="bg">
</g>
<g id="ui">
<g>
<polygon points="8.1,0 10.3,4.3 15.2,5 11.7,8.3 12.5,13 8.1,10.8 3.8,13 4.6,8.3 1.1,5 6,4.3 "/>
<polygon fill="none" stroke="#000000" stroke-miterlimit="10" points="8.1,13 10.3,17.3 15.2,18 11.7,21.3 12.5,26 8.1,23.8
3.8,26 4.6,21.3 1.1,18 6,17.3 "/>
</g>
</g>
<g id="pop_ups">
</g>
</svg>
Is there any other different ways to use more number of icons on web applications?
2
Answers
One way of doing it is giving your star an
id
and reuse it with ause
element. In fact the first SVG, the one where you keep your code may be hidden.In this case you may give your
<use>
element anx
and/or any
attribute. This is allowing you to move the star where ever you need it.An other option id putting the code for your icons in a
<symbol>
and reuse it exactly like before with the bonus that a<symbol>
can have aviewBox
attribute and this is allowing you to have it in different sizes. For this you may give your<use>
element anwidth
and/or aheight
attribute.Please note that is better to leave the main path (
#star
in this case) without a fill and a stroke. This way you can give your<use>
element a stroke and a fill so you can get differently filled or stroked starsIf you’re not going to access the inner parts of your SVG with code (ie to change the color with a CSS class, or animate them with JavaScript), then you can just use an img tag and set the SVG as the source like you would with any normal image. You still get a lot of the benefits that SVGs offer like perfect scaling, etc. For example:
Should work. Good luck!