SVG - inherit multiple colors/animations - svg

What I try to do:
Using a <use> element to copy an icon, and color the icon in two different colors when a specific class is added to the <use> element.
The Icon:
<symbol xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
id="red5" x="0px" y="0px" viewBox="0 0 48.1 50.8">
<style type="text/css">
.st0{
fill:#D73647;
stroke:#000000;
stroke-miterlimit:10;
}
/* use.active .color-a{
fill: green;
}
use.active .color-b{
fill: blue;
}*/ // Not working...
</style>
<g>
<path class="st0 color-a" d="M2.3,20.2L11.8,20.2C11.8,20.2 12.5,10.9 22.3,11.2C22.3,11.2 28.5,11.1 32.3,16L26.3,22.4L47.5,22.4L47.5,2.7L40.4,9C40.4,9 36.1,0.4 23.6,0.5C23.6,0.5 4.8,-0.2 2.3,20.2z">
</path>
<path class="st0 color-b" d="M45.8,30.6L36.3,30.6C36.3,30.6 35.6,39.9 25.8,39.6C25.8,39.6 19.6,39.7 15.8,34.8L21.8,28.4L0.5,28.4L0.5,48.1L7.7,41.7C7.7,41.7 12,50.3 24.5,50.2C24.5,50.3 43.3,51 45.8,30.6z">
</path>
</g>
</symbol>
The Use Element:
<use id="svg_16"
xlink:href="#red5" transform="matrix(0.6730555893894703,0,0,0.7071457914654147,-239.09557391490307,-165.87702520953462) "
y="269.9999919533732"
x="473.99998587369964"
class="default-state"
fill="black"></use>
When the class of the <use> is changed from "default-state" to "active", I want the colors of the arrows to change (each to its own color).
What I've tried so far:
I understood that for the paths to change their color from the <use> element I have to change their CSS class to this:
.st0{
fill: inherit; // <- changed
stroke: #000000;
stroke-miterlimit: 10;
}
and the color classes to this:
use.active {
fill: green;
}
but then when I set the class of <use> to "active" they both get the same color...
I would like to do the same thing for animations.
What am I missing? How do I achive this?

This is how I would do it: I would create one symbol and I would reuse twice the same path: once as it is and once rotated 180 degs. For the "default" state please remove the active class of the g element.
.st0{
stroke-miterlimit:10;
}
.active .color-a{
fill: green;
}
.active .color-b{
fill: blue;
}
svg{width:90vh;border:1px solid}
<svg viewBox="0 0 140 150" >
<symbol id="a" viewBox="0 0 48.1 50.8">
<path class="st0 color-a" id="k" d="M2.3,20.2L11.8,20.2C11.8,20.2 12.5,10.9 22.3,11.2C22.3,11.2 28.5,11.1 32.3,16L26.3,22.4L47.5,22.4L47.5,2.7L40.4,9C40.4,9 36.1,0.4 23.6,0.5C23.6,0.5 4.8,-0.2 2.3,20.2z">
</path>
</symbol>
<g class="active" id="svg_16" >
<use class="color-a"
xlink:href="#a">
</use>
<use class="color-b" transform="rotate(180 70 75)"
xlink:href="#a"> </use>
</g>
</svg>

Related

css rotate introduces outline when using SVG clip-path or mask

Is there a way to stop this outline in firefox?
#blob { background: red; width: 500px; height: 500px; clip-path: url(#myClip); transform: rotate(20deg);}
<div id="blob"></div>
<svg>
<defs>
<path d="M320.403196,424.677624 C426.787532,365.585154 447.310044,306.188587 433.45394,197.28033 C419.597836,88.3720737 316.997962,53.8862578 227.347416,40.9086547 C144.650118,28.9375873 104.472702,88.6407456 69.862267,131.812053 C15.52584,199.588564 48.3439099,300.905451 80.8563197,361.757908 C110.80391,417.809872 214.018859,483.770094 320.403196,424.677624 Z" id="path-1"></path>
<clipPath id="myClip"><use href="#path-1"></use></clipPath>
</defs>
</svg>
This renders correctly in other browsers, just need some work around for this firefox bug
same outline appears using either clip-path or mask
As a workaround you could rotate the clipPath instead:
#blob {
background: red;
width: 500px;
height: 500px;
clip-path: url(#myClip);
}
<div id="blob"></div>
<svg viewBox="0 0 397 409">
<defs>
<path d="M320.403196,424.677624 C426.787532,365.585154 447.310044,306.188587 433.45394,197.28033 C419.597836,88.3720737 316.997962,53.8862578 227.347416,40.9086547 C144.650118,28.9375873 104.472702,88.6407456 69.862267,131.812053 C15.52584,199.588564 48.3439099,300.905451 80.8563197,361.757908 C110.80391,417.809872 214.018859,483.770094 320.403196,424.677624 Z" id="path-1" />
<clipPath id="myClip" transform="rotate(20)" transform-origin="center">
<use href="#path-1"></use>
</clipPath>
</defs>
</svg>

CSS transform scale function moving SVG item position automatically

I am working on a navigation bar in a circle format split in 5.All the elements are SVGs.
It really look like a pie chart.While hovering one part, i would like to scale the svg element to the exterior. As i am trying to use scale with this, the element just translate itself further and do the scale effect.I'm not sure but i think it is a problem of overlapping css element? Anyway if someone could help me with this how to tell the part to stay at the same place and then scale bigger, or do i need to manually re-translate the element at the correct place?
Thanks, here is a represatation:
#group-part-1>text {
visibility: hidden;
}
#group-part-1:hover text {
visibility: visible;
}
#group-part-2>text {
visibility: hidden;
}
#group-part-2:hover text {
visibility: visible;
}
#group-part-3>text {
visibility: hidden;
}
#group-part-3:hover text {
visibility: visible;
}
#group-part-4>text {
visibility: hidden;
}
#group-part-4:hover text {
visibility: visible;
}
#group-part-5>text {
visibility: hidden;
}
#group-part-5:hover text {
visibility: visible;
}
#part-1:hover {
fill: red;
transform: scale(1.2);
}
#part-2:hover {
fill: green;
transform: scale(1.2);
}
#part-3:hover {
fill: purple;
}
#part-4:hover {
fill: orange;
}
#part-5:hover {
fill: blue;
}
<svg width="210" height="297" viewBox="0 0 210 297" fill="none" xmlns="http://www.w3.org/2000/svg">
<g id="example">
<g id="group-part-5">
<path id="part-5"
d="M156.393 107.998C159.608 118.055 159.557 128.873 156.247 138.899C152.936 148.926 146.536 157.647 137.964 163.813L108.768 123.223L156.393 107.998Z"
fill="black" stroke="black" stroke-width="0.0132292" />
<text x="160" y="150" fill="black">Project</text>
</g>
<g id="group-part-4">
<path id="part-4"
d="M137.963 163.813C129.391 169.979 119.087 173.272 108.529 173.222C97.9698 173.171 87.6979 169.78 79.1853 163.532L108.768 123.223L137.963 163.813Z"
fill="#1A1A1A" stroke="black" stroke-width="0.0132292" />
<text x="90" y="200" fill="black">About</text>
</g>
<g id="group-part-3">
<path id="part-3"
d="M79.2054 163.547C70.6897 157.304 64.3689 148.526 61.1491 138.469C57.9293 128.413 57.9756 117.596 61.2815 107.568L108.768 123.223L79.2054 163.547Z"
fill="#333333" stroke="black" stroke-width="0.0132292" />
<text x="10" y="150" fill="black">Contact</text>
</g>
<g id="group-part-2">
<path id="part-2"
d="M61.2928 107.534C64.606 97.508 71.008 88.7885 79.5814 82.625C88.1547 76.4615 98.4593 73.1703 109.018 73.2231L108.768 123.223L61.2928 107.534Z"
fill="#4D4D4D" stroke="black" stroke-width="0.0132292" />
<text x="25" y="75" fill="black">Home</text>
</g>
<g id="group-part-1">
<path id="part-1"
d="M108.941 73.2228C119.5 73.2594 129.776 76.6378 138.297 82.8738C146.818 89.1097 153.146 97.8831 156.374 107.937L108.768 123.223L108.941 73.2228Z"
fill="#666666" stroke="black" stroke-width="0.0132292" />
<text x="150" y="75" fill="black">Work</text>
</g>
</svg>
thanks
This is the way I would do it:
the svg element is centered around the point {x:0,y:0}: viewBox="-105 -105 210 210"
I calculate the points for the arc in base of the angle of the wedge and the radius of the circle
In this example I'm rotating the text too so I'm putting both the wedge and the text in the same group (#group_part5) and I'm transforming the group on mouse over: transform: translate(10px, 0) scale(1.2);
I'm wrapping everything in a different group (#example) and I'm rotating this group to the needed position.
//the angle for the circle wedge
let angle = 2*Math.PI/5;
//the radius of the circle wedge
let r = 60;
//calculate the points for the arc
let p1 = {x:r*Math.cos(-angle/2),
y:r*Math.sin(-angle/2)};
let p2 = {x:r*Math.cos(angle/2),
y:r*Math.sin(angle/2)};
//build the d attribute
let d = `M0,0L${p1.x},${p1.y}A${r},${r} 0 0 1 ${p2.x},${p2.y} z`;
//set the d attribute of the path
part5.setAttribute("d",d);
svg {
border: solid;
}
#example {
transform: rotate(36deg);
}
#group_part5:hover {
fill: red;
transform: translate(10px, 0) scale(1.2);
}
#group_part5:hover text {
fill: black;
}
<svg width="210" viewBox="-105 -105 210 210">
<g id="example">
<g id="group_part5">
<path id="part5" d="" />
<text fill="none" x="30">Project</text>
</g>
</g>
</svg>
Observation: If you don't want to use javascript you can take the d attribute for the path from the inspector.
UPDATE
The OP is commenting
The only thing is that the 'origin' top-left corner is moving, i want it to stay at the same place as well as the borders, the border only need to be longer and the circle exterior border further
If I understand you correctly in the previous demo please replace transform: translate(10px, 0) scale(1.2); with transform: scale(1.2);
If this is what you need there is a simpler way to do this: instead or scaling the wedge you can add a wide stroke - the same color as the fill - like in the following demo:
Please take a look:
svg {
border: solid;
}
#example {
transform: rotate(36deg);
}
#group_part5:hover {
fill: red;
/*transform: translate(10px, 0) scale(1.2);*/
}
#group_part5:hover path:nth-of-type(2){stroke:red;}
#group_part5:hover text {
fill: black;
}
<svg width="210" viewBox="-105 -105 210 210">
<g id="example">
<g id="group_part5">
<path id="part5" d="M0,0L48.54101966249685,-35.26711513754839A60,60 0 0 1 48.54101966249685,35.26711513754839 z"></path>
<path d="M48.54101966249685,-35.26711513754839A60,60 0 0 1 48.54101966249685,35.26711513754839" fill="none" stroke-width="10"></path>
<text fill="none" x="30">Project</text>
</g>
</g>
</svg>

SVG odd (random) colors

I am using .SVG files instead of .PNG for a small website I am building and I am encountering an issue with colors.
Whenever I add or remove an .SVG file to my code, all the the other .SVG file (logos) change colors and can't figure out why.
Here is an example of an SVG file :
<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"
viewBox="0 0 512 512" style="enable-background:new 0 0 512 512;" xml:space="preserve">
<style type="text/css">
.st0{fill:#27346A;}
.st1{fill:#2790C3;}
.st2{fill:#1F264F;}
</style>
<g>
<path class="st0" d="M408.5,38.6C384.8,11.6,341.9,0,287.1,0H128c-11.2,0-20.7,8.2-22.5,19.2L39.2,439.4
c-1.3,8.3,5.1,15.8,13.5,15.8H151l24.7-156.5l-0.8,4.9c1.8-11.1,11.2-19.2,22.4-19.2H244c91.7,0,163.5-37.2,184.5-145
c0.6-3.2,1.2-6.3,1.6-9.3c-2.6-1.4-2.6-1.4,0,0C436.3,90.3,430,63.2,408.5,38.6"/>
<path class="st0" d="M213.2,115.7c2.6-1.2,5.5-1.9,8.6-1.9h124.7c14.8,0,28.6,1,41.1,3c3.6,0.6,7.1,1.2,10.5,2
c3.4,0.8,6.7,1.6,9.9,2.5c1.6,0.5,3.2,0.9,4.7,1.5c6.2,2.1,12,4.5,17.2,7.3c6.2-39.8,0-66.9-21.6-91.5C384.8,11.6,341.9,0,287.1,0
H128c-11.2,0-20.7,8.2-22.5,19.2L39.2,439.4c-1.3,8.3,5.1,15.8,13.5,15.8H151l51.2-324.6C203.2,123.9,207.5,118.5,213.2,115.7z"/>
<path class="st1" d="M428.4,139.4c-21,107.7-92.8,145-184.5,145h-46.7c-11.2,0-20.7,8.2-22.4,19.2l-30.7,194.6
c-1.1,7.3,4.5,13.8,11.8,13.8h82.8c9.8,0,18.1-7.1,19.7-16.8l0.8-4.2l15.6-98.9l1-5.5c1.5-9.7,9.9-16.8,19.7-16.8h12.4
c80.2,0,143-32.6,161.4-126.8c7.7-39.4,3.7-72.3-16.6-95.4c-6.1-7-13.8-12.8-22.7-17.5C429.6,133.1,429.1,136.2,428.4,139.4z"/>
<path class="st2" d="M408.1,121.3c-3.2-0.9-6.5-1.8-9.9-2.5c-3.4-0.8-6.9-1.4-10.5-2c-12.6-2-26.4-3-41.2-3H221.8
c-3.1,0-6,0.7-8.6,2c-5.8,2.8-10,8.2-11.1,14.9l-26.5,168.1l-0.8,4.9c1.7-11.1,11.2-19.2,22.4-19.2H244
c91.7,0,163.5-37.2,184.5-145c0.6-3.2,1.1-6.3,1.6-9.3c-5.3-2.8-11.1-5.2-17.2-7.3C411.3,122.3,409.7,121.8,408.1,121.3"/>
</g>
</svg>
I attached a codepen portion of the code : https://codepen.io/testingsonmcd/pen/NWRLdgJ
If you remove the last svg file, the first 2 logos will display with the desired colors.
The <style> inside your svgs aren't scoped, meaning their rules will affect the whole document.
Since several of these rules affect the same property of the same selector, they'll conflict with each other.
Simplification of OP's problem:
<svg viewBox="0 0 50 50" width="50" height="50">
<style>
rect { fill: red; }
</style>
<rect width="50" height="50"/>
</svg>
<svg viewBox="0 0 50 50" width="50" height="50">
<style>
rect { fill: green; }
</style>
<rect width="50" height="50"/>
</svg>
<svg viewBox="0 0 50 50" width="50" height="50">
<style>
rect { fill: blue; }
</style>
<rect width="50" height="50"/>
</svg>
Simply choose better selectors for your CSS, for instance you could add a unique class name to each of the root <svg> and append that in front of each selector:
<svg class="svg-1" viewBox="0 0 50 50" width="50" height="50">
<style>
.svg-1 rect { fill: red; }
</style>
<rect width="50" height="50"/>
</svg>
<svg class="svg-2" viewBox="0 0 50 50" width="50" height="50">
<style>
.svg-2 rect { fill: green; }
</style>
<rect width="50" height="50"/>
</svg>
<svg class="svg-3" viewBox="0 0 50 50" width="50" height="50">
<style>
.svg-3 rect { fill: blue; }
</style>
<rect width="50" height="50"/>
</svg>

How to style an image tag in a SVG?

I would like to know if it is possible to target the below image tag, located in an SVG, and style it externally with CSS.
<g id="Vector_Smart_Object_xA0_Image_1_" class="trapSVG" >
<image style="overflow:visible;" width="15" height="15" id="Vector_Smart_Object_xA0_Image" xlink:href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAA
GXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAZlJREFUeNqcUz1LA0EQfbN3SYwf
+IEWBpSQRhAtLCy0EWws/QGCP8JKbEUri1QWaiEGLewsbQQb2xQJNoKIQgwqaooEk0t2nDlyEE1C
jAOzO+x7b3Znd5bQbEZ8zgVmZR6vAhGJyxI/S5yROS1uA7L7SzwWAtY8YHmSzGycKBYBhcvgygNz
7p5tRvArwU+F+6oCahQ7wNYQ0fqqcUcWyUGCDHoFKImLGDdcw4Wtvn8yn9SA3SCJf2w5yvYwUXnH
jdjHcD9zeKDJdV1x5Sm/Xq5v8xK9bzhhW2whbHTFlad81TmqlmEzBlo6DEVp9EdVzSZ3gGnj0HnN
ixaBL1O/iJUlKWKigzgw5SlfdX4CeZP4DBl0Y8pXXXeqNk2jw0OWbVdC5avOT8DA5bWt4kmjP5jy
lK86P4E0RSoH/kjWKlzqIFZcecpXnVNfz0vQdwu7YAgmToYGW7yI7nxgPd63niftnJSiz4IEegNZ
WexJs53Kg6NvQi4I8CLznfiVtHHKeji23keB+Uh23xO4SO0+U+Ifn+lf3/lbgAEAuN/KQ8s2cHgA
AAAASUVORK5CYII=" transform="matrix(0.75 0 0 0.75 913.5 276.75)">
</image>
</g>
</svg>
Sure, if you are using inline SVG.
#Vector_Smart_Object_xA0_Image {
transform: rotate(45deg);
}
#svg-rect {
fill: transparent;
stroke: gray;
stroke-width: .5px;
transition: fill .25s;
}
#svg-rect:hover {
fill: yellow;
}
<g id="Vector_Smart_Object_xA0_Image_1_" class="trapSVG" >
<image style="overflow:visible;" width="15" height="15" id="Vector_Smart_Object_xA0_Image" xlink:href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAA
GXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAZlJREFUeNqcUz1LA0EQfbN3SYwf
+IEWBpSQRhAtLCy0EWws/QGCP8JKbEUri1QWaiEGLewsbQQb2xQJNoKIQgwqaooEk0t2nDlyEE1C
jAOzO+x7b3Znd5bQbEZ8zgVmZR6vAhGJyxI/S5yROS1uA7L7SzwWAtY8YHmSzGycKBYBhcvgygNz
7p5tRvArwU+F+6oCahQ7wNYQ0fqqcUcWyUGCDHoFKImLGDdcw4Wtvn8yn9SA3SCJf2w5yvYwUXnH
jdjHcD9zeKDJdV1x5Sm/Xq5v8xK9bzhhW2whbHTFlad81TmqlmEzBlo6DEVp9EdVzSZ3gGnj0HnN
ixaBL1O/iJUlKWKigzgw5SlfdX4CeZP4DBl0Y8pXXXeqNk2jw0OWbVdC5avOT8DA5bWt4kmjP5jy
lK86P4E0RSoH/kjWKlzqIFZcecpXnVNfz0vQdwu7YAgmToYGW7yI7nxgPd63niftnJSiz4IEegNZ
WexJs53Kg6NvQi4I8CLznfiVtHHKeji23keB+Uh23xO4SO0+U+Ifn+lf3/lbgAEAuN/KQ8s2cHgA
AAAASUVORK5CYII=" transform="matrix(0.75 0 0 0.75 913.5 276.75)">
</image>
</g>
</svg>
<svg width="20" height="20">
<rect id="svg-rect" width="15" height="15" x="2" y="2"></rect>
</svg>
Is this just example image? It would be much simpler to use SVG rect element for this.

SVG will not display in browser

I am trying to display a simple bar in the browser using svg but for some reason it is not displaying. Here is the code I am using. It does not display the bar.
<h3>SVG Bar</h3>
<svg>
<rect with="50" height="200" style="fill: blue"/>
</svg>
What am I missing?
You've written your width attribute as "with".
But even correcting the typo, you can make a few more improvements:
svg {
border: 1px solid black;
width: 20vw;
height: 20vw;
}
<h3>SVG Bar</h3>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" viewBox="0 0 1000 1000">
<rect x="0" y="0" width="200" height="500" style="fill: blue">
</svg>

Resources