Html Web 浏览器不显示 svg 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24286079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
svg file not displayed by web browser
提问by Barun
I have a graphics.svg
file having following code:
我有一个graphics.svg
包含以下代码的文件:
<?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:
如果我从网络浏览器(Firefox、Chromium)打开该文件,则不会显示矢量图像。相反,该文件以 XML 格式显示:
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?
这是因为 svg 文件应该嵌入到 html 文件中才能正确显示吗?
回答by Quentin
No, it is because you have failed to specify the namespace for the svg
element with
不,这是因为您没有为svg
元素指定命名空间
xmlns="http://www.w3.org/2000/svg"
请参阅规范中的示例。
回答by Robert Longson
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.
您需要添加xmlns="http://www.w3.org/2000/svg"
作为根<svg>
元素的一个属性,当您在那里时,您也可以添加xmlns:xlink="http://www.w3.org/1999/xlink"
。
There are some more complete authoring guidelines herewhich you might want to peruse.
还有一些更完整的创作指南在这里,你可能想细读。