I try to integrate SVG icon on my web site, don't they are a thing i don't understand..
I have download 2 SVG icons :
Heart
<svg width="0" height="0" viewBox="0 0 32 32" style="position:absolute;margin-left: -100%;">
<path id ="home-icon" d="M57.062,31.398c0.932-1.025,0.842-2.596-0.201-3.508L33.884,7.785c-1.043-0.912-2.715-0.893-3.736,0.043L7.093,28.962
c-1.021,0.936-1.071,2.505-0.111,3.503l0.578,0.602c0.959,0.998,2.509,1.117,3.46,0.265l1.723-1.543v22.59
c0,1.386,1.123,2.508,2.508,2.508h8.987c1.385,0,2.508-1.122,2.508-2.508V38.575h11.463v15.804c-0.02,1.385,0.971,2.507,2.356,2.507
h9.524c1.385,0,2.508-1.122,2.508-2.508V32.107c0,0,0.476,0.417,1.063,0.933c0.586,0.515,1.817,0.102,2.749-0.924L57.062,31.398z"/>
</svg>
Project
<svg width="0" height="0" viewBox="0 0 64 64" style="position:absolute;margin-left: -100%;">
<g id="projects-icon">
<polygon points="22,6 22,10 28,16 22,22 22,26 32,16 "/>
<polygon points="10,10 10,6 0,16 10,26 10,22 4,16 "/>
<polygon points="18,12 10,20 14,20 22,12 "/>
</g>
</svg>
But the Heart icon is drawed for 32x32 and Project for 64x64, so when I try to use both on my menu, i must specify the image size in the viewBox item:
<nav id="top-menu">
<svg class="menu-icon" viewBox="0 0 32 32">
<use xlink:href="#heart-icon">
</svg>
<svg class="menu-icon" viewBox="0 0 64 64">
<use xlink:href="#project-icon">
</svg>
</nav>
Exemple on jsfiddle : http://jsfiddle.net/Nh57e/
In this case, i can't loop on my HTML and i must set size on HTML each time I want use an image.. (And if I want change the SVG, i need update all the html source for the new size :/ )
How can i do for use image without set the icon size ??
Thank all !
One solution would be to reference the whole SVGs rather than just parts of them.
In this version, we hide them in a hidden <div> rather than setting their sizes to zero:
<div style="display:none">
<svg id="project-icon" viewBox="0 0 32 32">
<g>
<polygon points="22,6 22,10 28,16 22,22 22,26 32,16 "/>
<polygon points="10,10 10,6 0,16 10,26 10,22 4,16 "/>
<polygon points="18,12 10,20 14,20 22,12 "/>
</g>
</svg>
</div>
I have taken out the width and height attributes here so that they default to 100%.
Then reference them from mini-SVGs that have the exact width and height you want:
<svg class="menu-icon" width="32px" height="32px">
<use xlink:href="#home-icon" />
</svg>
Demo here
Note. In your demo you had the viewBox sizes for the two SVGs back to front.
Another alternative is to use the symbol element. You can set a viewPort for each icon. And then reference them the same way you are currently.
<svg style="display: none;">
<symbol id="home-icon" viewBox="0 0 64 64">
<path d="M57.062,31.398c0.932-1.025,0.842-2.596-0.201-3.508L33.884,7.785c-1.043-0.912-2.715-0.893-3.736,0.043L7.093,28.962
c-1.021,0.936-1.071,2.505-0.111,3.503l0.578,0.602c0.959,0.998,2.509,1.117,3.46,0.265l1.723-1.543v22.59
c0,1.386,1.123,2.508,2.508,2.508h8.987c1.385,0,2.508-1.122,2.508-2.508V38.575h11.463v15.804c-0.02,1.385,0.971,2.507,2.356,2.507
h9.524c1.385,0,2.508-1.122,2.508-2.508V32.107c0,0,0.476,0.417,1.063,0.933c0.586,0.515,1.817,0.102,2.749-0.924L57.062,31.398z"/>
</symbol>
<symbol id="project-icon" viewBox="0 0 32 32">
<g>
<polygon points="22,6 22,10 28,16 22,22 22,26 32,16 "/>
<polygon points="10,10 10,6 0,16 10,26 10,22 4,16 "/>
<polygon points="18,12 10,20 14,20 22,12 "/>
</g>
</symbol>
</svg>
.menu-icon {
width: 32px;
height: 32px;
fill: #aaa;
}
<svg style="display: none;">
<symbol id="home-icon" viewBox="0 0 64 64">
<path d="M57.062,31.398c0.932-1.025,0.842-2.596-0.201-3.508L33.884,7.785c-1.043-0.912-2.715-0.893-3.736,0.043L7.093,28.962
c-1.021,0.936-1.071,2.505-0.111,3.503l0.578,0.602c0.959,0.998,2.509,1.117,3.46,0.265l1.723-1.543v22.59
c0,1.386,1.123,2.508,2.508,2.508h8.987c1.385,0,2.508-1.122,2.508-2.508V38.575h11.463v15.804c-0.02,1.385,0.971,2.507,2.356,2.507
h9.524c1.385,0,2.508-1.122,2.508-2.508V32.107c0,0,0.476,0.417,1.063,0.933c0.586,0.515,1.817,0.102,2.749-0.924L57.062,31.398z"/>
</symbol>
<symbol id="project-icon" viewBox="0 0 32 32">
<g>
<polygon points="22,6 22,10 28,16 22,22 22,26 32,16 "/>
<polygon points="10,10 10,6 0,16 10,26 10,22 4,16 "/>
<polygon points="18,12 10,20 14,20 22,12 "/>
</g>
</symbol>
</svg>
<nav id="top-menu">
home icon
<br/>
<svg class="menu-icon">
<use xlink:href="#home-icon" />
</svg>
<br/>
project icon
<br/>
<svg class="menu-icon">
<use xlink:href="#project-icon" />
</svg>
</nav>
Reference: css-tricks
Original demo on codepen (before I realized you can insert code snippets directly).
One way will be to edit the icons that will be listed together
so you will wrap it inside another group with consistent size
Related
Can I put svg resources used in the website behind the end of body, in order to keep
them outside of what will be rendered?
In short: Is it legal to do the following?
<!DOCTYPE html>
<html>
<head>...</head>
<body>
<svg xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 315.424 315.424" >
<use href="#arrow" fill="rgb(0,44,89)" />
</svg>
</body>
<svg>
<g id="arrow">
<path d="M311.929,222.266l-96.119-67.342c-1.413-0.99-2.783-1.513-4.307-1.513c-3.307,0-6.471,2.512-6.471,7.313v41.05H19.886
c-4.962,0-8.854,4.132-8.854,9.094v35.563c0,4.962,3.892,9.343,8.854,9.343h185.146v40.81c0,4.801,3.167,7.19,6.474,7.19
c0.001,0-0.089,0-0.089,0c1.524,0,3.032-0.461,4.445-1.451l96.09-67.306c2.214-1.55,3.473-3.864,3.473-6.375
S314.142,223.815,311.929,222.266z" />
</g>
</svg>
</html>
As mentioned before in the comments:
It's not valid and many browser won't display your elements.
So inserting your svg assets at the top or bottom of your <body> is the preferrable method.
Caution: Hiding your asset svg via display:none will break some referenced definitions like:
filters
gradients
masks and clip-paths
It works flawlessly for shape elements (like icons)
Example
function displayNone() {
document.querySelector('#svgAssets').style.display = 'none';
}
svg {
border: 1px dotted #ccc;
height: 10em;
display: inline-block;
}
<p><button onclick="displayNone()">Set display:none</button></p>
<svg viewBox="0 0 315.424 315.424">
<use href="#arrow" fill="red" />
</svg>
<svg viewBox="0 0 100 100">
<use href="#circle" fill="green" />
</svg>
<svg viewBox="0 0 200 100">
<use href="#ellipse" fill="url(#gradient)" />
</svg>
<svg id="svgAssets" style="visibility:visible; width:0; height:0" aria-hidden="true">
<defs>
<linearGradient id="gradient">
<stop stop-color="red" offset="0%"/>
<stop stop-color="orange" offset="100%"/>
</linearGradient>
</defs>
<symbol id="arrow" viewBox="0 0 315.424 315.424">
<path d="M311.929,222.266l-96.119-67.342c-1.413-0.99-2.783-1.513-4.307-1.513c-3.307,0-6.471,2.512-6.471,7.313v41.05H19.886 c-4.962,0-8.854,4.132-8.854,9.094v35.563c0,4.962,3.892,9.343,8.854,9.343h185.146v40.81c0,4.801,3.167,7.19,6.474,7.19 c0.001,0-0.089,0-0.089,0c1.524,0,3.032-0.461,4.445-1.451l96.09-67.306c2.214-1.55,3.473-3.864,3.473-6.375 S314.142,223.815,311.929,222.266z" />
</symbol>
<symbol id="circle" viewBox="0 0 100 100">
<circle cx="50%" cy="50%" r=50% />
</symbol>
<symbol id="ellipse" viewBox="0 0 200 100">
<ellipse cx="100" cy="50" rx="100" ry="50" />
</symbol>
</svg>
In the above example I'm using <symbol> elements which could be used as an alternative to <defs>. They also support different viewBox properties for each icon.
If you just need to place icons via <use> you could also use external file references like so:
<svg viewBox="0 0 315.424 315.424">
<use href="svgIcons.svg#arrow" fill="red" />
</svg>
However, your svg files need to be same domain (or send with appropriate CORS headers)
If the purpose is not to have the original #arrow rendered in the document, you might include it inside the svg in the body, wrapped around defs.
Demo in the snipped below.
<svg xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 315.424 315.424">
<defs>
<g id="arrow">
<path d="M311.929,222.266l-96.119-67.342c-1.413-0.99-2.783-1.513-4.307-1.513c-3.307,0-6.471,2.512-6.471,7.313v41.05H19.886
c-4.962,0-8.854,4.132-8.854,9.094v35.563c0,4.962,3.892,9.343,8.854,9.343h185.146v40.81c0,4.801,3.167,7.19,6.474,7.19
c0.001,0-0.089,0-0.089,0c1.524,0,3.032-0.461,4.445-1.451l96.09-67.306c2.214-1.55,3.473-3.864,3.473-6.375
S314.142,223.815,311.929,222.266z" />
</g>
</defs>
<use href="#arrow" fill="rgb(0,44,89)" />
</svg>
I am wondering is there a way have border (or stroke) all around except on the right side of the star (maybe using stroke-dasharray)?
SVG:
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" class="SvgDefinitions">
<defs>
<symbol id="HalfStar" viewBox="0 0 20 19">
<path d="M9,0.6L6.5,5.7L0.9,6.5c-0.2,0-0.4,0.1-0.6,0.3c-0.4,0.4-0.4,1,0,1.4l4.1,4l-1,5.6c0,0.2,0,0.4,0.1,0.6 c0.3,0.5,0.9,0.7,1.4,0.4l5.1-2.7l0,0V0C9.6,0,9.2,0.2,9,0.6z" />
</symbol>
</defs>
</svg>
<svg class="Star-Container">
<use href="#HalfStar" x-link:href="#HalfStar" />
</svg>
CSS:
#HalfStar {
stroke: red;
stroke-dasharray: 5,4;
}
CodePen: https://codepen.io/amir734jj/pen/zYqWZRN
The total length of the contour of half of the star measured with getTotalLength() is - 50px
The vertical bar length of the star is - 16px
For this segment, stroke-dasharray =" 0 16 " where 0 is the length of the stroke 16 is the length of the space
Therefore, the area of the star that should be filled with strokes is 34px
For 5 groups of strokes and spaces - 34/10 = 3,4px
As a result, the general formula will be:
stroke-dasharray="3.4,3.4 3.4,3.4 3.4,3.4 3.4,3.4 3.4,3.4 0, 16"
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" class="SvgDefinitions">
<defs>
<symbol id="HalfStar" viewBox="0 0 20 19">
<path stroke="red" stroke-dashoffset="0"
stroke-dasharray="3.4,3.4 3.4,3.4 3.4,3.4 3.4,3.4 3.4,3.4 0, 16"
d="M9,0.6L6.5,5.7L0.9,6.5c-0.2,0-0.4,0.1-0.6,0.3c-0.4,0.4-0.4,1,0,1.4l4.1,4l-1,5.6c0,0.2,0,0.4,0.1,0.6 c0.3,0.5,0.9,0.7,1.4,0.4l5.1-2.7l0,0V0C9.6,0,9.2,0.2,9,0.6z" />
</symbol>
</defs>
</svg>
<svg class="Star-Container">
<use href="#HalfStar" x-link:href="#HalfStar" />
</svg>
#NodeJS by comment
Is there a way for the border to be continuous for the rest and not
dashed?
To do this, swap 0, 16 by 16, 0 in the last group of parameters stroke-dasharray
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" class="SvgDefinitions">
<defs>
<symbol id="HalfStar" viewBox="0 0 20 19">
<path stroke="red" stroke-dashoffset="3"
stroke-dasharray="3.4,3.4 3.4,3.4 3.4,3.4 3.4,3.4 3.4,3.4 16,0"
d="M9,0.6L6.5,5.7L0.9,6.5c-0.2,0-0.4,0.1-0.6,0.3c-0.4,0.4-0.4,1,0,1.4l4.1,4l-1,5.6c0,0.2,0,0.4,0.1,0.6 c0.3,0.5,0.9,0.7,1.4,0.4l5.1-2.7l0,0V0C9.6,0,9.2,0.2,9,0.6z" />
</symbol>
</defs>
</svg>
<svg class="Star-Container">
<use href="#HalfStar" x-link:href="#HalfStar" />
</svg>
#Apha by comment
Your solution doesn't have a continuous border on the left. It's
dashed. I think the OP wanted continuous border on the left and no
border on the right
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" class="SvgDefinitions">
<defs>
<symbol id="HalfStar" viewBox="0 0 20 19">
<path stroke="red" stroke-dashoffset="0.75"
stroke-dasharray="33.5,0 0,16.5"
d="M9,0.6L6.5,5.7L0.9,6.5c-0.2,0-0.4,0.1-0.6,0.3c-0.4,0.4-0.4,1,0,1.4l4.1,4l-1,5.6c0,0.2,0,0.4,0.1,0.6 c0.3,0.5,0.9,0.7,1.4,0.4l5.1-2.7l0,0V0C9.6,0,9.2,0.2,9,0.6z" />
</symbol>
</defs>
</svg>
<svg class="Star-Container">
<use href="#HalfStar" x-link:href="#HalfStar" />
</svg>
I'm having trouble with a particular icon not scaling. If you go to this plunker you'll see the very first icon (the cup with the pencil, paint brush and ruler) doesn't respond to the space like every other svg element on the page.
I designed them all in Illustrator cc18 using save to generate the svg code.
For that particular icon Illustrator gives me this code.
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 22.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg version="1.1" id="illustration_x5F_icon" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
x="0px" y="0px" viewBox="0 0 79 146" style="enable-background:new 0 0 79 146;" xml:space="preserve">
<style type="text/css">
.st0{fill:none;}
</style>
<rect id="cup" x="0.6777539" y="67.352684" class="st0" width="77.6444931" height="77.647316"/>
<path id="ruler_x5F_shape" class="st0" d="M47.0672798,19.394165v45.8275757h1.1456909v-1.1456909h4.5957031v1.1456909h12.6415443
V19.394165H47.0672798z M48.2129707,22.8312378h4.5957031v1.1456909h-4.5957031V22.8312378z M48.2129707,27.4140015h4.5957031
v1.1456909h-4.5957031V27.4140015z M48.2129707,31.9967651h4.5957031v1.1456909h-4.5957031V31.9967651z M48.2129707,36.5795288
h4.5957031v1.1456909h-4.5957031V36.5795288z M48.2129707,41.1622925h4.5957031v1.1456909h-4.5957031V41.1622925z
M48.2129707,45.7450562h4.5957031v1.1456909h-4.5957031V45.7450562z M48.2129707,50.3278198h4.5957031v1.1456909h-4.5957031
V50.3278198z M48.2129707,54.9105835h4.5957031v1.1456909h-4.5957031V54.9105835z M48.2129707,59.4932861h4.5957031v1.1456909
h-4.5957031V59.4932861z M57.404438,62.9303589h-9.1914673V61.784668h9.1914673V62.9303589z M57.404438,58.3475952h-9.1914673
v-1.1456909h9.1914673V58.3475952z M57.404438,53.7648926h-9.1914673v-1.1456909h9.1914673V53.7648926z M57.404438,49.1821289
h-9.1914673V48.036438h9.1914673V49.1821289z M57.404438,44.5993652h-9.1914673v-1.1456909h9.1914673V44.5993652z
M57.404438,40.0166016h-9.1914673v-1.1456909h9.1914673V40.0166016z M57.404438,35.4338379h-9.1914673V34.288147h9.1914673
V35.4338379z M57.404438,30.8510742h-9.1914673v-1.1456909h9.1914673V30.8510742z M57.404438,26.2683105h-9.1914673v-1.1456909
h9.1914673V26.2683105z M57.404438,21.6855469h-9.1914673V20.539856h9.1914673V21.6855469z"/>
<g id="paint_x5F_brush">
<polygon id="paint_x5F_brush_x5F_shaft" class="st0" points="41.6965485,65.2217407 42.4845047,31.4855003 39.8335533,20.8818035
34.5317612,20.8818035 31.8808079,31.4855003 32.668766,65.2217407 "/>
<path id="paint_x5F_brush_x5F_tip" class="st0" d="M40.7088737,19.7361126
c0.7722778-1.1733189,1.775631-3.1726189,1.775631-5.6534939c0-5.1422243-7.0509987-7.2129793-5.3018494-13.0826187
c-0.7470016,0.8390205-5.3017902,7.9498148-5.3017902,13.0826187c0,2.8539448,0.819376,4.633482,1.5470791,5.6534939H40.7088737z"
/>
</g>
<g id="pencil">
<rect id="pencil_x5F_shaft" x="13.5497589" y="38.286869" class="st0" width="13.7482862" height="26.9348717"/>
<polygon id="pencil_x5F_tip" class="st0" points="22.6059189,25.2016068 18.2418861,25.2016068 13.7645836,37.1411209
27.0832214,37.1411209 "/>
<polygon id="pencil_x5F_lead" class="st0" points="20.4239025,19.3829746 18.456768,24.6287041 22.391037,24.6287041 "/>
</g>
</svg>
I then copy the code and place it in a <symbol> like this
<symbol viewbox="0 0 79 146" id="illustration_icon">
<rect id="cup" x="0.6777539" y="67.352684" class="st0" width="77.6444931" height="77.647316"/>
<path id="ruler_x5F_shape" class="st0" d="M47.0672798,19.394165v45.8275757h1.1456909v-1.1456909h4.5957031v1.1456909h12.6415443
V19.394165H47.0672798z M48.2129707,22.8312378h4.5957031v1.1456909h-4.5957031V22.8312378z M48.2129707,27.4140015h4.5957031
v1.1456909h-4.5957031V27.4140015z M48.2129707,31.9967651h4.5957031v1.1456909h-4.5957031V31.9967651z M48.2129707,36.5795288
h4.5957031v1.1456909h-4.5957031V36.5795288z M48.2129707,41.1622925h4.5957031v1.1456909h-4.5957031V41.1622925z
M48.2129707,45.7450562h4.5957031v1.1456909h-4.5957031V45.7450562z M48.2129707,50.3278198h4.5957031v1.1456909h-4.5957031
V50.3278198z M48.2129707,54.9105835h4.5957031v1.1456909h-4.5957031V54.9105835z M48.2129707,59.4932861h4.5957031v1.1456909
h-4.5957031V59.4932861z M57.404438,62.9303589h-9.1914673V61.784668h9.1914673V62.9303589z M57.404438,58.3475952h-9.1914673
v-1.1456909h9.1914673V58.3475952z M57.404438,53.7648926h-9.1914673v-1.1456909h9.1914673V53.7648926z M57.404438,49.1821289
h-9.1914673V48.036438h9.1914673V49.1821289z M57.404438,44.5993652h-9.1914673v-1.1456909h9.1914673V44.5993652z
M57.404438,40.0166016h-9.1914673v-1.1456909h9.1914673V40.0166016z M57.404438,35.4338379h-9.1914673V34.288147h9.1914673
V35.4338379z M57.404438,30.8510742h-9.1914673v-1.1456909h9.1914673V30.8510742z M57.404438,26.2683105h-9.1914673v-1.1456909
h9.1914673V26.2683105z M57.404438,21.6855469h-9.1914673V20.539856h9.1914673V21.6855469z"/>
<g id="paint_x5F_brush">
<polygon id="paint_x5F_brush_x5F_shaft" class="st0" points="41.6965485,65.2217407 42.4845047,31.4855003 39.8335533,20.8818035
34.5317612,20.8818035 31.8808079,31.4855003 32.668766,65.2217407 "/>
<path id="paint_x5F_brush_x5F_tip" class="st0" d="M40.7088737,19.7361126
c0.7722778-1.1733189,1.775631-3.1726189,1.775631-5.6534939c0-5.1422243-7.0509987-7.2129793-5.3018494-13.0826187
c-0.7470016,0.8390205-5.3017902,7.9498148-5.3017902,13.0826187c0,2.8539448,0.819376,4.633482,1.5470791,5.6534939H40.7088737z"
/>
</g>
<g id="pencil">
<rect id="pencil_x5F_shaft" x="13.5497589" y="38.286869" class="st0" width="13.7482862" height="26.9348717"/>
<polygon id="pencil_x5F_tip" class="st0" points="22.6059189,25.2016068 18.2418861,25.2016068 13.7645836,37.1411209
27.0832214,37.1411209 "/>
<polygon id="pencil_x5F_lead" class="st0" points="20.4239025,19.3829746 18.456768,24.6287041 22.391037,24.6287041 "/>
</g>
</symbol>
the way I'm using it in the HTML is the same for every icon which is like this
<div style="display:grid; grid-template-columns: 1fr 1fr 1fr 1fr; grid-auto-rows: 20vh;">
<div style="grid-area:1/1/2/2;">
<svg class="graphicA">
<use xlink:href="assets/symbol_sprite.svg#illustration_icon" />
</svg>
</div>
<div style="grid-area:1/2/2/3;">
<svg class="graphicA">
<use xlink:href="assets/symbol_sprite.svg#......" />
</svg>
</div>
<div style="grid-area:1/3/2/4;">
<svg class="graphicA">
<use xlink:href="assets/symbol_sprite.svg#......" />
</svg>
</div>
<!-- etc. etc. -->
</div>
You can refer to the plunker for the full code. Does anybody readily see what might be stopping this icon from scaling like the others?
There is a typo in the code, regarding viewBox.
<symbol viewbox="0 0 79 146" id="illustration_icon">
should be:
<symbol viewBox="0 0 79 146" id="illustration_icon">
Working version
Question
How can I overlay a transparent image pattern (PNG) over a SVG, while keeping the overlaid image pattern within the SVG's bounds, and keeping the SVG's fill visible?
Kind of like how in CSS you can define both background-color and backgroud-image.
Eg. background: red url(noise.png) top left repeat;
Image Example of what I am looking for
In this image, the balloon on the left is the SVG I currently have. What I'm trying to achieve is how the right balloon looks with noise applied to it.
Code Example:
Here's a code example of what I've tried so far:
FULL DEMO: http://staging.kassandrapoon.com/tests/svg/ (Sorry for the non-jsfiddle or code pen example. The background pattern wasn't loading cross domain)
SVG with my pattern attempt:
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px"
y="0px" width="143px" height="214px" viewBox="0 0 143 214" enable-background="new 0 0 143 214" xml:space="preserve">
<defs>
<!-- noise.png -->
<style type="text/css">
<![CDATA[
.filtered{
filter: url(#filter);
}
]]>
</style>
<filter id="filter" filterUnits="userSpaceOnUse">
<feImage xlink:href="noise.png" x="0" y="0" width="198" height="193" result="IMAGEFILL"/>
<feTile in="IMAGEFILL" result="TILEPATTERN"/>
<feComposite operator="in" in="TILEPATTERN" in2="SourceAlpha"/>
</filter>
</defs>
<g>
<g class="filtered">
<polygon fill-rule="evenodd" clip-rule="evenodd" fill="#DE3F18" points="90.8,214 53.2,214 47.3,175.9 98.1,175.9 "/>
<path fill-rule="evenodd" clip-rule="evenodd" fill="#DE3F18" d="M143,72.6c0-39.5-32-71.7-71.5-71.7S0,33.2,0,72.7
C0,79.1,0.9,86,2.4,91.6c0,0,1.3,4.4,2,6.2C4.6,98.2,5,98.6,5.1,99c0.5,1.3,1.2,2.7,1.8,4c0.3,0.6,0.6,1.1,0.9,1.6
c0.6,1.1,1.2,2.3,1.8,3.4c0.3,0.6,0.7,1.1,1,1.7c0.7,1.1,1.3,2.1,2,3.1c0.4,0.5,0.8,1.1,1.1,1.6c0.7,1,1.5,2,2.3,3
c0.4,0.5,0.8,1,1.2,1.4c0.8,1,1.7,1.9,2.6,2.9c0.4,0.4,0.8,0.8,1.2,1.3c1,1,2,1.9,3,2.8c0.4,0.3,0.8,0.7,1.2,1
c0.3,0.2,0.5,0.5,0.8,0.7L46.4,158h2.8h44.5h2.8l20.3-30.6c0.3-0.2,0.5-0.5,0.8-0.7c0.4-0.3,0.8-0.7,1.2-1c1-0.9,2-1.8,3-2.8
c0.4-0.4,0.8-0.8,1.2-1.3c0.9-0.9,1.8-1.9,2.6-2.9c0.4-0.5,0.8-1,1.2-1.4c0.8-1,1.6-2,2.3-3c0.4-0.5,0.8-1,1.1-1.6
c0.7-1,1.4-2.1,2-3.1c0.3-0.6,0.7-1.1,1-1.7c0.6-1.1,1.2-2.2,1.8-3.4c0.3-0.6,0.6-1.1,0.9-1.7c0.6-1.3,1.2-2.6,1.7-4
c0.2-0.4,0.4-0.8,0.5-1.3c0.7-1.8,2.3-7,2.3-7S143,79,143,72.6z"/>
<path fill-rule="evenodd" clip-rule="evenodd" fill="#F9F9EF" d="M126.1,68.4c0-37.4-24.4-68.8-55.2-68.8h0.3h-0.3
C40-0.3,14.7,31.1,14.7,68.4c0,0,0,0,0,0.1c0,0,0,0.1,0,0.1c0,6,1.2,12.5,2.2,17.2c0.2,0.8,0.9,2.6,1.3,4.2c0.4,1,0.9,2,1.3,3.2
c0.3,1,0.8,1.9,1.1,2.9c0.4,0.8,0.7,1.5,1.1,2.4c0.5,1.1,0.9,2.1,1.3,3l0.9,1.8c0.5,1,1,2,1.6,2.9l0.9,1.5c0,0.1,0.1,0.1,0.1,0.2
c0.6,0.9,1.1,1.8,1.6,2.6c0,0,0,0,0,0c0,0,0,0,0,0c0,0,0,0,0,0c0.3,0.5,0.6,0.9,1,1.4c0.7,0.9,29.4,46.8,29.4,46.8h25.7
c0,0,28.7-45.8,29.4-46.8c0.3-0.5,0.6-0.9,1-1.4c0,0,0,0,0,0c0,0,0,0,0,0c0,0,0,0,0,0c0.5-0.8,1.1-1.6,1.6-2.6
c0-0.1,0.1-0.1,0.1-0.2l0.9-1.5c0.5-0.9,1-1.9,1.6-2.9l0.9-1.8c0.4-0.8,0.9-1.9,1.3-3c0.3-0.8,0.7-1.6,1-2.4
c0.4-0.9,0.8-1.9,1.1-2.9c0.4-1.2,0.8-2.2,1.2-3.2c0.4-1.6,0.2-3.3,0.4-4.2c1.1-4.6,1.1-11.2,1.1-17.2
C126.1,68.6,126.1,68.5,126.1,68.4C126.1,68.5,126.1,68.4,126.1,68.4z"/>
<path fill-rule="evenodd" clip-rule="evenodd" fill="#DE3F18" d="M91.9,57.1c0,43.5-21.6,101-21.6,101s-21.6-57.5-21.6-101
S58.9,0.7,70.4,0.7C79.1,0.7,91.9,13.6,91.9,57.1z"/>
<polyline fill="none" stroke="#FC611F" stroke-width="1.8927" stroke-miterlimit="10" points="47.9,175.9 27.8,130 70.6,130
115.2,130.1 96.5,175.9 "/>
<line fill="none" stroke="#FC611F" stroke-width="1.8927" stroke-miterlimit="10" x1="73.2" y1="175.9" x2="72.3" y2="130"/>
<g>
<polygon fill="#DE3F18" points="46.4,158 49.3,158 93.7,158 96.6,158 115.3,130 27.7,130 "/>
</g>
</g>
<g>
<g>
<path fill="#F9F9EF" d="M102.6,188.6c0,5.5-2.3,7.4-5.1,7.4c-2.8,0-5.1-1.9-5.1-7.4s5.1-12.6,5.1-12.6S102.6,183,102.6,188.6z"/>
<path fill="#F9F9EF" d="M77.6,188.6c0,5.5-2.3,7.4-5.1,7.4c-2.8,0-5.1-1.9-5.1-7.4s5.1-12.6,5.1-12.6S77.6,183,77.6,188.6z"/>
<path fill="#F9F9EF" d="M52.6,188.6c0,5.5-2.3,7.4-5.1,7.4c-2.8,0-5.1-1.9-5.1-7.4s5.1-12.6,5.1-12.6S52.6,183,52.6,188.6z"/>
</g>
</g>
</g>
</svg>
I've tried defining a pattern in the SVG like here, but that replaces the existing fill color which I want to keep.
I have an svg file with 3 icons.
When I import it via the <img> tag, I get the 3 icons one below each other.
I want to use the icons in a row, one next to the other.
How can I use them separately?
The .svg file:
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 18.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" 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>
Thanks!
Use the SVG file as a sprite.
Create an icon-sized element, hiding the overflow:
.icon {
display: inline-block;
width: 16.3px;
height: 13.45px;
overflow: hidden;
}
Position the SVG within the element so the icon shows through:
.icon > img {
position: relative;
}
.darkStar > img {
top: 0;
}
.lightStar > img {
top: -13.45px;
}
Demo (using inline SVG instead of a linked <img>, which defeats the purpose, but is easier to demo here):
.icon {
display: inline-block;
width: 16.3px;
height: 13.45px;
overflow: hidden;
}
.icon > svg {
position: relative;
}
.darkStar > svg {
top: 0;
}
.lightStar > svg {
top: -13.45px;
}
<span class="icon lightStar">
<svg width="16.3px" height="26.9px">
<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 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" fill="none" stroke="#000000" stroke-miterlimit="10" />
</svg>
</span>
<span class="icon darkStar">
<svg width="16.3px" height="26.9px">
<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 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" fill="none" stroke="#000000" stroke-miterlimit="10" />
</svg>
</span>
You can use fragment identifiers to display only part of the SVG file in any particular img element.
The advantage of this approach is that the "individual sprites" in your SVG file are defined in your SVG file, so when using it elsewhere you don't need to know anything of the internal structure, you can just ask for what you want by name:
<button>
<img src="x.svg#star1" alt="*">
</button>
The most cross-platform-compatible solution is add some SVG views which define which part of the image to show for which ID fragment. The view syntax is similar to the global viewBox attribute of the root SVG element*:
<view id="star1" viewBox="0 0 10 10"/>
Here's a good blog post (with a live example) which explains the technique in great detail.
*note that I haven't calculated that viewBox value for your SVG, you'll have to figure it out yourself.
I'm not sure if you mean vertically or horizontally, but here's something I found from Codepen.io which has a lot of SVG samples you might want to go through.
http://codepen.io/jonnowitts/pen/waONVV
Here he has SVG's lined up vertically in a row.
<button type="button" id="positive">
<div class="content">
<svg xmlns="http://www.w3.org/2000/svg" width="25" height="42" viewBox="-9 0 38 40" preserveAspectRatio="xMidYMid meet">
<path class="check" fill="none" d="M0 20 L8 28 L25 10" stroke="white" stroke-width="3"/>
</svg>
<span>Positive</span>
</div>
</button>
<button id="negative">
<div class="content">
<svg xmlns="http://www.w3.org/2000/svg" width="25" height="42" viewBox="-9 0 38 40" preserveAspectRatio="xMidYMid meet">
<path class="cross-1" fill="none" d="M0 10 L20 30" stroke="white" stroke-width="3"/>
<path class="cross-2" fill="none" d="M20 10 L0 30" stroke="white" stroke-width="3"/>
</svg>
<span>Negative</span>
</div>
</button>
<button id="warning">
<div class="content">
<svg xmlns="http://www.w3.org/2000/svg" width="30" height="42" viewBox="-3 0 38 40" preserveAspectRatio="xMidYMid meet">
<polygon class="triangle"
stroke="white"
stroke-width="2"
fill="none"
points="15,4 0,34 30,34"
/>
<path class="exclaim-1" d="M15 14 L15 22 Z" stroke-width="4" stroke="white" fill="none" />
<path class="exclaim-2" d="M15 24 L15 29 Z" stroke-width="4" stroke="white" fill="none" />
</svg>
<span>Warning</span>
</div>
</button>