从问题
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.