It took me a lot of time to figure out an error while playing around with SVG symbols and I still have the question "why?".
I tried to include and SVG symbol using xlink:href like this:
<svg class="icon">
<use xlink:href="../node_modules/icons/icons.svg#my_icon_24px"></use>
</svg>
but the icon did not render at all. Althought the file was linked correctly. (I could open it when I clicked on the link to the SVG file in the source code.)
But when I copy the file icons.svg into the same dir my html file is in and then include it like this:
<svg class="icon">
<use xlink:href="icons.svg#my_icon_24px"></use>
</svg>
it works perfect. Are there any known issues with relative filepaths regarding xlink:href? I guess the .. was the problem.
The SVG didn't render in Firefox neither in Chrome. But why?
UPDATE
Okay seems to be a problem of opening a local HTML file without a webserver. I opened the HTML file in the browser with file://. When I open it over a webserver both paths work perfect.
At least Google Chrome gives a hint:
Unsafe attempt to load URL file:///....../node_modules/icons/icons.svg#my-icon from frame with URL file:///....../web/index.html. 'file:' URLs are treated as unique security origins.
Related
I'm using the foreignObject tag inside an SVG, which is working on Chrome, however, it isn't working on Safari. I have gone through about 20 solutions, but the issue persists.
One of the solutions was a user mistyping foreignObject, they instead wrote it as foreignobject (lowercase "O").
In my code, foreignObject is spelled with the correct casing, however, in Safari's devtools, I notice that the tag is spelled foreignobject!
(Below, Safari devtools screenshot, and my code screenshot)
I'm sure this is what is causing the issue I'm having, but it makes no sense, since I have it written correctly in the code, but it's misread when Safari builds the site.
Is there any precedence for something like this? What can I do to change this misreading of my element tag name?
The problem is that you are setting the xmlns attribute of your <foreignObject> to the XHTML name-space. Safari will thus consider it an HTML element when the SVG document is served as a standalone (if it was served inline in an HTML document, then they'd discard it.
This attribute must be set on the top HTML element, that is on the <foreignObject> content:
<foreignObject
requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"
width="300"
height="100"
>
<html-elem xmlns="http://www.w3.org/1999/xhtml">...
(Note that when served inline in an HTML document, Safari's web-dev tools will also show this element lower-cased, even though it will work correctly).
According to https://www.w3.org/TR/SVG/styling.html#StylingWithCSS, in order to configure svg to use styles defined in an external stylesheet, you can import them as follows:
<svg xmlns="http://www.w3.org/2000/svg">
<style>
#import url(mystyles.css);
</style>
<rect .../>
</svg>
This works fine in Chrome when the files exist in their original format, whether served from a web server or read from the local filesystem, but after building with webpack the resulting app's svg elements have no styling, so clearly the stylesheet isn't getting found.
All of the searches I've done turn up lots of info on what is more or less the reverse operation - referencing svg files from within css (e.g., to specify an svg image as the background for an element).
Does anyone know of a webpack plugin and/or configuration specifically for this case?
I have this piece of SVG picture:
<text
xml:space="preserve"
style="font-size:18px;font-style:normal;font-weight:normal;line-height:125%;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;
font-family:'Arial;Sans';
-inkscape-font-specification:'Arial;Sans';font-stretch:normal;font-variant:normal"
x="11.764346"
y="192.01521"
id="text3607"
sodipodi:linespacing="125%"><tspan
sodipodi:role="line"
id="tspan3609"
x="11.764346"
y="192.01521">PCI-E</tspan></text>
which I edited on linux using inkscape. It used font "sans" which is not available on windows. I would like to specify a font-family that contains fonts available on all major operating systems, but whatever syntax I use it doesn't work. So far I tried:
font-family:'Arial' - works on windows
font-family:'Sans' - works on linux
font-family:'Sans,Arial' - broken
font-family:'Sans;Arial' - broken
What is correct syntax for this to work? I was rendering the picture in IE and Firefox, both seems to have same problems.
in order to change font-family in svg you should first import font in defs in svg like this:
<defs>
<style type="text/css">#import url('https://fonts.googleapis.com/css?family=Lato|Open+Sans|Oswald|Raleway|Roboto|Indie+Flower|Gamja+Flower');</style>
</defs>
then you can change font-family either using an inline style or by javascript
<text xmlns="http://www.w3.org/2000/svg" style="direction:rtl ;font-family:Gamja Flower" id="nametxt" class="text" transform="matrix(1 0 0 1 390 88.44)">text</text>
and for javascript:
svgTextNode.style.fontFamily=FontFamily
It seems that problem was I had it wrapped in quotes. Correct syntax (or at least it works to me):
font-family:Sans,Arial; (no quotes)
In case if we need to declare global style we could use the following syntax:
<svg width="100" height="100"
xmlns="http://www.w3.org/2000/svg">
<style>
text {
font-family:Roboto-Regular,Roboto;
}
</style>
...
</svg>
For security reason, embedded svg images have to be standalone images. You will need to make the svg 'standalone' by embedding all external assets (in our case is the font definition) into it.
To embedded the font inside svg file, follow these steps:
1. Generate the embedded font url as base64
Download the font file you want to use under .ttf extension.
We will need to have the embedded as data URI scheme.
Upload this font file to any online Data URI converter,
I'm using dopiaza.org data URI generator for simplicity (or you can use any File to Base64 converter tool, as long as you follow the same data-uri generated pattern).
Upload the font file to the converter. Ensure Use base64 encoding is checked. Since we're embedding font, so choose Explicitly specify mime type
and put the mime type is application/font-woff
Hit the Generate Data URI and let the tool do the job, it should present you the following data URI format:
data:<mime-type>;base64,<the_encoded_font_as_base64_content>
In our case using font as Mime-Type, it will be:
data:application/font-woff;base64,AAEAAAATAQAABAAwR0RFRv4pBjw....
2. Declare the embedded font inside our SVG file
Edit our SVG file that's using the font. Declare #font-face inside the tag. Put the generated data-uri URL above in the src: url("<generated_data_uri>")
<svg>
<defs>
<style>
#font-face {
font-family: Inter;
src: url("data:application/font-woff;base64,AAEAAAATAQAABAAwR0RFRv4pBjw....")
}
</style>
</defs>
<!-- The rest of your SVG content goes here -->
</svg>
In my case I was converting an SVG as a base64.
Using font-family="Arial, sans-serif;" was not working, but when I removed ";" semi-colon from last portion, voila! it worked.
I have a pie-chart SVG generated by d3. If
I save the content in a file with .svg extension
none of the browsers are able to display it.
If I save the same content in a file with
extension .html, it gets displayed fine.
Why ?
The SVG content is here http://pastebin.com/9QPKT5ju
To add to more detail, there is no web server involved,
just saving the content in a file with .html extension
& loading the file in browser makes it display correctly,
while changing the extension to .svg & reloading it in
browser makes it disappear.
The reason I am doing this, is that I am generating the
svg using Node.js on server side & want to embed the
generated svg in a html page & a PDF file. For the above
experiment I just wanted to see if generated svg displays
properly in browser as it would be loaded dynamically in
a fixed HTML template.
You need to add the following to the base svg element:
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
Your color definitions also need to be like this:
<radialGradient id="grad-0" cx="0" cy="0" r="190"><stop offset="0" stop-color="#ace98c"></stop><stop offset="0.3" stop-color="#ace98c"></stop><stop offset="1" stop-color="#70c046"></stop></radialGradient>
instead of radialgradient (i.e. the camel casing is important.)
I created a star using Illustrator which I saved as an SVG file. Later in my HTML coding I called that SVG like this:
<object type="image/svg+xml" data="images/star.svg" width="100%" height="100%"></object>
But I am unable to see the image in any browsers. I have tried Safari version 5.0 and Firefox 5.0 versions to preview my html.
Is it possible to get an SVG file to display in a browser?
Yes, it is possible. You can use the object tag, or you can use CSS background-image, or with recent browsers you can embed it directly inline using the<svg> tag. Read this:
http://www.alistapart.com/articles/using-svg-for-flexible-scalable-and-fun-backgrounds-part-ii
The SVG file format was originally designed for browsers if i am not wrong.
One crude solution to your problem is by using this pre-made SVG sample code:
<svg width="500" height="400" xmlns="http://www.w3.org/2000/svg">
<g>
<title>Layer 1</title>
<path id="svg_7" d="m64,143c1,0 95,-44 105,-40c10,4 27,59 27,59" fill-opacity="null" stroke-opacity="null" stroke-width="2" stroke="#995757" fill="#FF8787"/>
</g>
</svg>
Now open up the SVG file you created in Illustrator with Notepad and find something called the ''d'' attribute. Copy the values of the ''d'' attribute. For example in the sample code above you would be copying this: "m64,143c1,0 95,-44 105,-40c10,4 27,59 27,59". Take extra care to include the "" tags while copying.
Now create a new Notepad file and paste the sample code i gave you above. Then replace the ''d'' value with the value you copied from your Illustrator SVG file. Make sure you set this values in the sample code with the ones you prefer. **fill-opacity="null" stroke-opacity="null" stroke-width="2" stroke="#995757" fill="#FF8787"**.
Also make sure you change the width="500" and height="400" values with ones that correspond to the paper size you are using in Illustrator.
Then save the file with an .svg extension at the end of the filename and choose to save as type ''All Files'' instead of ''Text Document''.
Pretty crude solution but i think illustrator creates SVG files that are not exactly browser friendly.