Why does the background path of this svg change on hover - html
I'm using the following markup:
<a href="#">
<!-- first svg -->
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" id="Icon" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 48 48" enable-background="new 0 0 48 48" xml:space="preserve">
<linearGradient id="SVGID_1_" gradientUnits="userSpaceOnUse" x1="23.9995" y1="0" x2="23.9995" y2="48.0005">
<stop offset="0" style="stop-color:#FFFFFF"/>
<stop offset="1" style="stop-color:#EAEAEA"/>
</linearGradient>
<path fill-rule="evenodd" clip-rule="evenodd" fill="url(#SVGID_1_)" d="M48,42c0,3.313-2.687,6-6,6H6c-3.313,0-6-2.687-6-6V6
c0-3.313,2.687-6,6-6h36c3.313,0,6,2.687,6,6V42z"/>
<path fill-rule="evenodd" clip-rule="evenodd" fill="none" d="M27.695,29.811c0,0,1.519,5.062,5.974,4.05
c4.456-1.013,4.759-6.684,2.127-9.619C33.163,21.305,26.379,25.962,27.695,29.811z"/>
<path fill-rule="evenodd" clip-rule="evenodd" d="M11.828,18.179c0,0-3.344,3.499-2.763,9.192
c0.581,5.694,4.186,10.392,16.208,10.392c12.021,0,15.045-6.275,15.116-11.436c0.071-5.159-2.253-7.46-3.344-8.243
c0,0,0.007-3.704-0.343-5.661c0,0-1.85-0.219-5.845,2.07c0,0-5.454-0.533-12.722,0.065c0,0-3.053-2.04-6.129-2.574
C12.006,11.984,11.496,15.196,11.828,18.179z"/>
<path fill-rule="evenodd" clip-rule="evenodd" fill="#E2B89F" d="M17.211,23.815h14.916c0,0,4.227-0.475,4.227,6.44
c0.034,6.086-11.139,5.693-11.139,5.693s-12.236,0.486-12.243-6.269C12.956,23.579,17.211,23.815,17.211,23.815z"/>
<g>
<g>
<path fill-rule="evenodd" clip-rule="evenodd" fill="#9C584F" d="M30.767,26.591c0.959,0,1.737,1.25,1.737,2.787
c0,1.54-0.778,2.788-1.737,2.788c-0.958,0-1.736-1.248-1.736-2.788C29.03,27.841,29.809,26.591,30.767,26.591z"/>
<path fill="#FFFFFF" d="M30.767,32.666c-1.254,0-2.236-1.444-2.236-3.288c0-1.843,0.982-3.287,2.236-3.287
c1.255,0,2.237,1.444,2.237,3.287C33.004,31.222,32.021,32.666,30.767,32.666z M30.767,27.091c-0.585,0-1.236,0.939-1.236,2.287
c0,1.349,0.651,2.288,1.236,2.288s1.237-0.939,1.237-2.288C32.004,28.03,31.352,27.091,30.767,27.091z"/>
</g>
</g>
<g>
<g>
<path fill-rule="evenodd" clip-rule="evenodd" fill="#9C584F" d="M18.767,26.591c0.959,0,1.737,1.25,1.737,2.787
c0,1.54-0.778,2.788-1.737,2.788c-0.958,0-1.736-1.248-1.736-2.788C17.03,27.841,17.809,26.591,18.767,26.591z"/>
<path fill="#FFFFFF" d="M18.767,32.666c-1.254,0-2.236-1.444-2.236-3.288c0-1.843,0.982-3.287,2.236-3.287
c1.254,0,2.237,1.444,2.237,3.287C21.004,31.222,20.021,32.666,18.767,32.666z M18.767,27.091c-0.585,0-1.236,0.939-1.236,2.287
c0,1.349,0.651,2.288,1.236,2.288c0.585,0,1.237-0.939,1.237-2.288C20.004,28.03,19.352,27.091,18.767,27.091z"/>
</g>
</g>
<path fill-rule="evenodd" clip-rule="evenodd" fill="#9C584F" d="M24.076,32.705c0,0,0.499-1.418,1.109-0.089
c0,0-0.457,0.297-0.285,0.996l1.428,0.546h-3.23l1.28-0.575C24.378,33.583,24.562,32.527,24.076,32.705z"/>
</svg>
</a>
<a href="#">
<!-- second svg -->
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 17.1.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" 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 480 480" enable-background="new 0 0 480 480" xml:space="preserve">
<linearGradient id="SVGID_1_" gradientUnits="userSpaceOnUse" x1="261.3877" y1="161.9675" x2="261.3877" y2="173.4675" gradientTransform="matrix(41.667 0 0 41.667 -10651.2422 -6748.2861)">
<stop offset="0" style="stop-color:#EC9728"/>
<stop offset="1" style="stop-color:#DF4C18"/>
</linearGradient>
<path fill="url(#SVGID_1_)" d="M479.6,419.7c0,33.1-26.8,59.9-59.9,59.9H60.3c-33.1,0-59.9-26.8-59.9-59.9V60.3
c0-33.1,26.8-59.9,59.9-59.9h359.4c33.1,0,59.9,26.8,59.9,59.9V419.7z"/>
<circle fill="#009245" cx="142.8" cy="139" r="92"/>
<circle fill="#D4145A" cx="304.8" cy="148" r="92"/>
<ellipse fill="#D9E021" cx="206.2" cy="320.8" rx="182.4" ry="137.8"/>
<ellipse fill="#0000FF" cx="388.6" cy="217.1" rx="69.8" ry="131.5"/>
</svg>
</a>
and the following styling:
svg {
display: inline-block;
width: 64px;
height: 64px;
}
The result displays a behavior which I don't understand.
Why does the background path of the first svg change color on hover to match the color of the second svg? This behavior is consistent regardless of which svg is placed first. The first svg (Github icon) is part of Zurb's webicons. Here is a Codepen which illustrates the problem in question. Please note that I'm using Bootstrap.
You have two gradients with the same ID #SVGID_1_, which is illegal in a XML file. One in each SVG:
<linearGradient id="SVGID_1_" ...> ... </linearGradient>
Two paths reference it. That is probably confusing the browser making it behave strangely. It seems that a different gradient object is referenced when the hover event over the link occurs, causing the change in color (I am not sure.) Different algorithms might select one or the other in different moments, since they expect the IDs to be unique.
If you use different IDs for each gradient the problem will not occur.
You have multiple layers on page with same id. This can happen after Adobe Illustrator export. As workaround you can include css properties directly in svg objects instead class styles.
When you save illustration as SVG, in SVG More Options make sure that in CSS Properties you have selected Presentation Atributes.
Related
Is it legal to put <svg> resources after </body>?
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>
linearGradient not working for SVG sprites
I am trying to combine all svg icons into one sprite file like this for organization, caching and other purposes. But for some reason, linearGradient is not being applied <?xml version="1.0" encoding="utf-8"?> <svg version="1.1" id="icons" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 50 50" style="enable-background:new 0 0 50 50;" xml:space="preserve"> <!-- other icons --> <!-- ... --> <g id="toggle"> <path d="M4,7.992c-2.206,0-4-1.794-4-4c0-2.206,1.794-4,4-4h10c2.206,0,4,1.794,4,4c0,2.206-1.794,4-4,4H4z"/> <circle fill="url('#toggle-linear-gradient')" cx="14" cy="3.992" r="3"/> <defs> <linearGradient id="toggle-linear-gradient" gradientUnits="userSpaceOnUse" x1="14" y1="0.9922" x2="14" y2="6.9922"> <stop offset="0" stop-color="#FFFFFF"/> <stop offset="1" stop-color="#8C8C8B"/> </linearGradient> </defs> </g> </svg> Now, in file preview I can see circle with applied gradient correctly When I try to import it into my HTML like this <svg> <use href="#toggle" xlink:href="#toggle"></use> </svg> the circle is not being colored with defined gradient When I moved <linearGradient> out of svg file, it worked, but why? <svg> <linearGradient id="toggle-linear-gradient" gradientUnits="userSpaceOnUse" x1="14" y1="0.9922" x2="14" y2="6.9922"> <stop offset="0" stop-color="#FFFFFF"/> <stop offset="1" stop-color="#8C8C8B"/> </linearGradient> <use href="#toggle" xlink:href="#toggle"></use> </svg> Even tho it worked, the problem is, that this kills the purpose of keeping all the icons in one file if some styling can only be defined from the outside. Displaying same icon will force me defining same gradient multiple times. Would love to have everything in the same file. Any ideas or insights? P.S. importing as <img> displays the circle gradient correctly.
Pretty sure, you have hidden your main svg asset file by display:none. If you change this to visibility:hidden it should work: function unhide(){ document.querySelector('.dsp-non').classList.remove('dsp-non'); } .svgAssetHidden{ visibility:hidden; position:absolute; width:0px; height:0px; overflow:hidden; } .svgIcon{ display:inline-block; width:10em; } .dsp-non{ display:none; } <button onclick="unhide()">remove display:none</button> <h3>visibility:hidden</h3> <svg class="svgIcon" viewBox="0 0 50 20"> <use href="#toggle" href="#toggle"></use> </svg> <h3>display:none</h3> <svg class="svgIcon" viewBox="0 0 50 20"> <use href="#toggle2" href="#toggle"></use> </svg> <svg class="svgAssetHidden" id="icons" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 50 50" style="enable-background:new 0 0 50 50;" xml:space="preserve"> <!-- other icons --> <!-- ... --> <g id="toggle"> <path d="M4,7.992c-2.206,0-4-1.794-4-4c0-2.206,1.794-4,4-4h10c2.206,0,4,1.794,4,4c0,2.206-1.794,4-4,4H4z"/> <circle fill="url('#toggle-linear-gradient')" cx="14" cy="3.992" r="3"/> <defs> <linearGradient id="toggle-linear-gradient" gradientUnits="userSpaceOnUse" x1="14" y1="0.9922" x2="14" y2="6.9922"> <stop offset="0" stop-color="#FFFFFF"/> <stop offset="1" stop-color="#8C8C8B"/> </linearGradient> </defs> </g> </svg> <svg class="svgAssetDspNon dsp-non" id="icons" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 50 50" style="enable-background:new 0 0 50 50;" xml:space="preserve"> <!-- other icons --> <!-- ... --> <g id="toggle2"> <path d="M4,7.992c-2.206,0-4-1.794-4-4c0-2.206,1.794-4,4-4h10c2.206,0,4,1.794,4,4c0,2.206-1.794,4-4,4H4z"/> <circle fill="url('#toggle-linear-gradient2')" cx="14" cy="3.992" r="3"/> <defs> <linearGradient id="toggle-linear-gradient2" gradientUnits="userSpaceOnUse" x1="14" y1="0.9922" x2="14" y2="6.9922"> <stop offset="0" stop-color="#FFFFFF"/> <stop offset="1" stop-color="#8C0000"/> </linearGradient> </defs> </g> </svg> Hiding a svg by display:none will also break some other features like filters.
SVG image as a cursor pixcelated
I'm using this custom size 64*64 plus icon svg as zoom icon as a cursor in my website. As you can see, it's pixcelated. Specially the white border. Don't know why. Because it's svg it suppose to looks sharp right? I tried everything. Increasing border width. Increasing size. Set shape-rendering="auto" None of above working. Here is my svg code <?xml version="1.0" encoding="utf-8"?> <!-- Generator: Adobe Illustrator 19.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) --> <svg width="64px" height="64px" 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="-285 377 40 40" style="enable-background:new -285 377 40 40;" xml:space="preserve" shape-rendering="auto"> <style type="text/css"> .st0{fill:none;} .st1{opacity:0.8;fill:none;stroke:#FFFFFF;stroke-width:3;stroke-miterlimit:10;enable-background:new ;} </style> <title>zoom_icon</title> <g> <title>background</title> <rect id="canvas_background" x="-286" y="376" class="st0" width="12.6" height="12.6"/> </g> <g> <title>Layer 1</title> <g id="Layer_2_1_"> <g id="Layer_1-2"> <circle id="svg_1" shape-rendering="geometricPrecision" class="st1" cx="-264.8" cy="397.2" r="18"/> <line id="svg_2" class="st1" x1="-264.8" y1="386.7" x2="-264.8" y2="407.7"/> <line id="svg_3" class="st1" x1="-254.3" y1="397.2" x2="-275.3" y2="397.2"/> </g> </g> </g> </svg> Here is my the svg file: https://svgshare.com/i/H_C.svg Here is the real world example. Jsfiddle What course to pixcelated this image?
you can try removing the circle and just use a border: .plus { border: solid white 1px; border-radius: 50%; }
How to implement an animating gradient within my SVG example
Hi I've been looking at source code that implements a colour gradient on a quick example SVG that I made within Adobe Illustrator. For some reason I can't seem to work out how to get it to work such that the section "st0" are affected by the gradient named "logo-gradient". Any help? <?xml version="1.0" encoding="utf-8"?> <!-- Generator: Adobe Illustrator 22.1.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) --> <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" style="enable-background:new 0 0 47.4 47.7;" xml:space="preserve" width="50" height="50"> <defs> <linearGradient id="logo-gradient" x1="50%" y1="0%" x2="50%" y2="100%" > <stop offset="0%" stop-color="#7A5FFF"> <animate attributeName="stop-color" values="#7A5FFF; #01FF89; #7A5FFF" dur="4s" repeatCount="indefinite"></animate> </stop> <stop offset="100%" stop-color="#01FF89"> <animate attributeName="stop-color" values="#01FF89; #7A5FFF; #01FF89" dur="4s" repeatCount="indefinite"></animate> </stop> </linearGradient> </defs> <style type="text/css"> .st0{fill:"url('#logo-gradient')"} </style> <g id="Layer_2"> <rect x="-11.8" y="-9" width="66" height="63"/> </g> <g id="Layer_1"> <g> <g> <path d="M12.1,30.4v1c0,8.1,3.9,11.9,8.7,11.9c3.9,0,7.3-1.8,9.4-6.6h0.1c-1.4,7.4-5.8,11-12.5,11c-7,0-13.6-4.6-13.6-15.1 c0-11.4,7.1-15.6,13.9-15.6c6.1,0,12.2,3.2,12.2,11.8c0,0.5-0.1,1-0.1,1.5H12.1z M12.1,29.3h10.8c0.1-0.5,0.1-0.9,0.1-1.2 c0-6.9-1.6-10.1-5-10.1C14.4,18,12.4,21.9,12.1,29.3z"/> <path d="M44.1,43.2c0,0.7,0.1,0.8,0.7,1.3l3.6,2.6v0.1h-16v-0.1l3.6-2.6c0.5-0.4,0.6-0.7,0.6-1.3V8.8c0-0.8-0.1-1-0.6-1.4 l-3.6-2.6V4.7l11.8-1.4V43.2z"/> </g> <g> <path class="st0" d="M12.1,30.4v1c0,8.1,3.9,11.9,8.7,11.9c3.9,0,7.3-1.8,9.4-6.6h0.1c-1.4,7.4-5.8,11-12.5,11 c-7,0-13.6-4.6-13.6-15.1c0-11.4,7.1-15.6,13.9-15.6c6.1,0,12.2,3.2,12.2,11.8c0,0.5-0.1,1-0.1,1.5H12.1z M12.1,29.3h10.8 c0.1-0.5,0.1-0.9,0.1-1.2c0-6.9-1.6-10.1-5-10.1C14.4,18,12.4,21.9,12.1,29.3z"/> <path class="st0" d="M44.1,43.2c0,0.7,0.1,0.8,0.7,1.3l3.6,2.6v0.1h-16v-0.1l3.6-2.6c0.5-0.4,0.6-0.7,0.6-1.3V8.8 c0-0.8-0.1-1-0.6-1.4l-3.6-2.6V4.7l11.8-1.4V43.2z"/> </g> </g> </g> </svg>
Chrome gradient in symbol from external svg not rendered
I have a svg file which defines a symbol like that: test.svg <?xml version="1.0" encoding="UTF-8"?> <svg xmlns="http://www.w3.org/2000/svg" xml:space="preserve" width="1024px" height="1024px" shape-rendering="geometricPrecision" text-rendering="geometricPrecision" image-rendering="optimizeQuality" fill-rule="evenodd" clip-rule="evenodd" viewBox="0 0 1024 1024" xmlns:xlink="http://www.w3.org/1999/xlink"> <defs> <linearGradient id="id0"> <stop offset="0" stop-color="#E6E6E6"/> <stop offset="1" stop-color="gray"/> </linearGradient> </defs> <symbol id="fid_group" class="logo"> <rect x="0" y="0" width="50" height="50" fill="url(#id0)" stroke="black" stroke-width="1"/> </symbol> </svg> I try to use it the following way: <html> <head> </head> <body> <svg xmlns="http://www.w3.org/2000/svg" xml:space="preserve" width="1024px" height="1024px" shape-rendering="geometricPrecision" text-rendering="geometricPrecision" image-rendering="optimizeQuality" fill-rule="evenodd" clip-rule="evenodd" viewBox="0 0 1024 1024" xmlns:xlink="http://www.w3.org/1999/xlink"> <use xlink:href="test.svg#fid_group"></use> </svg> </body> </html> In Firefox I get the square with the gradient inside, in Chrome I get only the black stroke. Is there something wrong with how I use the gradient or the symbol?