Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

svg file not displayed by web browser

Tags:

html

svg

I have a graphics.svg file having following code:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="400" height="110">
  <rect width="400" height="110" style="fill:rgb(0,0,255);" />
</svg>

If I open that file from web-browser(Firefox, Chromium), the vector image is not displayed. Instead, the file is displayed in XML format:

This XML file does not appear to have any style information associated with it. The document tree is shown below.

<svg width="400" height="110">
    <rect width="400" height="110" style="fill:rgb(0,0,255);"/>
</svg>

Is this because the svg file should be embedded in html file to be displayed properly?

like image 383
Barun Avatar asked Jun 18 '14 12:06

Barun


People also ask

Why are my SVG images not showing?

Like the <img> method described above, inserting SVGs using CSS background images means that the SVG can't be manipulated with JavaScript, and is also subject to the same CSS limitations. If your SVGs aren't showing up at all, it might be because your server isn't set up properly.

How do I view SVG files in my browser?

From Chrome and Edge to Safari and Firefox, all the major browsers allow you to open SVG files these days — whether you're on a Mac or Windows. Just launch your browser and click on File > Open to choose the file you want to view. It'll then be displayed in your browser.

Can browsers display SVG?

In order to view SVG files, you need a viewer or browser that supports Scalable Vector Graphics. Some browsers, such as Mozilla Firefox, have built-in support for SVG files. Note: SVGZ graphics that you create in the ODS HTML5 destination can be viewed only with the Google Chrome or Opera web browsers.

Why is SVG not showing up in Chrome?

Chrome browser doesn't support SVG format with <img src=””> tag anymore. Peoples are facing issues that they aren't able to see their images in Chrome browser because the images are in SVG format. So I found a solution to display SVG image in Chrome browser with <object> tag.


2 Answers

No, it is because you have failed to specify the namespace for the svg element with

xmlns="http://www.w3.org/2000/svg"

See the examples in the spec.

like image 179
Quentin Avatar answered Sep 28 '22 03:09

Quentin


You are missing the namespace declaration.

You need to add xmlns="http://www.w3.org/2000/svg" as an attribute of the root <svg> element and while you're there you might as well add xmlns:xlink="http://www.w3.org/1999/xlink" too.

There are some more complete authoring guidelines here which you might want to peruse.

like image 28
Robert Longson Avatar answered Sep 28 '22 03:09

Robert Longson