Recently I exported a map document from the ESRI ArcMap 10.2.2 to SVG.
I would like to animate this map with css and javascript however the "clippath" polygons that arcmap creates makes it impossible to do so. How would can I get the group "Puerto Rico" to highlight over mouseover.
This is a summary of the code:
<svg width="1008pt" height="612pt" viewBox="0 0 1008 612" enable-background="new 0 0 1008 612" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="Puerto_Rico_and_Virgin_Islands">
<g id="state" class="test">
<g id="Puerto_Rico">
<clipPath id="SVG_CP_1">
<path d= coordinates are put in here />
</clipPath>
</g>
</g>
The CSS
<style type="text/css">
.test{
fill: grey;
stroke:#fff;
stroke-width:0.75;
stroke-opacity:1;
fill: orange;
}
#state:hover {
fill: red !important;
</style>
The real code:
visit jsfiddle for the entire code http://jsfiddle.net/jwitcoski/arv4g21f/3/
Using the wikipedia svg map that does not use clippath I was able to get everything to work. http://jsfiddle.net/jwitcoski/y3yhgjjy/9/
The clipPath issue is a red herring. It has nothing to do with why your CSS isn't working.
The reason your sample fiddle wasn't working was for two reasons:
Your CSS was missing a closing '}'. But I am guessing this was just a typo introduced in your fiddle.
The main reason was that your path definition for PR had a presentation attribute for fill.
<path fill="none" ... />
This presentation attribute has a higher priority than the CSS rule because it is more specific and thus over-rides it. If you remove the fill="none", everything works.
Demo fiddle here
Related
Can we do this?
Use SYMBOL CSS SVG Fragment USE in CSS as background: url or similar?
It seems to me, all other things being equal, SYMBOL use does not work, where G with separate viewbox works. If this cannot be done, why? Is there a better way to do SVG Spritesheets?
SYMBOL has an ID, has a viewBox..am I doing somthing wrong or using symbol inc. viewbox, VS, separate viewbox & g = Does Not Work? oO
Am I doing somthing wrong, or, does the below CSS SVG fragment usage only work with the separate viewbox and G (and not work with symbol with viewbox)?
SYMBOL SVG
<symbol xmlns="http://www.w3.org/2000/svg" transform="rotate(360)" viewBox="0 0 24 24" id="basket">
<path fill="none" stroke="#626262" stroke-width="2" d="M2 12h20l-2 11H4L2 12zm18-4l-6-7M4 8l6-7M1 8h22v4H1V8zm7 7v5m8-5v5m-4-5v5"></path>
<path fill="rgba(0, 0, 0, 0)" d="M0 0h24v24H0z"></path>
</symbol>
..VS..
G & VIEWBOX SVG
<view id="icon-clock-view" viewBox="0 0 32 32" />
<g id="icon-clock">
<path d="M20.6,23.3L14,16.7V7.9h4v7.2l5.4,5.4L20.6,23.3z M16-0.1c-8.8,0-16,7.2-16,16s7.2,16,16,16s16-7.2,16-16S24.8-0.1,16-0.1z
M16,27.9c-6.6,0-12-5.4-12-12s5.4-12,12-12s12,5.4,12,12S22.6,27.9,16,27.9z"/>
</g>
CSS
.icon-clock {background: url(https://example.com/svg-defs.svg#icon-clock-view) no-repeat;}
.basket {background: url(https://example.com/svg-defs.svg#basket) no-repeat;}
This seems to works great. Using 'name' as example to follow.
Wrap <path>(s) & </path> within <symbol> </symbol> I did not use <g>
Add id="name" to <symbol> e.g. <symbol id="name">
Outside the symbol tags, add a <view> and a <use>. The URL, where the complete file is uploaded to, and view ID will become your SVG Fragment.
<view id="icon-name-view" viewBox="0 1240 16 16" />
<use xlink:href="#icon-name" width="16" height="16" x="0" y="1240" id="name"></use>
Change the viewbox to locate the SVG in the spritesheet, here I'd used Y axis, this particular SVG is located at Y axis 1240. Save the SVG Spritesheet to say, svg-defs.svg.
In your CSS use the SVG spritesheet URL with the Fragment Ident e.g.
background: url(https://example.com/svg/svg-defs.svg#icon-name-view) no-repeat;
To change colours I was using filter, e.g.
filter: invert(80%) sepia(21%) saturate(872%) hue-rotate(44deg) brightness(84%) contrast(91%);
Positioning can be done with the ViewBox's but I also used transform, translate.
transform: translateY (10px);
Hope you find this useful, I hope to write a complete guide and will link it when I do. Thank you.
I'm working on a project that uses svg. I generate this svg code thanks to an ocaml library:
<svg xmlns="http://www.w3.org/2000/svg" xmlns:l="http://www.w3.org/1999/xlink" version="1.1" width="161.8mm" height="100mm" viewBox="0 0 161.8 100" color-profile="auto" color-interpolation="linearRGB" color-interpolation-filters="linearRGB">
<g fill="none" stroke-miterlimit="9.98123" transform="matrix(100 0 0 -100 -0 100)">
<defs>
<path id="i1" d="M0 0L1.618 0L1.618 1L0 1Z"/>
</defs>
<use l:href="#i1" fill="#50C878"/>
</g>
</svg>
When I use gthumb or GIMP to display the svg, it prints the correct green square. However, when I include this block of code in Chromium or Firefox, the block appears in the html tree but nothing is display. This is the first time I use svg: after several researches on Google, I can't find anything....
Does someone know why it doesn't work ?
Edit
Solve with the answer:
<use xlink:href="#i1">
I define an SVG element as such
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<symbol id="clock" viewBox="0 0 30 30">
<circle class="clock_base" fill="none" stroke="black" stroke-width="3" ......></circle>
<line class="clock_hour" .....</line>
<line class="clock_minute" .....</line>
</symbol>
</defs>
</svg>
And it is inserted in my DOM with <use>
<svg width="100%" viewBox="0 0 30 30"><use xlink:href="clock.svg#clock"></use></svg>
I see the element so no problem there. What I wanted was to rotate the hour and minute hands independently. So I tried
.clock_hour {
animation: spin 2s infinite linear;
}
Which does nothing.
I know my animation works because I can apply it to the svg and the whole thing spins, but I just want the hands to spin. How can I do this?
It is because when the browser finishes parsing your css file and try to apply styles to each declared selector, it does not see a <line> element with class name clock_hour because it is in a different file that has to be loaded, and it happens after the css file was parsed and applied, so that is why you don't see the animation running on class_hour. If you copy the markup in your clock.svg and insert it in the same page as the other svg, it will apply the animation correctly
Chrome v49 broke letter-spacings in SVG when used in combination with matrix transformation and translations:
Living example: https://jsfiddle.net/75fpn6de/6/
SVG:
<svg height="300px" version="1.1" width="100%" xmlns="http://www.w3.org/2000/svg">
<g transform="matrix(0.20695652173913043,0,0,0.20695652173913043,0,10.881739130434767)">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ev="http://www.w3.org/2001/xml-events" version="1.1" viewBox="0 0 2500 2794" width="2500" height="2794">
<g transform="translate(1436.5 1087) rotate(0)">
<text xmlns="http://www.w3.org/2000/svg" fill="#ed7373" y="237" style="font-family: 'Great Vibes';" font-size="237" letter-spacing="0.1em" text-anchor="middle">Some text</text>
</g>
</svg>
</g>
</svg>
CSS:
#import url(https://fonts.googleapis.com/css?family=Great+Vibes);
Am I doing anything wrong here (like breaking some specs) and Chrome got more "restrictive" or is it a newly introduced bug in Chrome?
FYI: The example is (obviously) only a small part of a bigger SVG which is dynamically generated by the server and I can't just change the nesting and the usage of the transform matrix & translate functions.
I had the very same issue, and even if I couldn't find anything about this issue in the changelog, it seems it has been fixed in Chrome 50.
I have an html file that uses inline svg. I use this so I can add classes to my svg and style them with CSS. The following snippet works:
<div>
<svg class="icon" viewbox="0 0 512 512">
<path d=" ... "/>
</svg>
</div>
Howeever, the tag can be quite long if the svg is complex. I'm currently using this svg in 3 different locations, and everytime I need to copy paste the entire path. It would be better if I could define the path only 1 time, preferabvly in a css class like this:
<div>
<svg class="icon" viewbox="0 0 512 512">
<path class="compleximage"/>
</svg>
</div>
.compleximage
{
d: ... ;
}
But this doesn't seem to work. Maybe I'm getting it wrong syntactically, or maybe it can't be done this way. If so, are there ways other to prevent having to copy/paste the svg in my html files? I'm trying to follow the "0,1 or infinite" design pattern, and copy/pasting code 3 times break that.
You can use the use tag to display the path in more than one place. Just give the path an id attribute and then refer to that in the xlink:href of the <use> element.
Something like
<defs>
<path id="image1" d="..." />
</defs>
<use x="20" y="10" xlink:href="#image1" />
<use x="50" y="50" xlink:href="#image1" />
etc.
In HTML5 you can do it.
Please try this:
<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg>
On the other hand, you can create your own SVG and get it via <img src="" />.
Sample below:
<img class="papa" src="http://s.cdpn.io/3/kiwi.svg">
Reference here:
(1) http://css-tricks.com/using-svg/
Another reference here how to change it via css:
(2) http://codepen.io/chriscoyier/pen/evcBu
You cannot set svg attributes using CSS. You can only set styles like stroke, fill, etc.
The only way would be to use javascript to set those paths dynamically. I would recommend jQuery.
this link should be helpful for using jQuery to modify svg files.
Modify a svg file using jQuery