I'm trying to make the markers unified for a bunch of SVG images. My problem is that I cannot make external references in marker definitions work. It may be connected to question How to reference external svg file in svg correctly? but a link is still missing.
I made a little example to demonstrate my problem:
b.svg (which is referenced):
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1">
<defs>
<circle id="b" r="6" stroke="black" fill="green" />
<marker id="b_end"
orient="auto"
style="overflow:visible">
<use xlink:href="#b" />
</marker>
</defs>
</svg>
a.svg (trying to reference b.svg):
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1">
<defs>
<circle id="a" r="6" stroke="black" fill="yellow" />
<marker id="a_end"
orient="auto"
style="overflow:visible">
<use xlink:href="#a" />
</marker>
<marker id="b_end"
orient="auto"
style="overflow:visible">
<use xlink:href="b.svg#b" />
</marker>
</defs>
<path d="m 10,10 20,20" style="marker-end:url(#a_end)" stroke="black" />
<path d="m 40,10 20,20" style="marker-end:url(#b_end)" stroke="black" />
<path d="m 70,10 20,20" style="marker-end:url(b.svg#b_end)" stroke="black" />
</svg>
As you can see, I referenced the marker for the first line via an internal id (actually two since the marker has a reference, too). This works fine.
I used an internal marker definition with an external path for the second line. It doesn't work. (The line is diplayed, the marker isn't.)
I used an external marker in the third line. It doesn't work either.
The problem may be that the external content isn't in the hosting DOM - at least not when the reference in the style is resolved.
OK, but what can I do about it? How can I reference external elements for markers in SVG?
I think I can answer my original question based on my experiments and on the comment left by Robert.
The code I wrote should work in SVG and it does work with Opera and Firefox. Plus it works when generating a PDF with Apache FOP which was the key point for me.
The only problem is that external referencing doesn't work in IE, Chrome and Safari. I'm not sure when external references from style definitions were implemented in Firefox: it doesn't work in 7.0 but it works in 11.
Related
Do you know why my simple shapes are being converted to paths (which are more complex shapes)?
I have an example of my file versus what the SVG generator creates: codepen
My svg:
<svg id="cancel" viewBox="0 0 25 25" fill="none" stroke="#000" stroke-width="0.8px" stroke-linecap="round">
<title>Cancel</title>
<circle cx="12.5" cy="12.53" r="12.08" vector-effect="non-scaling-stroke"/>
<line x1="17.86" y1="7.86" x2="7.86" y2="17.86" vector-effect="non-scaling-stroke"/>
<line x1="17.84" y1="17.84" x2="7.88" y2="7.88" vector-effect="non-scaling-stroke"/>
</svg>
Generator svg:
<svg viewBox="0 0 25 25">
<title>cancel</title>
<path fill="none" stroke="#000" stroke-width="0.8" stroke-miterlimit="4" stroke-linecap="round" stroke-linejoin="miter" d="M24.58 12.53c0 6.672-5.408 12.080-12.080 12.080s-12.080-5.408-12.080-12.080c0-6.672 5.408-12.080 12.080-12.080s12.080 5.408 12.080 12.080z"></path>
<path fill="none" stroke="#000" stroke-width="0.8" stroke-miterlimit="4" stroke-linecap="round" stroke-linejoin="miter" d="M17.86 7.86l-10 10"></path>
<path fill="none" stroke="#000" stroke-width="0.8" stroke-miterlimit="4" stroke-linecap="round" stroke-linejoin="miter" d="M17.84 17.84l-9.96-9.96"></path>
</svg>
Primitives are rewritten as paths, presentation attributes have been moved from the <svg> element to the contained <paths>s. An id attribute has been removed. If the web services you are using have reasons for doing that, they do not publicly share them (or at least I haven't found doc on that). Giving opinions on the why must remain outside the scope of this answer.
The attribute vector-effect="non-scaling-stroke" you used is not part of the SVG 1.1 specification, but only SVG 1.2 Tiny and the SVG 2 draft. Internet Explorer does not support this attribute, so removing it improves browser compatibility.
Adding stroke-miterlimit="4" is objectivly useless, as that is the default value.
There is a library svgo that could rewrite the code in the way you see it (almost, excluding the default value issue), but I cannot tell for sure if the services are using that.
I have an html page with two svg use elements:
The first references inline svg.
The second references an external svg file (same code).
I am trying to figure out why the second example does not show the svg paths even though the SVG code that is inline is exactly the same as the SVG code in the linked file.
https://s3.amazonaws.com/jagger/svg/index.html
<svg class="svg-inline">
<use xlink:href="#test" />
</svg>
<svg class="svg-external">
<use xlink:href="sprite.svg#test" />
</svg>
<svg width="0" height="0">
<symbol id="test" viewBox="0 0 600 600">
<title>Test Icon</title>
<rect id="svg_2" height="214.39594" width="481.62782" y="10" x="10" stroke-width="5" stroke="#000000" fill="none"/>
<line fill="none" stroke="#000000" stroke-width="5" x1="10" y1="10" x2="400" y2="400" id="svg_1"/>
</symbol>
</svg>
using of external sprite is working in the latest version of all modern browsers
still problem in actual IE and older browsers versions
see MDN <use> browser_compatibility table
for IE (and older browsers) just use polyfill svg4everybody
Really basic SVG question. I have read
SVG sprite in external file
and it works fine for me to add a svg graphic, but I can't get it to work with defs. First the file 'defs.svg':
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >
<defs id='patternDefs'>
<pattern id="pattern1"
x="2" y="2"
width="5" height="5"
patternUnits="userSpaceOnUse" >
<circle cx="2" cy="2" r="2" class="blue" />
</pattern>
</defs>
</svg>
and then the svg in a separate file:
<svg>
<use xlink:href="defs.svg#patternDefs"></use>
<circle cx="15" cy="15" r="50" stroke-width="2" stroke="red" fill="url(#pattern1)" />
</svg>
I am looking to get the fill="url(#pattern1)" part to work, as that is what is referencing the def in the external file.
Sorry if you think this has been answered elsewhere but I've read a ton of stuff and thought that if I could get the sprite version to work then why not a defs version. (I am very new to svg)
xlink:href="defs.svg#patternDefs" should be xlink:href="defs.svg#pattern1"
On top of that <use> has to point to something to be rendered, not a pattern. If you want to fill a circle with a pattern just set the circle's fill to the pattern. E.g.
<svg>
<circle cx="80" cy="80" r="50" stroke-width="2" stroke="red" fill="url(defs.svg#pattern1)" />
</svg>
Note that external fills are not widely supported, although they do work on Firefox for instance.
We have icons created in Adobe Illustrator that need to be sent over a network connection and rendered at 72x72px, and we have control on both ends over how they're rendered. We need a small file size. They're fairly simple icons, but in SVG form, they're anywhere from 32KB to 6KB, and when I render them as 72x72px PNGs, they end up being smaller, around 3KB!
How could this be possible? I thought SVGs were supposed to be much smaller since they're just vector representations. Is there some optimization I can do to make the SVGs smaller?
Edit: Here's an example. This is a Wikimedia SVG, not one of our icons, since I can't post our actual icons online. But it's similar to some of the icons we have and has the same problem:
PNG version, 2KB:
SVG version
is 30KB. Comes out to 5KB zipped.
To prove that SVGs can be small, here is a handcrafted version of your file that is only 922 bytes uncompressed. It could be made smaller :)
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="500" height="500">
<g transform="translate(250,250)">
<g id="q">
<g id="h">
<rect x="198" y="-5" width="44" height="10"/>
<circle cx="220" cy="0" r="5" transform="rotate(6)"/>
<circle cx="220" cy="0" r="5" transform="rotate(12)"/>
<circle cx="220" cy="0" r="5" transform="rotate(18)"/>
<circle cx="220" cy="0" r="5" transform="rotate(24)"/>
</g>
<use xlink:href="#h" transform="rotate(30)"/>
<use xlink:href="#h" transform="rotate(60)"/>
</g>
<use xlink:href="#q" transform="rotate(90)"/>
<use xlink:href="#q" transform="rotate(180)"/>
<use xlink:href="#q" transform="rotate(270)"/>
<circle r="22"/>
<rect y="-5" width="150" height="14" transform="rotate(-15)"/>
<rect x="-4" width="8" height="220"/>
</g>
</svg>
Link : http://jsfiddle.net/xkpeD/
or just
<svg width="300px" height="300px" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<circle cx="50" cy="50" r="20" fill="pink" fill-opacity="0.5"/>
<use xlink:href="#:example" x="20" y="20"/>
<defs>
<circle id=":example" cx="50" cy="50" r="20" fill="pink" fill-opacity="0.5"/>
</defs>
</svg>
It displays ok in all browsers (IE9, Chrome, Firefox, Safari 5.1), but in new Safari 6 only 1 circle is rendered. Seems that all <use> tags doesn't rendered in Safari 6.
Any help please?
I had the same issue, OP. I solved it by doing 2 steps
Separated the <use> and the <defs> into 2 different <svg>'s (not sure if this step is necessary, also had to do it for other reasons). Side note, if an <svg> only has <defs>, you can use style="display: none;" to make it not take space in the layout.
Moved the <svg> containing the <defs> ABOVE the <svg> containing the <use> in my HTML. This step is crucial.
Hope this helps. Necessary and working for Safari Version 7.1.2 as of today, 12/19/14
sam.kozin's answer worked for me. Just so that this answer actually has visibility.
Replace <use ... /> with <use ...></use>
So:
<svg width="300px" height="300px" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<circle cx="50" cy="50" r="20" fill="pink" fill-opacity="0.5"/>
<use xlink:href="#:example" x="20" y="20"></use>
<defs>
<circle id=":example" cx="50" cy="50" r="20" fill="pink" fill-opacity="0.5"/>
</defs>
</svg>
I was using <use href=""> that was rendering without issues in Firefox / Chrome, but not in Safari. So I had to change to <use xlink:href=""> and this fixed my rendering issues in Safari.
Check if you are using correct http content-type header and serving your document as valid XML. More info in this similiar question.