`?-6ren"> `?-在 HTML5 中使用 SVG 时:是 XML 声明 SVG 需要 作为图像通过 或 作为 CSS background-image是吗? 这与“Are SVG parameters such as-6ren">
gpt4 book ai didi

xml - HTML5 中的 SVG – 什么时候需要 XML 声明 ``?

转载 作者:技术小花猫 更新时间:2023-10-29 12:15:06 26 4
gpt4 key购买 nike

在 HTML5 中使用 SVG 时:是 XML 声明 <?xml version="1.0" encoding="UTF-8"?> SVG 需要

  • 作为图像通过 <img>
  • 作为 CSS background-image是吗?

这与“Are SVG parameters such as 'xmlns' and 'version' needed ”。两个答案和 MDN Namespace crash course 对命名空间问题进行了必要的澄清。 .

但是 SVG 1.1 没有包含关于 XML 声明的必要性或何时可以省略的声明?

没有声明的例子:

<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200" viewBox="0 0 200 200">
<circle id="circle--red" cx="30" cy="30" r="30" fill="#f00"/>
</svg>

2016 年 7 月 4 日更新:澄清问题是关于 XML 声明。谢谢@Martin Honnen!2017 年 10 月 24 日更新:更改为 “UTF-8“ uppercase和 SVGO 优化的属性顺序。

最佳答案

对于 HTML5,正确的 DOCTYPE declaration

<!DOCTYPE html> 

需要给浏览器指定全标准模式。

你展示了什么,

<?xml version="1.0" encoding="utf-8"?>

是一个XML declaration .是optional for XML 1.0required for XML 1.1 , 但是

  • XML 1.1 并未得到广泛使用。
  • version="1.0"encoding="utf-8" 无论如何都是默认值。

当您希望指定不同的编码时,请在 HTML5 中使用 XML 声明,尤其是当文件可能不仅被浏览器而且还被 XML 处理器使用时。

有关详细信息,请参阅 HTML5: A vocabulary and associated APIs for HTML and XHTML

关于内部 SVG 的注意事项(感谢 @Quentin):HTML5 文档中嵌入的 SVG 不应有独立的 XML 声明。在格式良好的 XML 中只允许一个 XML 声明,并且它必须在顶部, 如果有的话。参见 this answer有关 XML 声明放置要求的更多详细信息。

关于外部 SVG 的注意事项(感谢 @Kaiido):通过 HTML5 img 或 CSS background-images 引用的 SVG 必须有自己的XML 声明并应使用以下 DOCTYPE 声明:

<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">

关于外部 SVG(更新)的注意事项(感谢,@user314159):

根据 1.3. SVG namespace and DTDScalable Vector Graphics (SVG) 2 W3C Working Draft 09 July 2015 :

A DTD is not provided in this specification, as the use of DTDs for validating documents is known to be problematic. In particular, DTDs do not handle namespaces gracefully and the range of constraints they can express is limited. It is recommended that authors do not include a DOCTYPE declaration in SVG documents.

[强调已添加。]

关于xml - HTML5 中的 SVG – 什么时候需要 XML 声明 `<?xml version="1. 0"encoding="UTF- 8"?>`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38169475/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com