Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
561 views
in Technique[技术] by (71.8m points)

html - Closing SVG tags, explicit or self closing?

I was validating my code, and got the following error Tag cannot be self-closing. Use an explicit closing tag. in IE for my svg path because it's self closing.

<path d="m53.911,10.023c-1.46-.898-3.195-1.019-4.699-1.019h-3.439c" />

Now I know meta tags in HTML5 don't require the forward dash /, you just close them with >. Does the same thing apply to svg tags? Like so:

<path d="m53.911,10.023c-1.46-.898-3.195-1.019-4.699-1.019h-3.439c" >

Or… Using an explicit closing tag? Like so:

<path d="m53.911,10.023c-1.46-.898-3.195-1.019-4.699-1.019h-3.439c" > </path>

What is the correct way of closing a path?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The answer by Robert Longson is great, but links to a document that is marked as:

This document has been discontinued and is only made available for historical purposes.


I wanted to find some up-to-date specification on this behavior, and here is what I found:

A Self-closing tag is a special form of start tag with a slash immediately before the closing right angle bracket. These indicate that the element is to be closed immediately, and has no content. Where this syntax is permitted and used, the end tag must be omitted. In HTML, the use of this syntax is restricted to void elements and foreign elements. If it is used for other elements, it is treated as a start tag.

Source: W3C HTML5 Reference Editor's Draft

Start tags must have the following format:

  1. Then, if the element is one of the void elements, or if the element is a foreign element, then there may be a single U+002F SOLIDUS character (/). This character has no effect on void elements, but on foreign elements it marks the start tag as self-closing.

Source: HTML Living Standard from WHATWG and HTML: The Living Standard - A technical specification for Web developers

Inline SVG and MathML support

The syntax <foo/> opens and immediately closes the foo element if it is a MathML or SVG element (i.e. not an HTML element).

Source: HTML5 Parser at Mozilla Developer Network


Conclusion: What Robert Longson wrote in his answer is still valid. This warning in IE11 developer tools is wrong. Using the self-closing syntax in HTML5 is valid (but only for void elements, such as <br/>; or foreign elements, which are those from MathML and SVG).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...