Css hover sometimes doesn't work on svg paths - html
I got a svg with paths, and i have css hover on them, but hover sometimes work, sometimes not.
What can be the problem?
<div id="map_wrapper">
<svg width="640" height="480" xmlns="http://www.w3.org/2000/svg">
<!-- Created with SVG-edit - http://svg-edit.googlecode.com/ -->
<g>
<title>Layer 1</title>
<path id="svg_3" d="m200,114l-114,95l26,97l99,20c0,0 19,-67 19,-68c0,-1 -1,-5 4,-8c5,-3 39,-10 40,-10c1,0 13,-2 14,-9c1,-7 -4,-36 -8,-40c-4,-4 -23,-15 -27,-17c-4,-2 -24,-16 -24,-23c0,-7 -1,-15 -1,-21c0,-6 -6,-19 -7,-19c-1,0 -21,3 -21,3z" stroke-linecap="null" stroke-linejoin="null" stroke-dasharray="null" stroke-width="5" stroke="#000000" fill="none"/>
<path id="svg_4" d="m244,101c0,0 14,55 16,56c2,1 22,11 24,13c2,2 23,17 24,18c1,1 1,28 -1,31c-2,3 -3,25 -9,30c-6,5 -32,14 -35,14c-3,0 -8,5 -10,8c-2,3 -10,37 -10,37c0,0 7,10 16,15c9,5 53,12 59,12c6,0 30,0 40,-8c10,-8 34,-7 35,-31c1,-24 1,-48 1,-65c0,-17 -13,-61 -15,-66c-2,-5 -21,-21 -21,-23c0,-2 34,-20 44,-15c10,5 29,24 33,28c4,4 10,20 16,5c6,-15 28,-31 -1,-46c-29,-15 -25,-24 -55,-25c-30,-1 -42,-5 -53,-5c-11,0 -46,-2 -52,1c-6,3 -46,16 -46,16z" stroke-linecap="null" stroke-linejoin="null" stroke-dasharray="null" stroke-width="5" stroke="#000000" fill="none"/>
<path id="svg_5" d="m428,180c0,0 -3,52 -4,53c-1,1 -2,27 -2,31c0,4 -8,29 -11,34c-3,5 -15,36 -21,38c-6,2 -77,18 -81,18c-4,0 -68,0 -68,7c0,7 -1,18 8,23c9,5 23,9 45,14c22,5 97,12 111,6c14,-6 44,-20 55,-30c11,-10 28,-28 37,-42c9,-14 14,-15 23,-40c9,-25 16,-109 12,-114c-4,-5 -32,-12 -45,-9c-13,3 -59,11 -59,11z" stroke-linecap="null" stroke-linejoin="null" stroke-dasharray="null" stroke-width="5" stroke="#000000" fill="none"/>
</g>
</svg>
</div>
and css
path{
fill:none;
stroke:black;
}
path:hover{
fill:red;
stroke:blue;
}
Here is the fiddle, just hover them quickly.
http://jsfiddle.net/gWXbn/
There's no fill so the interior does not catch mouse events by default and therefore hover doesn't react to that. Changing pointer-events to all will fix it in this case:
path{
fill:none;
stroke:black;
pointer-events:all;
}
The following fixed my problem with svg and jQuery hover and alike.
svg, svg * {
pointer-events: none;
}
This always works for me when using object tag
object {
pointer-events: none;
}
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.
Change SVG fill="none" to fill="#FFF" when Hover?
This SVG have 2 fill one is NONE and secound is color RED. How I do so when it's not hover one stay in NONE, then when it's hover it change to color? By the way it's right now, it only change the one that is red of the .icon{ fill: red; transition: all 200ms ease-out; } .icon:hover{ fill: blue; } <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="22" viewBox="0 0 24 22"> <path fill="none" d="M17.73,3c-3.26,0-5,3.47-5.73,5-.75-1.54-2.48-5-5.72-5A4.05,4.05,0,0,0,2,7.19c0,3.44,4.74,7.85,10,13,5.26-5.15,10-9.56,10-13A4.06,4.06,0,0,0,17.73,3Z" transform="translate(0 -1)" /> <path fill="#ff0000" d="M17.73,1A6.53,6.53,0,0,0,12,4.25,6.51,6.51,0,0,0,6.28,1,6,6,0,0,0,0,7.19C0,11.85,5.57,16.62,12,23c6.43-6.38,12-11.15,12-15.81A6,6,0,0,0,17.73,1ZM12,20.19C6.74,15,2,10.63,2,7.19A4.05,4.05,0,0,1,6.28,3c3.24,0,5,3.49,5.72,5,.75-1.55,2.47-5,5.73-5A4.06,4.06,0,0,1,22,7.19C22,10.63,17.26,15,12,20.19Z" transform="translate(0 -1)"/> </svg>
Try adding a wrapper class and giving css fill to the svg path. This is a sample code. Hope it helps .svg-wrapper svg path{ fill: red; transition: all 200ms ease-out; } .svg-wrapper svg path:hover{ fill: blue; } <div class="svg-wrapper"> <svg xmlns="http://www.w3.org/2000/svg" width="24" height="22" viewBox="0 0 24 22"> <path fill="none" d="M17.73,3c-3.26,0-5,3.47-5.73,5-.75-1.54-2.48-5-5.72-5A4.05,4.05,0,0,0,2,7.19c0,3.44,4.74,7.85,10,13,5.26-5.15,10-9.56,10-13A4.06,4.06,0,0,0,17.73,3Z" transform="translate(0 -1)" /> <path fill="#ff0000" d="M17.73,1A6.53,6.53,0,0,0,12,4.25,6.51,6.51,0,0,0,6.28,1,6,6,0,0,0,0,7.19C0,11.85,5.57,16.62,12,23c6.43-6.38,12-11.15,12-15.81A6,6,0,0,0,17.73,1ZM12,20.19C6.74,15,2,10.63,2,7.19A4.05,4.05,0,0,1,6.28,3c3.24,0,5,3.49,5.72,5,.75-1.55,2.47-5,5.73-5A4.06,4.06,0,0,1,22,7.19C22,10.63,17.26,15,12,20.19Z" transform="translate(0 -1)"/> </svg> </div>
You can animate it with an SVG tag. Here is how you would animate the fill property back and forth with an hover effect on a square: <?xml version="1.0"?> <svg width="120" height="120" viewBox="0 0 120 120" version="1.1" xmlns="http://www.w3.org/2000/svg"> <rect x="10" y="10" width="100" height="100" fill='#000000'> <animate attributeType="XML" attributeName="fill" from="#000000" to="#ff0000" dur="0.5s" repeatCount="1" begin='mouseover' fill='freeze'/> <animate attributeType="XML" attributeName="fill" from="#ff0000" to="#000000" dur="0.5s" repeatCount="1" begin='mouseout' fill='freeze'/> </rect> </svg> When this SVG is placed on a page it will turn red when you rollover it and go back to black when rolling out.
Why is this an empty space appearing inside of SVG element?
I have the fiddle with this problem. If I set viewBox property on the symbol element, icon displayed correctly. But if I set viewBox with same value on svg element with use inside, around use element appears weird empty space, inside of SVG-container. Viewport of first variant is the same with viewport of second variant - 35x35px size at 2.5x3.5px coords. What's the reason of this behavior? Bug or my own mistake? EDIT: Add picture with correct and incorrect areas. #svg-icons { display: none; } .icon { display: inline-block; border: 1px solid red; } .icon + .icon { margin-left: 20px; } <svg version="1.1" id="svg-icons" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <symbol id="icon-1" viewBox="2.5 3.5 35 35"> <rect x="2.5" y="3.5" stroke="#000000" stroke-miterlimit="10" width="35" height="35" /> <circle fill="#FFFFFF" stroke="#000000" stroke-miterlimit="10" cx="20" cy="21" r="14" /> </symbol> <symbol id="icon-2"> <rect x="2.5" y="3.5" stroke="#000000" stroke-miterlimit="10" width="35" height="35" /> <circle fill="#FFFFFF" stroke="#000000" stroke-miterlimit="10" cx="20" cy="21" r="14" /> </symbol> </svg> <svg class="icon icon-1" width="100" height="100"> <use xlink:href="#icon-1"></use> </svg> <svg class="icon icon-2" viewBox="2.5 3.5 35 35" width="100"> <use xlink:href="#icon-2"></use> </svg>
This probrem will be solved by thinking about how the use element is treated on drawing. According to SVG 1.1 2nd edition, use element refers symbol element is treated as svg element on drawing. So the result by that svg structure is same as this. <svg class="icon icon-2" viewBox="2.5 3.5 35 35" width="100"> <g> <svg width="100%" height="100%"> <rect x="2.5" y="3.5" stroke="#000000" stroke-miterlimit="10" width="35" height="35" /> <circle fill="#FFFFFF" stroke="#000000" stroke-miterlimit="10" cx="20" cy="21" r="14" /> </svg> </g> </svg> Inner svg element has range of 100% width and height positioned at (0,0), and outer svg element has viewBox offset to (2.5, 3.5). Thus shapes are cutoff by displacement of 2 svg element's rendering ranges.
Why the <use> element in SVG doesn't work?
I have the following simple example. It is stored in image.svg: <svg> <defs> <g id="shape"> <circle cx="100" cy="100" r="100" /> </g> </defs> </svg> However, putting this code in a HTML file doesn't load anything. Why is that? <svg> <use xlink:href="#shape" x="10" y="10" /> </svg> What am I doing wrong? I can't seem to make it work.
If you are using elements from another document, you have to specify the document! <use xlink:href="#shape" x="10" y="10" /> This means "use the #shape element from the current document". To import from another document, you need to put the reference to the SVG file in the xlink:href attribute: <use xlink:href="image.svg#shape" x="10" y="10" /> Obviously you need to check the path is correct here. Note that this is not supported in any version of Internet Explorer, though polyfills are available.
For external svg files you need the namespace ... and I have added a fill to render the circle otherwise it will be transparent: <svg xmlns="http://www.w3.org/2000/svg" > <symbol id="shape" width="200" height="200" viewbox="0 0 200 200"> <circle cx="100" cy="100" r="100" fill="currentColor" /> </symbol> <text y="20">Symbol above will not render unless referenced by use element</text> </svg> Then when you reference it you need to use the correct namespace for xlink: svg.defs-only { display:block; position: absolute; height:0; width:0; margin: 0; padding: 0; border: none; overflow: hidden; } svg { color: orange; stroke: red; } .purple { color: purple; stroke: black; } <svg class="defs-only" xmlns="http://www.w3.org/2000/svg" > <symbol id="shape" width="50" height="50" viewbox="0 0 50 50"> <circle cx="25" cy="25" r="20" fill="currentColor" stroke="inherit" /> </symbol> </svg> <svg xmlns:xlink="http://www.w3.org/1999/xlink"> <use xlink:href="#shape" x="10" y="10" /> <use xlink:href="#shape" x="80" y="10" class="purple" /> </svg> If you are referencing an external file, you need to put the filename before the # e.g. image.svg#shape making sure you get the path correct of course. Note, not all browsers support fragment identifiers - notably IE and Edge - you need to use a javascript polyfill like svg4everybody for those browsers. Workaround - use svg inline only
You need to have the use-tag inside the SVG with the shape you want to use: <svg> <defs> <g id="shape"> <circle cx="100" cy="100" r="100" /> </g> </defs> <use xlink:href="#shape" x="10" y="10" /> </svg>
SVG 2 (when implemented in browsers) will allow to reference another SVG file without any fragment identifier: New in SVG 2: An href without a fragment allows an entire SVG document to be referenced without having to ensure that it has an ID on its root element. Before (SVG 1.1): <!-- my-vector.svg --> <svg id="icon" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"> <circle r="10" cx="12" cy="12" /> </svg> <use href="my-vector.svg#icon"></use> After (there will be no need to define id="..." on the svg): <!-- my-vector.svg --> <svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"> <circle r="10" cx="12" cy="12" /> </svg> <use href="my-vector.svg"></use> SVG 2 seems to be in the process of development in major browsers (see this Chrome feature and specifically this Chromium issue: Issue 366545: [SVG2] Allow to reference entire files).
Clip-path using svg html element is not working
I'm trying to clip an image using a svg but it is not working. Follows the code: img { clip-path: url(#svgPath); } <svg xmlns:xlink="http://www.w3.org/1999/xlink"> <defs> <clipPath id="svgPath"> <rect x="286.5" y="95" stroke="#000000" stroke-miterlimit="10" width="274" height="274"></rect> <rect x="286.5" y="391" stroke="#000000" stroke-miterlimit="10" width="274" height="274"></rect> <rect x="582.5" y="95" stroke="#000000" stroke-miterlimit="10" width="274" height="274"></rect> <rect x="582.5" y="391" stroke="#000000" stroke-miterlimit="10" width="274" height="274"></rect> </clipPath> </defs> </svg> <img src="https://s3-us-west-1.amazonaws.com/powr/defaults/image-slider2.jpg" /> Thank you very much
You need to use the -webkit- vendor prefix as chrome, opera and safari consider clip-path to be an experimental feature. More on clip-path browser support can be found on caniuse.com. body { margin: 0; } img { -webkit-clip-path: url(#svgPath); clip-path: url(#svgPath); } <img src="https://s3-us-west-1.amazonaws.com/powr/defaults/image-slider2.jpg" /> <svg xmlns:xlink="http://www.w3.org/1999/xlink"> <defs> <clipPath id="svgPath"> <rect x="286.5" y="95" stroke="#000000" stroke-miterlimit="10" width="274" height="274" fill="#ffffff"></rect> <rect x="286.5" y="391" stroke="#000000" stroke-miterlimit="10" width="274" height="274"></rect> <rect x="582.5" y="95" stroke="#000000" stroke-miterlimit="10" width="274" height="274"></rect> <rect x="582.5" y="391" stroke="#000000" stroke-miterlimit="10" width="274" height="274"></rect> </clipPath> </defs> </svg>