Safari 6 svg tag use issues - 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.

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.

Transforming <svg> element in SVG behaves differently in Chrome, Firefox

I have a fairly simple SVG which I've converted into a SSCCE. Here's the SVG (and a fiddle you can see for yourself):
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1">
<rect id="leader" width="100%" height="100%" stroke="red" fill="none" />
<svg id="left" x="5%" y="5%" width="40%" height="85%">
<rect width="100%" height="100%" fill="blue"/>
</svg>
<svg id="left" x="55%" y="5%" width="40%" height="85%" transform="scale(.5)">
<rect width="100%" height="100%" fill="red"/>
</svg>
</svg>
I'm expecting a large, empty red rectangle containing two smaller rectangles: one blue one which takes up quite a bit of space and another one (red) which is half the size of the blue one. There is a translation which occurs as well, but that's not terribly important for this question.
In Firefox, I get the expected image, which is this:
However, when I view the same image in Chrome (or Safari), it seems to be ignoring my transformation, and the two rectangles are both the same size:
Is there something wrong with my SVG, is this a bug in either of these browsers, or is this an unsupported part of SVG in Chrome/Safari? There is an old bug from early 2017 which is reported to be fixed, so I'm thinking that I'm missing something about the way SVG transforms are supposed to work.
The transform attribute for an <svg> element has only been introduced for SVG 2. For now it is not supported in all browsers. (Setting a version attribute on the root element has no effect.)
You can achieve the same effect if you wrap the <svg> element with a <g> and define the transformation there. The percentage values for the positioning will still be relative to the nearest parent element establishing a viewport, which is the outer <svg>.
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<rect id="leader" width="100%" height="100%" stroke="red" fill="none" />
<svg id="left" x="5%" y="5%" width="40%" height="85%">
<rect width="100%" height="100%" fill="blue"/>
</svg>
<g transform="scale(.5)">
<svg id="left" x="55%" y="5%" width="40%" height="85%">
<rect width="100%" height="100%" fill="red"/>
</svg>
</g>
</svg>
The bug you referenced, btw, does not apply. It's not easy to see at first glance, but the attached test case shows this refers to setting a transformation on a <g> element via script.

SVG use linked from an external source not showing svg data

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

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.

How could simple SVGs take more bytes than PNGs?

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>

Resources