Html web浏览器未显示svg文件

Html web浏览器未显示svg文件,html,svg,Html,Svg,我有一个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"

我有一个
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>

如果我从web浏览器(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>
此XML文件似乎没有任何与之关联的样式信息。文档树如下所示。

这是因为svg文件应该嵌入到html文件中才能正确显示吗?

否,这是因为您未能使用

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

请参阅。

您缺少名称空间声明

您需要添加
xmlns=”http://www.w3.org/2000/svg“
作为根
元素的一个属性,在这里您还可以添加
xmlns:xlink=”http://www.w3.org/1999/xlink“
也是


您可能需要仔细阅读一些更完整的创作指南。

为什么要在文件中没有任何xlink内容的情况下设置xlink名称空间?因为大多数SVG都包含xlink,一旦添加了需要的内容,您就会再次遇到此问题。