wavy button shape with text inside - html

I'm trying to create this button:
I'm doing this with SVG, so here is my code:
.svg-button {
position:relative;
}
a.test {
display:block;
}
<div class="svg-button">
<?xml version="1.0" encoding="UTF-8"?>
<svg width="150px" height="51px" viewBox="0 0 150 51" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g id="Desktop-HD" transform="translate(-987.000000, -779.000000)" stroke="#102CCA">
<g id="Group-6" transform="translate(836.000000, 668.000000)">
<g id="Group" transform="translate(151.000000, 111.000000)">
<path d="M0.5,0.5 L0.5,47.4475413 C38.7946588,43.6370428 73.268313,43.6553687 103.92161,47.5038947 C134.273898,51.314629 149.5,49.8747765 149.5,43.6367187 L149.5,30.078125 C149.5,19.2695184 149.5,19.0898429 149.5,0.936176488 L0.5,0.5 Z" id="Rectangle"></path>
</g>
</g>
</g>
</g>
</svg>
<a class="test" href="#">add to cart</a>
</div>
Any ideas on how I might integrate the text inside the shape and add a hover state for the shape/button?
Thanks!

There are a number of ways you could do it.
You could move the text into the SVG using a <text> element...
a.test:hover svg path {
fill: red;
}
<div class="svg-button">
<a class="test" href="#">
<svg width="150px" height="51px" viewBox="0 0 150 51" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g id="Desktop-HD" transform="translate(-987.000000, -779.000000)" stroke="#102CCA">
<g id="Group-6" transform="translate(836.000000, 668.000000)">
<g id="Group" transform="translate(151.000000, 111.000000)">
<path d="M0.5,0.5 L0.5,47.4475413 C38.7946588,43.6370428 73.268313,43.6553687 103.92161,47.5038947 C134.273898,51.314629 149.5,49.8747765 149.5,43.6367187 L149.5,30.078125 C149.5,19.2695184 149.5,19.0898429 149.5,0.936176488 L0.5,0.5 Z" id="Rectangle"></path>
</g>
</g>
</g>
</g>
<text x="75" y="27" text-anchor="middle">add to cart</text>
</svg>
</a>
</div>
Or if you wanted to keep the text as HTML. You could use relative/absolute positioning to centre the text over the SVG.
.svg-button {
position: relative;
}
.svg-button svg,
.svg-button a {
position: absolute;
display: block;
width: 150px;
height: 51px;
top: 0;
}
.svg-button a {
text-align: center;
line-height: 45px;
}
.svg-button:hover svg path {
fill: red;
}
<div class="svg-button">
<svg width="150px" height="51px" viewBox="0 0 150 51" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g id="Desktop-HD" transform="translate(-987.000000, -779.000000)" stroke="#102CCA">
<g id="Group-6" transform="translate(836.000000, 668.000000)">
<g id="Group" transform="translate(151.000000, 111.000000)">
<path d="M0.5,0.5 L0.5,47.4475413 C38.7946588,43.6370428 73.268313,43.6553687 103.92161,47.5038947 C134.273898,51.314629 149.5,49.8747765 149.5,43.6367187 L149.5,30.078125 C149.5,19.2695184 149.5,19.0898429 149.5,0.936176488 L0.5,0.5 Z" id="Rectangle"></path>
</g>
</g>
</g>
</g>
</svg>
<a class="test" href="#">add to cart</a>
</div>

I'd recommend grouping the svg path and the text 'add to cart' inside of the svg (with a "g" element). I'm also guessing that you want this whole button to act as the link, so I'd recommend structuring the HTML like the following:
<a>
<svg>
<g>
<path></path>
<text>Add to cart</text>
</g>
</svg>
</a>
...and positioning the path and the text inside of the group with x and y values. You can then target the path and the text, when the whole link is being hovered on like this:
a:hover svg g text {
fill: red;
}
a:hover svg g path {
stroke: red;
}
Check out this simple codepen I made for a demo and let me know if you have any questions! https://codepen.io/segheysens/pen/VzbzeJ

Related

Problems with controlling SVG colors from outside through CSS

I got an SVG from a designer of which I'd like to set the color through CSS from outside of the SVG. I managed to overwrite part of the colors, but not all. What would I need to do to have the icon unfilled, with red lines over the grey background? Ideally without modifying the SVG because there are plenty of them with similar structure.
html {
background-color: #eeeeee;
}
.icon{
height: 6rem;
width: 6rem;
}
svg {
fill:red;
}
<html>
<div class="icon">
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 24.1.3, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg version="1.1" baseProfile="basic" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px"
y="0px" viewBox="0 0 76.5 76.5" xml:space="preserve">
<style type="text/css">
.st0{display:none;}
.st1{fill:none;stroke:#000000;stroke-width:2;}
.st2{fill:#FFFFFF;}
.st3{fill:none;stroke:#FFFFFF;stroke-width:5;}
.st4{fill:none;stroke:#000000;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;}
.st5{display:inline;}
.st6{font-family:'Akkurat-Bold';}
.st7{font-size:7.0024px;}
.st8{display:inline;fill:#2B3427;}
.st9{fill:none;stroke:#2B3427;stroke-width:2;}
.st10{fill:none;stroke:#2B3427;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;}
.st11{display:inline;fill:#FFFFFF;}
.st12{fill:#2B3427;}
</style>
<g id="Vorlagen" class="st0">
</g>
<g id="Kreis_Black">
<path d="M38.3,4.8c18.4,0,33.4,15,33.4,33.4s-15,33.4-33.4,33.4S4.8,56.7,4.8,38.3S19.8,4.8,38.3,4.8 M38.3,2.8
C18.7,2.8,2.8,18.7,2.8,38.3s15.9,35.4,35.4,35.4s35.4-15.9,35.4-35.4S57.8,2.8,38.3,2.8L38.3,2.8z"/>
<path d="M38.3,4.8c18.4,0,33.4,15,33.4,33.4s-15,33.4-33.4,33.4S4.8,56.7,4.8,38.3S19.8,4.8,38.3,4.8 M38.3,2.8
C18.7,2.8,2.8,18.7,2.8,38.3s15.9,35.4,35.4,35.4s35.4-15.9,35.4-35.4S57.8,2.8,38.3,2.8L38.3,2.8z"/>
</g>
<g id="Illu">
<g>
<path class="st1" d="M35.2,43.3c-1.5,1-9.1,3.6-14.7-1.1c-4-3.3-4.4-8.7-3.6-24.9c9.5-0.9,17.4,0.9,22,5c4.1,3.6,3.9,8.6,4.3,11.2
C43.6,36.5,39.5,40.4,35.2,43.3z"/>
<path class="st2" d="M44.8,41.6c9.7,6,18.3-7.2,20.2-15.9c-5.5-3.6-13-2.3-17.7,0.6C43.8,28.5,39.2,35,44.8,41.6"/>
<path class="st3" d="M44.8,41.6c9.7,6,18.3-7.2,20.2-15.9c-5.5-3.6-13-2.3-17.7,0.6C43.8,28.5,39.2,35,44.8,41.6z"/>
<path class="st2" d="M44.8,41.6C57.2,51.9,63,36.7,64.9,25.7c-8.2-3.9-13-2.3-17.7,0.6C43.8,28.5,39.2,35,44.8,41.6"/>
<path class="st1" d="M44.8,41.6C57.2,51.9,63,36.7,64.9,25.7c-8.2-3.9-13-2.3-17.7,0.6C43.8,28.5,39.2,35,44.8,41.6z"/>
<path class="st4" d="M22.7,22.6c0,0,14.8,6,11.3,43.2c0,0,12.4-31.6,24-36.1"/>
</g>
<rect x="21.1" y="47.3" class="st2" width="32.6" height="8.1"/>
</g>
<g id="Typo" class="st0">
<text transform="matrix(1 0 0 1 25.4238 53.8332)" class="st5 st6 st7">VEGAN</text>
</g>
<g id="Typo_Gepfadet">
<g>
<path d="M28.1,53.8h-1l-1.6-5h1l1.2,3.8l1.2-3.8h0.9L28.1,53.8z"/>
<path d="M30.8,53.8v-5h3.5v0.8h-2.6v1.2H34v0.8h-2.3V53h2.6v0.8H30.8z"/>
<path d="M38.9,53.8l-0.1-0.5c-0.1,0.3-0.6,0.6-1.2,0.6c-0.6,0-1.1-0.1-1.5-0.6c-0.5-0.5-0.6-1.2-0.6-2s0.1-1.5,0.6-2
c0.4-0.4,0.9-0.6,1.5-0.6c0.6,0,1.1,0.2,1.5,0.6c0.2,0.3,0.4,0.6,0.5,1h-0.9c0-0.2-0.1-0.3-0.2-0.5c-0.2-0.2-0.4-0.3-0.8-0.3
c-0.3,0-0.6,0.1-0.8,0.3c-0.3,0.4-0.4,1-0.4,1.4s0,1.1,0.4,1.5c0.2,0.2,0.5,0.3,0.8,0.3c0.3,0,0.6-0.1,0.7-0.3
c0.3-0.3,0.3-0.6,0.3-1h-1.2v-0.8h2.1v2.7H38.9z"/>
<path d="M44,53.8l-0.3-1h-2l-0.3,1h-0.9l1.7-5h1l1.7,5H44z M42.7,50L42,52.1h1.4L42.7,50z"/>
<path d="M49.1,53.8l-2.2-3.4v3.4H46v-5h1l2.1,3.4v-3.4H50v5H49.1z"/>
</g>
</g>
<g id="Ikons_x5F_Grün" class="st0">
<path class="st8" d="M38.3,4.8c18.4,0,33.4,15,33.4,33.4s-15,33.4-33.4,33.4S4.8,56.7,4.8,38.3S19.8,4.8,38.3,4.8 M38.3,2.8
C18.7,2.8,2.8,18.7,2.8,38.3s15.9,35.4,35.4,35.4s35.4-15.9,35.4-35.4S57.8,2.8,38.3,2.8L38.3,2.8z"/>
<path class="st8" d="M38.3,4.8c18.4,0,33.4,15,33.4,33.4s-15,33.4-33.4,33.4S4.8,56.7,4.8,38.3S19.8,4.8,38.3,4.8 M38.3,2.8
C18.7,2.8,2.8,18.7,2.8,38.3s15.9,35.4,35.4,35.4s35.4-15.9,35.4-35.4S57.8,2.8,38.3,2.8L38.3,2.8z"/>
<g class="st5">
<path class="st9" d="M35.2,43.3c-1.5,1-9.1,3.6-14.7-1.1c-4-3.3-4.4-8.7-3.6-24.9c9.5-0.9,17.4,0.9,22,5c4.1,3.6,3.9,8.6,4.3,11.2
C43.6,36.5,39.5,40.4,35.2,43.3z"/>
<path class="st2" d="M44.8,41.6c9.7,6,18.3-7.2,20.2-15.9c-5.5-3.6-13-2.3-17.7,0.6C43.8,28.5,39.2,35,44.8,41.6"/>
<path class="st3" d="M44.8,41.6c9.7,6,18.3-7.2,20.2-15.9c-5.5-3.6-13-2.3-17.7,0.6C43.8,28.5,39.2,35,44.8,41.6z"/>
<path class="st2" d="M44.8,41.6C57.2,51.9,63,36.7,64.9,25.7c-8.2-3.9-13-2.3-17.7,0.6C43.8,28.5,39.2,35,44.8,41.6"/>
<path class="st9" d="M44.8,41.6C57.2,51.9,63,36.7,64.9,25.7c-8.2-3.9-13-2.3-17.7,0.6C43.8,28.5,39.2,35,44.8,41.6z"/>
<path class="st10" d="M22.7,22.6c0,0,14.8,6,11.3,43.2c0,0,12.4-31.6,24-36.1"/>
</g>
<rect x="21.1" y="47.3" class="st11" width="32.6" height="8.1"/>
<g class="st5">
<path class="st12" d="M28.1,53.8h-1l-1.6-5h1l1.2,3.8l1.2-3.8h0.9L28.1,53.8z"/>
<path class="st12" d="M30.8,53.8v-5h3.5v0.8h-2.6v1.2H34v0.8h-2.3V53h2.6v0.8H30.8z"/>
<path class="st12" d="M38.9,53.8l-0.1-0.5c-0.1,0.3-0.6,0.6-1.2,0.6c-0.6,0-1.1-0.1-1.5-0.6c-0.5-0.5-0.6-1.2-0.6-2s0.1-1.5,0.6-2
c0.4-0.4,0.9-0.6,1.5-0.6c0.6,0,1.1,0.2,1.5,0.6c0.2,0.3,0.4,0.6,0.5,1h-0.9c0-0.2-0.1-0.3-0.2-0.5c-0.2-0.2-0.4-0.3-0.8-0.3
c-0.3,0-0.6,0.1-0.8,0.3c-0.3,0.4-0.4,1-0.4,1.4s0,1.1,0.4,1.5c0.2,0.2,0.5,0.3,0.8,0.3c0.3,0,0.6-0.1,0.7-0.3
c0.3-0.3,0.3-0.6,0.3-1h-1.2v-0.8h2.1v2.7H38.9z"/>
<path class="st12" d="M44,53.8l-0.3-1h-2l-0.3,1h-0.9l1.7-5h1l1.7,5H44z M42.7,50L42,52.1h1.4L42.7,50z"/>
<path class="st12" d="M49.1,53.8l-2.2-3.4v3.4H46v-5h1l2.1,3.4v-3.4H50v5H49.1z"/>
</g>
</g>
</svg>
</div>
</html>
why not simply remove the style part inside the SVG ?
(and create your own css [in the html HEAD] with the colors you want)
other just use css !impotant in your css
html { background-color: #eeeeee; }
.icon{
height: 6rem;
width: 6rem;
}
svg { fill:red; }
/* add: */
.st1 { stroke:blue !important; }
path { stroke:orange !important; }
SVG is a regular HTML element. Therefore it takes background-color: white or transparent instead of fill to stylize its background.

How to change the background color and text color of SVG using css?

I have SVG and what I am doing is I have to change the color of the SVG using css. I tried below code but it's only changing the background color I also want to change the text color.
This is my actual SVG.
My expected output.
I am getting the output.
Is it possible?
.mylogo2 {
fill: yellow;
color: red
}
<div class="mylogo2">
<svg xmlns="http://www.w3.org/2000/svg" width="187.684" height="58.885" viewBox="0 0 187.684 58.885">
<g id="Sample_Logo" data-name="Sample Logo" transform="translate(-475 -511)">
<path id="Path_1" data-name="Path 1" d="M0,19.038,187.684,0V39.848L0,58.885Z" transform="translate(475 511)"/>
<path id="Path_2" data-name="Path 2" d="M6.888-8.456-.56-19.824H4.872l4.2,7.42,4.452-7.42H18.7L11.256-8.456V0H6.888ZM19.1-9.912a11.2,11.2,0,0,1,.8-4.312,9.457,9.457,0,0,1,2.226-3.29A9.807,9.807,0,0,1,25.508-19.6a12.213,12.213,0,0,1,4.284-.728,12.213,12.213,0,0,1,4.284.728,9.807,9.807,0,0,1,3.388,2.086,9.457,9.457,0,0,1,2.226,3.29,11.2,11.2,0,0,1,.8,4.312,11.2,11.2,0,0,1-.8,4.312,9.457,9.457,0,0,1-2.226,3.29A9.807,9.807,0,0,1,34.076-.224,12.213,12.213,0,0,1,29.792.5a12.213,12.213,0,0,1-4.284-.728A9.807,9.807,0,0,1,22.12-2.31,9.457,9.457,0,0,1,19.894-5.6,11.2,11.2,0,0,1,19.1-9.912Zm4.536,0a7.271,7.271,0,0,0,.434,2.562,5.868,5.868,0,0,0,1.246,2.016A5.687,5.687,0,0,0,27.258-4a6.488,6.488,0,0,0,2.534.476A6.488,6.488,0,0,0,32.326-4a5.687,5.687,0,0,0,1.946-1.33A5.868,5.868,0,0,0,35.518-7.35a7.271,7.271,0,0,0,.434-2.562,7.3,7.3,0,0,0-.434-2.548,5.842,5.842,0,0,0-1.246-2.03,5.687,5.687,0,0,0-1.946-1.33,6.488,6.488,0,0,0-2.534-.476,6.488,6.488,0,0,0-2.534.476,5.687,5.687,0,0,0-1.946,1.33,5.842,5.842,0,0,0-1.246,2.03A7.3,7.3,0,0,0,23.632-9.912Zm36.54,2.268A9.561,9.561,0,0,1,59.64-4.4,7.147,7.147,0,0,1,58.058-1.82,7.247,7.247,0,0,1,55.44-.112,9.83,9.83,0,0,1,51.828.5a9.914,9.914,0,0,1-3.64-.616A7.247,7.247,0,0,1,45.57-1.82,7.147,7.147,0,0,1,43.988-4.4a9.561,9.561,0,0,1-.532-3.248v-12.18h4.368V-7.812A4.827,4.827,0,0,0,48.118-6.1a4.081,4.081,0,0,0,.826,1.358,3.751,3.751,0,0,0,1.274.9,4,4,0,0,0,1.61.322,3.9,3.9,0,0,0,1.6-.322,3.787,3.787,0,0,0,1.26-.9A4.081,4.081,0,0,0,55.51-6.1,4.827,4.827,0,0,0,55.8-7.812V-19.824h4.368ZM64.2-19.824h7.672a13.537,13.537,0,0,1,2.87.294,6.809,6.809,0,0,1,2.38.98A4.883,4.883,0,0,1,78.75-16.7a6.258,6.258,0,0,1,.6,2.9,5.818,5.818,0,0,1-1.092,3.57,4.916,4.916,0,0,1-3.192,1.862L80.108,0H74.872L70.728-7.924H68.572V0H64.2Zm4.368,8.2h2.576q.588,0,1.246-.042a3.9,3.9,0,0,0,1.19-.252,2.058,2.058,0,0,0,.882-.658,2,2,0,0,0,.35-1.26,2.092,2.092,0,0,0-.308-1.2,2.053,2.053,0,0,0-.784-.686,3.478,3.478,0,0,0-1.092-.322,8.908,8.908,0,0,0-1.2-.084H68.572Zm22.232-8.2h4.368V-4.032h8.092V0H90.8Zm13.8,9.912a11.2,11.2,0,0,1,.8-4.312,9.457,9.457,0,0,1,2.226-3.29A9.807,9.807,0,0,1,111.02-19.6a12.213,12.213,0,0,1,4.284-.728,12.213,12.213,0,0,1,4.284.728,9.807,9.807,0,0,1,3.388,2.086,9.457,9.457,0,0,1,2.226,3.29,11.2,11.2,0,0,1,.8,4.312,11.2,11.2,0,0,1-.8,4.312,9.457,9.457,0,0,1-2.226,3.29,9.807,9.807,0,0,1-3.388,2.086A12.213,12.213,0,0,1,115.3.5a12.213,12.213,0,0,1-4.284-.728,9.807,9.807,0,0,1-3.388-2.086,9.457,9.457,0,0,1-2.226-3.29A11.2,11.2,0,0,1,104.608-9.912Zm4.536,0a7.271,7.271,0,0,0,.434,2.562,5.868,5.868,0,0,0,1.246,2.016A5.687,5.687,0,0,0,112.77-4a6.488,6.488,0,0,0,2.534.476A6.488,6.488,0,0,0,117.838-4a5.687,5.687,0,0,0,1.946-1.33A5.868,5.868,0,0,0,121.03-7.35a7.271,7.271,0,0,0,.434-2.562,7.3,7.3,0,0,0-.434-2.548,5.842,5.842,0,0,0-1.246-2.03,5.687,5.687,0,0,0-1.946-1.33A6.488,6.488,0,0,0,115.3-16.3a6.488,6.488,0,0,0-2.534.476,5.687,5.687,0,0,0-1.946,1.33,5.842,5.842,0,0,0-1.246,2.03A7.3,7.3,0,0,0,109.144-9.912ZM146.916-1.4A15.812,15.812,0,0,1,142.884.056,20.476,20.476,0,0,1,138.6.5a12.213,12.213,0,0,1-4.284-.728,9.807,9.807,0,0,1-3.388-2.086A9.457,9.457,0,0,1,128.7-5.6a11.2,11.2,0,0,1-.8-4.312,11.2,11.2,0,0,1,.8-4.312,9.457,9.457,0,0,1,2.226-3.29,9.806,9.806,0,0,1,3.388-2.086,12.213,12.213,0,0,1,4.284-.728,15.855,15.855,0,0,1,4.382.574A8.58,8.58,0,0,1,146.5-17.78l-3.08,3.36a6.137,6.137,0,0,0-2.016-1.4,7.184,7.184,0,0,0-2.8-.476,6.487,6.487,0,0,0-2.534.476,5.687,5.687,0,0,0-1.946,1.33,5.842,5.842,0,0,0-1.246,2.03,7.3,7.3,0,0,0-.434,2.548,7.271,7.271,0,0,0,.434,2.562,5.868,5.868,0,0,0,1.246,2.016A5.687,5.687,0,0,0,136.066-4a6.488,6.488,0,0,0,2.534.476,8.349,8.349,0,0,0,2.464-.322,9.3,9.3,0,0,0,1.652-.658v-3.22H139.16V-11.76h7.756Zm2.772-8.512a11.2,11.2,0,0,1,.8-4.312,9.457,9.457,0,0,1,2.226-3.29A9.807,9.807,0,0,1,156.1-19.6a12.213,12.213,0,0,1,4.284-.728,12.213,12.213,0,0,1,4.284.728,9.807,9.807,0,0,1,3.388,2.086,9.457,9.457,0,0,1,2.226,3.29,11.2,11.2,0,0,1,.8,4.312,11.2,11.2,0,0,1-.8,4.312,9.457,9.457,0,0,1-2.226,3.29,9.807,9.807,0,0,1-3.388,2.086A12.213,12.213,0,0,1,160.384.5,12.213,12.213,0,0,1,156.1-.224a9.807,9.807,0,0,1-3.388-2.086,9.457,9.457,0,0,1-2.226-3.29A11.2,11.2,0,0,1,149.688-9.912Zm4.536,0a7.271,7.271,0,0,0,.434,2.562A5.868,5.868,0,0,0,155.9-5.334,5.687,5.687,0,0,0,157.85-4a6.488,6.488,0,0,0,2.534.476A6.488,6.488,0,0,0,162.918-4a5.687,5.687,0,0,0,1.946-1.33A5.868,5.868,0,0,0,166.11-7.35a7.271,7.271,0,0,0,.434-2.562,7.3,7.3,0,0,0-.434-2.548,5.842,5.842,0,0,0-1.246-2.03,5.687,5.687,0,0,0-1.946-1.33,6.488,6.488,0,0,0-2.534-.476,6.487,6.487,0,0,0-2.534.476,5.687,5.687,0,0,0-1.946,1.33,5.842,5.842,0,0,0-1.246,2.03A7.3,7.3,0,0,0,154.224-9.912Z" transform="matrix(0.995, -0.105, 0.105, 0.995, 484.921, 558.826)" fill="#fff"/>
</g>
</svg>
</div>
You need to change the fill of the second path:
.mylogo2 {
fill: yellow;
}
#Path_2 {
fill: red;
}
<div class="mylogo2">
<svg xmlns="http://www.w3.org/2000/svg" width="187.684" height="58.885" viewBox="0 0 187.684 58.885">
<g id="Sample_Logo" data-name="Sample Logo" transform="translate(-475 -511)">
<path id="Path_1" data-name="Path 1" d="M0,19.038,187.684,0V39.848L0,58.885Z" transform="translate(475 511)"/>
<path id="Path_2" data-name="Path 2" d="M6.888-8.456-.56-19.824H4.872l4.2,7.42,4.452-7.42H18.7L11.256-8.456V0H6.888ZM19.1-9.912a11.2,11.2,0,0,1,.8-4.312,9.457,9.457,0,0,1,2.226-3.29A9.807,9.807,0,0,1,25.508-19.6a12.213,12.213,0,0,1,4.284-.728,12.213,12.213,0,0,1,4.284.728,9.807,9.807,0,0,1,3.388,2.086,9.457,9.457,0,0,1,2.226,3.29,11.2,11.2,0,0,1,.8,4.312,11.2,11.2,0,0,1-.8,4.312,9.457,9.457,0,0,1-2.226,3.29A9.807,9.807,0,0,1,34.076-.224,12.213,12.213,0,0,1,29.792.5a12.213,12.213,0,0,1-4.284-.728A9.807,9.807,0,0,1,22.12-2.31,9.457,9.457,0,0,1,19.894-5.6,11.2,11.2,0,0,1,19.1-9.912Zm4.536,0a7.271,7.271,0,0,0,.434,2.562,5.868,5.868,0,0,0,1.246,2.016A5.687,5.687,0,0,0,27.258-4a6.488,6.488,0,0,0,2.534.476A6.488,6.488,0,0,0,32.326-4a5.687,5.687,0,0,0,1.946-1.33A5.868,5.868,0,0,0,35.518-7.35a7.271,7.271,0,0,0,.434-2.562,7.3,7.3,0,0,0-.434-2.548,5.842,5.842,0,0,0-1.246-2.03,5.687,5.687,0,0,0-1.946-1.33,6.488,6.488,0,0,0-2.534-.476,6.488,6.488,0,0,0-2.534.476,5.687,5.687,0,0,0-1.946,1.33,5.842,5.842,0,0,0-1.246,2.03A7.3,7.3,0,0,0,23.632-9.912Zm36.54,2.268A9.561,9.561,0,0,1,59.64-4.4,7.147,7.147,0,0,1,58.058-1.82,7.247,7.247,0,0,1,55.44-.112,9.83,9.83,0,0,1,51.828.5a9.914,9.914,0,0,1-3.64-.616A7.247,7.247,0,0,1,45.57-1.82,7.147,7.147,0,0,1,43.988-4.4a9.561,9.561,0,0,1-.532-3.248v-12.18h4.368V-7.812A4.827,4.827,0,0,0,48.118-6.1a4.081,4.081,0,0,0,.826,1.358,3.751,3.751,0,0,0,1.274.9,4,4,0,0,0,1.61.322,3.9,3.9,0,0,0,1.6-.322,3.787,3.787,0,0,0,1.26-.9A4.081,4.081,0,0,0,55.51-6.1,4.827,4.827,0,0,0,55.8-7.812V-19.824h4.368ZM64.2-19.824h7.672a13.537,13.537,0,0,1,2.87.294,6.809,6.809,0,0,1,2.38.98A4.883,4.883,0,0,1,78.75-16.7a6.258,6.258,0,0,1,.6,2.9,5.818,5.818,0,0,1-1.092,3.57,4.916,4.916,0,0,1-3.192,1.862L80.108,0H74.872L70.728-7.924H68.572V0H64.2Zm4.368,8.2h2.576q.588,0,1.246-.042a3.9,3.9,0,0,0,1.19-.252,2.058,2.058,0,0,0,.882-.658,2,2,0,0,0,.35-1.26,2.092,2.092,0,0,0-.308-1.2,2.053,2.053,0,0,0-.784-.686,3.478,3.478,0,0,0-1.092-.322,8.908,8.908,0,0,0-1.2-.084H68.572Zm22.232-8.2h4.368V-4.032h8.092V0H90.8Zm13.8,9.912a11.2,11.2,0,0,1,.8-4.312,9.457,9.457,0,0,1,2.226-3.29A9.807,9.807,0,0,1,111.02-19.6a12.213,12.213,0,0,1,4.284-.728,12.213,12.213,0,0,1,4.284.728,9.807,9.807,0,0,1,3.388,2.086,9.457,9.457,0,0,1,2.226,3.29,11.2,11.2,0,0,1,.8,4.312,11.2,11.2,0,0,1-.8,4.312,9.457,9.457,0,0,1-2.226,3.29,9.807,9.807,0,0,1-3.388,2.086A12.213,12.213,0,0,1,115.3.5a12.213,12.213,0,0,1-4.284-.728,9.807,9.807,0,0,1-3.388-2.086,9.457,9.457,0,0,1-2.226-3.29A11.2,11.2,0,0,1,104.608-9.912Zm4.536,0a7.271,7.271,0,0,0,.434,2.562,5.868,5.868,0,0,0,1.246,2.016A5.687,5.687,0,0,0,112.77-4a6.488,6.488,0,0,0,2.534.476A6.488,6.488,0,0,0,117.838-4a5.687,5.687,0,0,0,1.946-1.33A5.868,5.868,0,0,0,121.03-7.35a7.271,7.271,0,0,0,.434-2.562,7.3,7.3,0,0,0-.434-2.548,5.842,5.842,0,0,0-1.246-2.03,5.687,5.687,0,0,0-1.946-1.33A6.488,6.488,0,0,0,115.3-16.3a6.488,6.488,0,0,0-2.534.476,5.687,5.687,0,0,0-1.946,1.33,5.842,5.842,0,0,0-1.246,2.03A7.3,7.3,0,0,0,109.144-9.912ZM146.916-1.4A15.812,15.812,0,0,1,142.884.056,20.476,20.476,0,0,1,138.6.5a12.213,12.213,0,0,1-4.284-.728,9.807,9.807,0,0,1-3.388-2.086A9.457,9.457,0,0,1,128.7-5.6a11.2,11.2,0,0,1-.8-4.312,11.2,11.2,0,0,1,.8-4.312,9.457,9.457,0,0,1,2.226-3.29,9.806,9.806,0,0,1,3.388-2.086,12.213,12.213,0,0,1,4.284-.728,15.855,15.855,0,0,1,4.382.574A8.58,8.58,0,0,1,146.5-17.78l-3.08,3.36a6.137,6.137,0,0,0-2.016-1.4,7.184,7.184,0,0,0-2.8-.476,6.487,6.487,0,0,0-2.534.476,5.687,5.687,0,0,0-1.946,1.33,5.842,5.842,0,0,0-1.246,2.03,7.3,7.3,0,0,0-.434,2.548,7.271,7.271,0,0,0,.434,2.562,5.868,5.868,0,0,0,1.246,2.016A5.687,5.687,0,0,0,136.066-4a6.488,6.488,0,0,0,2.534.476,8.349,8.349,0,0,0,2.464-.322,9.3,9.3,0,0,0,1.652-.658v-3.22H139.16V-11.76h7.756Zm2.772-8.512a11.2,11.2,0,0,1,.8-4.312,9.457,9.457,0,0,1,2.226-3.29A9.807,9.807,0,0,1,156.1-19.6a12.213,12.213,0,0,1,4.284-.728,12.213,12.213,0,0,1,4.284.728,9.807,9.807,0,0,1,3.388,2.086,9.457,9.457,0,0,1,2.226,3.29,11.2,11.2,0,0,1,.8,4.312,11.2,11.2,0,0,1-.8,4.312,9.457,9.457,0,0,1-2.226,3.29,9.807,9.807,0,0,1-3.388,2.086A12.213,12.213,0,0,1,160.384.5,12.213,12.213,0,0,1,156.1-.224a9.807,9.807,0,0,1-3.388-2.086,9.457,9.457,0,0,1-2.226-3.29A11.2,11.2,0,0,1,149.688-9.912Zm4.536,0a7.271,7.271,0,0,0,.434,2.562A5.868,5.868,0,0,0,155.9-5.334,5.687,5.687,0,0,0,157.85-4a6.488,6.488,0,0,0,2.534.476A6.488,6.488,0,0,0,162.918-4a5.687,5.687,0,0,0,1.946-1.33A5.868,5.868,0,0,0,166.11-7.35a7.271,7.271,0,0,0,.434-2.562,7.3,7.3,0,0,0-.434-2.548,5.842,5.842,0,0,0-1.246-2.03,5.687,5.687,0,0,0-1.946-1.33,6.488,6.488,0,0,0-2.534-.476,6.487,6.487,0,0,0-2.534.476,5.687,5.687,0,0,0-1.946,1.33,5.842,5.842,0,0,0-1.246,2.03A7.3,7.3,0,0,0,154.224-9.912Z" transform="matrix(0.995, -0.105, 0.105, 0.995, 484.921, 558.826)" fill="#fff"/>
</g>
</svg>
</div>

Show text on hover on polygon in SVG

Is that possible to add text on hover with svg polygon shape?
My code like this
<svg>
<polygon class="st0" points="0,1.833 638,1.833 383,348.833 0,348.833 "/>
<polygon class="st0" points="0,355.333 649,355.333 891.5,664.833 0,664.833 "/>
<polygon class="st0" points="392.5,348.833 514.75,181.333 645.25,348.833 "/>
<polyline class="st0" points="518.875,174.908 644.667,2.021 1139.833,1.52 1139.75,663.583 897.25,663.583 "/>
</svg>
when I checked many reference there is only examples for a single polygon, But on my code I need 4 shape with different text in a SVG tag. Is that possible to add text on hover with multiple polygons?
Here a fiddle what I have
When I hover I want like this
Any advice would be appreciated
Yo can add
<title>Your text</title>
tag inside <svg></svg> or <poligon></poligon> tag that shows default tooltip with text on it.
Updated jsfiddle
Source:
https://developer.mozilla.org/en/docs/Web/SVG/Element/title
Tooltips not showing when hovering over SVG polygons
.st0 {
fill: #0491B7;
}
.st1 {
fill: #0491B7;
}
.st1:hover {
fill: red;
opacity: 0.5;
}
.st0:hover {
fill: red;
}
text{
display:none;
fill:#fff;
font-size:2em;
font-family:sans-serif;
}
text.sub-text{
display:none;
fill:#fff;
font-size:0.8em;
font-family:sans-serif;
}
g:hover > text{
display:block;
}
<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="1139.833px" height="663.313px" viewBox="0 0 1139.833 663.313" style="enable-background:new 0 0 1139.833 663.313;" xml:space="preserve">
<g>
<polygon class="st0" points="0,0.313 638,0.313 383,347.313 0,347.313 ">
<title>One</title>
</polygon>
<text x="10" y="40"> TITLE </text>
<text x="10" y="55" class="sub-text">Some Content</text>
</g>
<polygon class="st1" points="0,353.813 649,353.813 891.5,663.313 0,663.313 ">
<title>Two</title>
</polygon>
<polygon class="st0" points="392.5,347.313 514.75,179.813 645.25,347.313 ">
<title>Three</title>
</polygon>
<polyline class="st0" points="518.875,173.388 644.667,0.501 1139.833,0 1139.75,662.063 897.25,662.063">
<title>Four</title>
</polyline>
</svg>

How do I use SVG icons in HTML?

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>

How to style one particular SVG path in CSS?

Is it possible to style differently a same SVG symbol with 2 distinct CSS classes. Like in this jsfiddle
.shape-55 {
width: 55px;
height: 55px;
}
/* Want to color only the first path in red */
.shape-style-1 path:nth-of-type(1) {
fill: red;
}
/* Want to color only the first path in blue */
.shape-style-2 path:nth-of-type(1) {
fill: blue;
}
<!-- SVG Spritesheets-->
<svg class="shape" style="display:none;">
<symbol viewBox="0 0 64 64" id="shape-1">
<title>sw_av_a_tkn-cscd</title>
<g>
<g>
<path d="M22.541,0C10.511,0,0.723,4.298,0.723,9.581v4.455c0,5.283,9.788,9.581,21.817,9.581c12.032,0,21.819-4.298,21.819-9.581
V9.581C44.359,4.298,34.572,0,22.541,0z M40.568,14.969v3.733c-2.49,1.708-6.309,3.034-10.837,3.726v-3.804
C34.17,17.941,37.963,16.646,40.568,14.969z M7.197,3.443c1.56,0.624,2.804,1.122,3.78,1.513c1.959-0.907,4.483-1.582,7.34-1.925
c-0.348-0.535-0.787-1.213-1.332-2.061c1.771-0.204,3.635-0.315,5.556-0.315c1.926,0,3.792,0.112,5.569,0.318
c-0.548,0.847-0.984,1.522-1.329,2.058c2.851,0.343,5.381,1.018,7.34,1.925c0.973-0.391,2.215-0.888,3.771-1.508
c3.268,1.453,5.387,3.368,5.748,5.483c-2.227,0.07-3.982,0.123-5.357,0.167c0.025,0.158,0.043,0.318,0.043,0.482
c0,1.35-0.918,2.608-2.498,3.665c1.137,0.313,2.582,0.713,4.414,1.219c-2.543,1.629-6.314,2.89-10.756,3.542
c-0.682-0.83-1.229-1.495-1.661-2.02c-1.649,0.252-3.422,0.388-5.271,0.388c0,0-0.003,0-0.006,0c-1.235,0-0.006,0.66-0.006,0
c0,0,0,0-0.002,0c-1.849,0-3.621-0.136-5.271-0.388c-0.432,0.524-0.979,1.19-1.664,2.021c-4.448-0.653-8.223-1.913-10.765-3.544
c1.828-0.506,3.273-0.905,4.41-1.219c-1.58-1.057-2.495-2.315-2.495-3.665c0-0.164,0.018-0.324,0.041-0.482
C5.424,9.055,3.672,9.002,1.44,8.932C1.801,6.814,3.923,4.899,7.197,3.443z M4.512,14.969c2.611,1.679,6.406,2.975,10.854,3.656
v3.806c-4.541-0.691-8.359-2.019-10.854-3.729V14.969z"></path>
<path d="M22.541,13.296c4.596,0,8.199-1.633,8.199-3.715c0-2.084-3.604-3.718-8.199-3.718c-4.597,0-8.197,1.634-8.197,3.718
C14.343,11.663,17.944,13.296,22.541,13.296z M22.541,6.521c4.089,0,7.54,1.4,7.54,3.06c0,1.658-3.451,3.059-7.54,3.059
c-4.087,0-7.541-1.4-7.541-3.059C15,7.922,18.454,6.521,22.541,6.521z"></path>
</g>
<path d="M25.038,34.037c-0.81,0.059-1.638,0.094-2.483,0.094c0,0-0.003,0-0.006,0c0,0-0.002,0-0.006,0c0,0,0,0-0.002,0
c-1.849,0-3.621-0.141-5.271-0.391c-0.432,0.524-0.979,1.191-1.664,2.022c-4.448-0.653-8.223-1.912-10.765-3.546
c1.288-0.355,2.376-0.655,3.312-0.912c-3.259-1.173-5.81-2.753-7.378-4.609c-0.028,0.211-0.052,0.424-0.052,0.64v4.456
c0,5.266,9.721,9.548,21.694,9.58C22.864,38.741,23.758,36.27,25.038,34.037z M15.367,40.185c-4.541-0.69-8.359-2.021-10.854-3.729
v-3.733c2.611,1.679,6.406,2.976,10.854,3.658V40.185z"></path>
<path d="M22.083,45.084c0-0.699,0.041-1.388,0.104-2.069c-1.72-0.018-3.375-0.143-4.917-0.376c-0.432,0.524-0.979,1.189-1.664,2.02
c-4.448-0.652-8.223-1.912-10.765-3.544c1.288-0.355,2.376-0.659,3.312-0.915c-3.259-1.172-5.81-2.749-7.378-4.605
c-0.028,0.21-0.052,0.426-0.052,0.639v4.456c0,5.281,9.788,9.578,21.817,9.578c0.061,0,0.116-0.003,0.175-0.003
C22.316,48.6,22.083,46.868,22.083,45.084z M15.367,49.08c-4.541-0.69-8.359-2.017-10.854-3.728v-3.731
c2.611,1.679,6.406,2.974,10.854,3.655V49.08z"></path>
<path d="M23.161,51.904c-0.204,0.003-0.402,0.023-0.606,0.023c0,0-0.003,0-0.006,0c0,0-0.002,0-0.006,0c0,0,0,0-0.002,0
c-1.849,0-3.621-0.14-5.271-0.391c-0.432,0.524-0.979,1.189-1.664,2.023c-4.448-0.653-8.223-1.915-10.765-3.548
c1.288-0.355,2.376-0.655,3.312-0.912c-3.259-1.172-5.81-2.752-7.378-4.611c-0.028,0.213-0.052,0.426-0.052,0.642v4.456
c0,5.282,9.788,9.581,21.817,9.581c1.519,0,2.999-0.069,4.427-0.198C25.3,56.883,23.998,54.499,23.161,51.904z M15.367,57.979
c-4.541-0.691-8.359-2.018-10.854-3.726v-3.736c2.611,1.679,6.406,2.976,10.854,3.657V57.979z"></path>
<g>
<path d="M44.359,22.805v-4.37c0-0.213-0.02-0.428-0.055-0.639c-1.568,1.858-4.119,3.437-7.375,4.608
c0.863,0.237,1.877,0.518,3.043,0.84C41.393,22.958,42.859,22.805,44.359,22.805z"></path>
<path d="M26.123,32.331c1.037-1.479,2.25-2.82,3.608-4.007v-0.846c0.454-0.069,0.897-0.151,1.341-0.232
c0.432-0.322,0.868-0.633,1.323-0.923c-0.933,0.207-1.9,0.391-2.909,0.539c-0.682-0.831-1.229-1.495-1.661-2.02
c-1.649,0.249-3.422,0.388-5.271,0.388c0,0-0.003,0-0.006,0c0,0-0.002,0-0.006,0c0,0,0,0-0.002,0
c-1.849,0-3.621-0.139-5.271-0.388c-0.432,0.524-0.979,1.188-1.664,2.021c-4.448-0.653-8.223-1.914-10.765-3.546
c1.288-0.355,2.376-0.657,3.312-0.914c-3.259-1.172-5.81-2.75-7.378-4.608c-0.028,0.211-0.052,0.426-0.052,0.639v4.456
c0,5.283,9.788,9.58,21.817,9.58C23.761,32.471,24.954,32.417,26.123,32.331z M15.367,31.285
c-4.541-0.693-8.359-2.018-10.854-3.728v-3.733c2.611,1.679,6.406,2.974,10.854,3.656V31.285z"></path>
</g>
<path d="M44.359,26.166c-10.445,0-18.914,8.469-18.914,18.918C25.446,55.53,33.914,64,44.359,64
c10.451,0,18.918-8.47,18.918-18.916C63.277,34.635,54.811,26.166,44.359,26.166z M53.428,46.976c0,0.49-0.402,0.889-0.891,0.889
h-5.393v5.393c0,0.49-0.4,0.893-0.891,0.893h-3.789c-0.486,0-0.889-0.402-0.889-0.893v-5.393h-5.393
c-0.488,0-0.889-0.398-0.889-0.889v-3.786c0-0.49,0.4-0.892,0.889-0.892h5.393v-5.393c0-0.489,0.402-0.886,0.889-0.886h3.789
c0.49,0,0.891,0.396,0.891,0.886v5.393h5.393c0.488,0,0.891,0.401,0.891,0.892V46.976z"></path>
</g>
</symbol>
</svg>
<div>
<svg class="shape-55 shape-style-1">
<use xlink:href="#shape-1"></use>
</svg>
</div>
<div>
<svg class="shape-55 shape-style-2">
<use xlink:href="#shape-1"></use>
</svg>
</div>
I need to color the first "path" in red when using CSS class shape-style-1 and in blue when using shape-style-2.
Thank you.
You can style the parent div's colors, and use fill: currentColor to use that color for the appropriate SVG element.
So this CSS:
div > svg {
width: 55px;
height: 55px;
}
svg g g > path:nth-of-type(1) {
fill: currentColor;
}
div:nth-of-type(even) {
color: red;
}
div:nth-of-type(odd) {
color: blue;
}
… used with this HTML:
<div><svg><use xlink:href="#shape-1"></use></svg></div>
<div><svg><use xlink:href="#shape-1"></use></svg></div>
<div><svg><use xlink:href="#shape-1"></use></svg></div>
<div><svg><use xlink:href="#shape-1"></use></svg></div>
… will look like this:
Fiddle
You can achieve what you want, but without CSS.
See this fiddle: http://jsfiddle.net/41e0f7z5/2/
The trick is that you can set a fill attribute to the <use> element. Then, your symbol is composed of a path and a group owning all the other pathes. This group has a fillattribute set to black. But, because the firt path has no fillattribute, it will inherit from the <use> one.
<svg class="shape" style="display:none;">
<symbol viewBox="0 0 64 64" id="shape-1">
<path d="..."/>
<g fill="black">
<path d="..." />
<path d="..." />
<path d="..." />
...
</g>
</symbol>
</svg>
<div>
<svg class="shape-55">
<use xlink:href="#shape-1" fill="red"></use>
</svg>
</div>
<div>
<svg class="shape-55">
<use xlink:href="#shape-1" fill="blue"></use>
</svg>
</div>
Something like this?
.shape-55 {
width: 55px;
height: 55px;
}
<body>
<!-- SVG Spritesheets-->
<svg class="shape" style="display:none;">
<symbol viewBox="0 0 64 64" id="shape-1">
<title>sw_av_a_tkn-cscd</title>
<path d="M22.541,0C10.511,0,0.723,4.298,0.723,9.581v4.455c0,5.283,9.788,9.581,21.817,9.581c12.032,0,21.819-4.298,21.819-9.581
V9.581C44.359,4.298,34.572,0,22.541,0z M40.568,14.969v3.733c-2.49,1.708-6.309,3.034-10.837,3.726v-3.804
C34.17,17.941,37.963,16.646,40.568,14.969z M7.197,3.443c1.56,0.624,2.804,1.122,3.78,1.513c1.959-0.907,4.483-1.582,7.34-1.925
c-0.348-0.535-0.787-1.213-1.332-2.061c1.771-0.204,3.635-0.315,5.556-0.315c1.926,0,3.792,0.112,5.569,0.318
c-0.548,0.847-0.984,1.522-1.329,2.058c2.851,0.343,5.381,1.018,7.34,1.925c0.973-0.391,2.215-0.888,3.771-1.508
c3.268,1.453,5.387,3.368,5.748,5.483c-2.227,0.07-3.982,0.123-5.357,0.167c0.025,0.158,0.043,0.318,0.043,0.482
c0,1.35-0.918,2.608-2.498,3.665c1.137,0.313,2.582,0.713,4.414,1.219c-2.543,1.629-6.314,2.89-10.756,3.542
c-0.682-0.83-1.229-1.495-1.661-2.02c-1.649,0.252-3.422,0.388-5.271,0.388c0,0-0.003,0-0.006,0c-1.235,0-0.006,0.66-0.006,0
c0,0,0,0-0.002,0c-1.849,0-3.621-0.136-5.271-0.388c-0.432,0.524-0.979,1.19-1.664,2.021c-4.448-0.653-8.223-1.913-10.765-3.544
c1.828-0.506,3.273-0.905,4.41-1.219c-1.58-1.057-2.495-2.315-2.495-3.665c0-0.164,0.018-0.324,0.041-0.482
C5.424,9.055,3.672,9.002,1.44,8.932C1.801,6.814,3.923,4.899,7.197,3.443z M4.512,14.969c2.611,1.679,6.406,2.975,10.854,3.656
v3.806c-4.541-0.691-8.359-2.019-10.854-3.729V14.969z"></path>
<g fill="black">
<path d="M22.541,13.296c4.596,0,8.199-1.633,8.199-3.715c0-2.084-3.604-3.718-8.199-3.718c-4.597,0-8.197,1.634-8.197,3.718
C14.343,11.663,17.944,13.296,22.541,13.296z M22.541,6.521c4.089,0,7.54,1.4,7.54,3.06c0,1.658-3.451,3.059-7.54,3.059
c-4.087,0-7.541-1.4-7.541-3.059C15,7.922,18.454,6.521,22.541,6.521z"></path>
<path d="M25.038,34.037c-0.81,0.059-1.638,0.094-2.483,0.094c0,0-0.003,0-0.006,0c0,0-0.002,0-0.006,0c0,0,0,0-0.002,0
c-1.849,0-3.621-0.141-5.271-0.391c-0.432,0.524-0.979,1.191-1.664,2.022c-4.448-0.653-8.223-1.912-10.765-3.546
c1.288-0.355,2.376-0.655,3.312-0.912c-3.259-1.173-5.81-2.753-7.378-4.609c-0.028,0.211-0.052,0.424-0.052,0.64v4.456
c0,5.266,9.721,9.548,21.694,9.58C22.864,38.741,23.758,36.27,25.038,34.037z M15.367,40.185c-4.541-0.69-8.359-2.021-10.854-3.729
v-3.733c2.611,1.679,6.406,2.976,10.854,3.658V40.185z"></path>
<path d="M22.083,45.084c0-0.699,0.041-1.388,0.104-2.069c-1.72-0.018-3.375-0.143-4.917-0.376c-0.432,0.524-0.979,1.189-1.664,2.02
c-4.448-0.652-8.223-1.912-10.765-3.544c1.288-0.355,2.376-0.659,3.312-0.915c-3.259-1.172-5.81-2.749-7.378-4.605
c-0.028,0.21-0.052,0.426-0.052,0.639v4.456c0,5.281,9.788,9.578,21.817,9.578c0.061,0,0.116-0.003,0.175-0.003
C22.316,48.6,22.083,46.868,22.083,45.084z M15.367,49.08c-4.541-0.69-8.359-2.017-10.854-3.728v-3.731
c2.611,1.679,6.406,2.974,10.854,3.655V49.08z"></path>
<path d="M23.161,51.904c-0.204,0.003-0.402,0.023-0.606,0.023c0,0-0.003,0-0.006,0c0,0-0.002,0-0.006,0c0,0,0,0-0.002,0
c-1.849,0-3.621-0.14-5.271-0.391c-0.432,0.524-0.979,1.189-1.664,2.023c-4.448-0.653-8.223-1.915-10.765-3.548
c1.288-0.355,2.376-0.655,3.312-0.912c-3.259-1.172-5.81-2.752-7.378-4.611c-0.028,0.213-0.052,0.426-0.052,0.642v4.456
c0,5.282,9.788,9.581,21.817,9.581c1.519,0,2.999-0.069,4.427-0.198C25.3,56.883,23.998,54.499,23.161,51.904z M15.367,57.979
c-4.541-0.691-8.359-2.018-10.854-3.726v-3.736c2.611,1.679,6.406,2.976,10.854,3.657V57.979z"></path>
<path d="M44.359,22.805v-4.37c0-0.213-0.02-0.428-0.055-0.639c-1.568,1.858-4.119,3.437-7.375,4.608
c0.863,0.237,1.877,0.518,3.043,0.84C41.393,22.958,42.859,22.805,44.359,22.805z"></path>
<path d="M26.123,32.331c1.037-1.479,2.25-2.82,3.608-4.007v-0.846c0.454-0.069,0.897-0.151,1.341-0.232
c0.432-0.322,0.868-0.633,1.323-0.923c-0.933,0.207-1.9,0.391-2.909,0.539c-0.682-0.831-1.229-1.495-1.661-2.02
c-1.649,0.249-3.422,0.388-5.271,0.388c0,0-0.003,0-0.006,0c0,0-0.002,0-0.006,0c0,0,0,0-0.002,0
c-1.849,0-3.621-0.139-5.271-0.388c-0.432,0.524-0.979,1.188-1.664,2.021c-4.448-0.653-8.223-1.914-10.765-3.546
c1.288-0.355,2.376-0.657,3.312-0.914c-3.259-1.172-5.81-2.75-7.378-4.608c-0.028,0.211-0.052,0.426-0.052,0.639v4.456
c0,5.283,9.788,9.58,21.817,9.58C23.761,32.471,24.954,32.417,26.123,32.331z M15.367,31.285
c-4.541-0.693-8.359-2.018-10.854-3.728v-3.733c2.611,1.679,6.406,2.974,10.854,3.656V31.285z"></path>
<path d="M44.359,26.166c-10.445,0-18.914,8.469-18.914,18.918C25.446,55.53,33.914,64,44.359,64
c10.451,0,18.918-8.47,18.918-18.916C63.277,34.635,54.811,26.166,44.359,26.166z M53.428,46.976c0,0.49-0.402,0.889-0.891,0.889
h-5.393v5.393c0,0.49-0.4,0.893-0.891,0.893h-3.789c-0.486,0-0.889-0.402-0.889-0.893v-5.393h-5.393
c-0.488,0-0.889-0.398-0.889-0.889v-3.786c0-0.49,0.4-0.892,0.889-0.892h5.393v-5.393c0-0.489,0.402-0.886,0.889-0.886h3.789
c0.49,0,0.891,0.396,0.891,0.886v5.393h5.393c0.488,0,0.891,0.401,0.891,0.892V46.976z"></path>
</g>
</symbol>
</svg>
<div>
<svg class="shape-55 shape-style-1" fill="red">
<use xlink:href="#shape-1"></use>
</svg>
</div>
<div>
<svg class="shape-55 shape-style-2" fill="blue">
<use xlink:href="#shape-1"></use>
</svg>
</div>
</body>