Do we still need end slashes in HTML5?

2020-01-24 07:03发布

In HTML5, do we still need the end slash like in XHTML?

<img src="some_image.png" />

validator.w3.org didn't complain if I dropped it, not even a warning. But some online documents seem to indicate the end slash is still required for tags such as img, link, meta, br, etc.

标签: html xhtml
5条回答
孤傲高冷的网名
2楼-- · 2020-01-24 07:04

Nope. HTML has never required it, even before HTML5. If you plan to use XHTML with HTML features, then yes, it is necessary.

查看更多
放我归山
3楼-- · 2020-01-24 07:08

According to Start Tags they are optional.

查看更多
爷、活的狠高调
4楼-- · 2020-01-24 07:18

FROM W3C:

Void elements: area, base, br, col, embed, hr, img, input, keygen, link, meta, param, source, track, wbr

"Void elements only have a start tag; end tags must not be specified for void elements."

http://www.w3.org/TR/html5/syntax.html#void-elements

查看更多
叛逆
5楼-- · 2020-01-24 07:23

img tags are Void Elements so they do not need an end tag.

Void elements area, base, br, col, command, embed, hr, img, input, keygen, link, meta, param, source, track, wbr

...

Void elements only have a start tag; end tags must not be specified for void elements.

W3C | WHATWG

That being said it's not strict parsing in HTML5 so it won't do any major harm.

查看更多
Rolldiameter
6楼-- · 2020-01-24 07:31

In HTML 5, the closing slash is optional on void elements such img (I am adding this because the currently accepted answer only says: "end tags must not be specified for void elements", and does not address closing slashes in void elements).

Citing from http://www.w3.org/TR/html5/syntax.html#start-tags (number 6):

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) character. This character has no effect on void elements, but on foreign elements it marks the start tag as self-closing.

查看更多
登录 后发表回答