SVG use linked from an external source not showing svg data - svg

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

Related

Why does this nested SVG not work in Firefox?

I'm creating a contact sheet that displays all the icons in a library. I've built it using nested SVGs. Here's a minimal version:
<svg xmlns="http://www.w3.org/2000/svg" version="1.1"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:svgjs="http://svgjs.dev/svgjs" viewBox="0 0 200 200">
<svg id="sheet_core" width="200" height="200" x="0" y="0">
<svg width="200" height="200" x="0" y="0">
<symbol id="piece" viewBox="-2.5 -2.5 105 105">
<circle r="50" cx="50" cy="50" data-playerfill="true" fill="#ffffff" stroke-width="5" stroke="#000000"></circle>
</symbol>
<use href="#piece" x="25" y="25" transform="matrix(0.75,0,0,0.75,6.25,6.25)"></use>
<text x="0" y="192.1015625" svgjs:data="{"leading":"1.3"}">piece</text>
</svg>
</svg>
</svg>
This works fine in Chrome and Opera, but does not work in Firefox.
You should see this: https://ibb.co/VT8SSSd.
But in Firefox, I get this: https://ibb.co/5L7PP22.
If I inspect the code, the <symbol>s are greyed out, suggesting they are not being referenced, when they very clearly are right afterwards. I also tried moving all the symbols into a global <defs>, but that didn't work either. Any ideas how to tweak this so it will work in Firefox too?
These SVG browser differences are maddening, I've got to say.
Once I loaded the SVG on my website so I could test in some different environments, it suddenly started working. My suspicion is there's some weird CSPF happening, perhaps only on my end. But the SVG does not appear to be the fundamental problem.
As you were.

How to set SVG fill using pattern in a different SVG file [duplicate]

The following attempt to make a rectangle with a pattern fill doesn't seem to work in Safari 6.1, Firefox 30, or Chrome 36, even though the W3 spec seems to say that a I can use a non-local IRI reference, including a relative one, like fill="url(localURL.svg#MyId)".
test.html
<html>
<head>
<style>
.patterned { fill: url("patterns.svg#polkadot");
stroke: lime; stroke-width: 5px}
</style>
</head>
<body>
<svg width="500" height="500">
<rect class="patterned" height="27" width="58">
</svg>
</body>
</html>
patterns.svg
<svg xml:space="preserve" width="225" height="110" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<pattern id="polkadot" patternunits="userSpaceOnUse" x="0" y="0" width="20" height="20">
<circle r="10" cx=12 cy=10 fill="purple">
</pattern>
</defs>
</svg>
Safari and Chrome show a black-filled green-outlined rectangle. Firefox shows an empty or white-filled green-outlined rectangle. None of them show the pattern of purple circles.
I'm trying this approach because I couldn't get an SVG fill pattern to work on Safari in the Backbone+JQuery+D3 project I'm working on using the most common method, an inline defs with fill="url(#MyId)". I couldn't get that approach to fail as a simple test case -- I thought I had, but that turned out to be a different Safari bug with an obvious workaround. At least that approach worked in some browsers.
You've a load of syntax errors in your patterns.svg file. Missing " characters round attribute values, an unclosed circle element, patternunits instead of patternUnits.
SVG standalone must be valid XML, it's not as forgiving as html and it's case sensitive on attribute names too. If you loaded the patterns.svg file directly, browsers would tell you all these things.
With all this fixed (as below) this works in Firefox. I'm not sure Chrome/Webkit have implemented this yet.
<svg xml:space="preserve" width="225" height="110" xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<pattern id="polkadot" patternUnits="userSpaceOnUse" x="0" y="0" width="20" height="20">
<circle r="10" cx="12" cy="10" fill="purple"/>
</pattern>
</defs>
</svg>

SVG fills in external file

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.

Safari 6 svg tag use issues

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.

Externally defined markers don't appear in SVG

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.

Resources