HTML5中的SVG标记可以省略自动关闭标签吗?

前端之家收集整理的这篇文章主要介绍了HTML5中的SVG标记可以省略自动关闭标签吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
从问题 Do I need a “/” at the end of an <img> or <br> tag,etc.?我了解到自动关闭标签,例如< br />和< img />在HTML5中完全是可选的,可以简单地写成< br>和< img>.

但对于与HTML5内联的SVG元素,情况也是如此吗?例如,可以< circle />标签被改为< circle>?

<svg viewBox="0 0 1000 1000">
    <circle cx="500" cy="500" r="500" />
</svg>

如果允许,通常是好的还是坏的做法?

解决方法

以下是我在 W3 SVG and MathML elements in HTML documents documentation中发现的一些规则
  • SVG and MathML elements whose start tags have a single “/” character before the closing “>” character are said to be marked as
    self-closing.
  • SVG and MathML elements must either have a start tag and an end tag,or a start tag that is marked as self-closing,in which case they
    must not have an end tag.
  • SVG and MathML elements whose start tag is marked as self-closing,can’t have any contents.
  • The contents of an SVG or MathML element whose start tag is not marked as self-closing are any elements,character data,comments,and
    CDATA sections that it contains,with the restriction that any
    character data it contains must be normal character data.

我认为第二点强制svg元素具有显式结束标记或自闭标记.

猜你在找的HTML5相关文章