Do we still need end slashes in HTML5?

匿名 (未验证) 提交于 2019-12-03 02:11:02

问题:

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.

回答1:

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.



回答2:

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.



回答3:

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



回答4:

According to Start Tags they are optional.



回答5:

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



回答6:

ALL tags must be closed (either by being self closing, or by a separate closing action). Some are self closing in HTML4 (like the <img> tag) while others (like the <p> tag) require a separate closing tags. Remember ALL tags must be closed or the browser (if it is being strict for compliance) will have errors. This means that even tags that have not content between opening and closing, MUST be closed in one way or another. Below I'll examine the <img> tag as it doesn't have any content between opening and closing.

In HTML4 it is self-closing. This doesn't meant it doesn't close, as remember ALL tags must be closed. It just closes itself by its very presence. So in HTML4 the <img> tag is simply: <img>

In XHTML it is NOT self closing. This means that you must provide a separate closing tag like this: <img></img>

Then in HTML5 someone finally got smart and decided to make closing a tag that doesn't have content between opening and closing, a very simple task. Use just one tag, but have a slash before the greater-than sign, like this: <img />

This is also fully backward compatible with HTML4. If you aren't using XHTML (which requires a separate closing tag) and are unsure if your code is using HTML4 or HTML5 standards. Your best bet is to ALWAYS have a closing slash before the greater than sign for tags that have no content between opening and closing. This way it will ALWAYS display properly in EVERY browser.

Here's an example of properly written HTML code that is has a high degree of compatibility with all browsers.

<!DOCTYPE html> <html> <head> <title>test website</title> </head> <body> <p>This is a test.</p> <img src="testpic.jpg" /> </body> </html> 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!