Is it not necessary to close the tag in HTML 5 like HTML?

这一生的挚爱 提交于 2019-12-23 09:47:40

问题


Is it not necessary to close the tag in HTML 5 like HTML? or it's a bug in W3C validator

Why this code is valid in W3C validator

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>title</title>
    <link rel="stylesheet" href="style.css">
    <script src="script.js"></script>
  </head>
  <body>
<p>Some Text
  </body>
</html>

I would be surprised if it's really valid in HTML5. But is there any benefit to keep this behavior valid in HTML5. Do HTML5 creators think that stricter rules of XHTML were not good for Web?


回答1:


That markup is indeed valid. <p> tags don't have to be closed in HTML 4.01 or HTML5. I'm not sure where you got the idea that HTML5 requires everything to be closed like in XHTML.

HTML5 is just regular HTML with extra new features (hence the version jump from 4.01 to 5). It does not in any way derive from XHTML. You can close all of your HTML5 tags so it looks like well-formed XML, but the spec doesn't require you to.




回答2:


Do HTML5 creators think that stricter rules of XHTML were not good for Web?

Pretty much, yes.

Their view is that it just makes creating a web page harder. HTML has been wildly successful because just about anyone can create a working web page without knowing barely any HTML at all. It's a very small learning curve to get started which people can build upon when they're ready.

If you need to know a lot of pedantic rules just to get started, then many people won't bother, and HTML will not be as successful.




回答3:


Leaving the closing tag for a <p> element out is valid in most situations, though there are a few where it isn't. The exact rules at the World Wide Web Consortium are:

A p element’s end tag may be omitted if the p element is immediately followed by an address, article, aside, blockquote, dir, div, dl, fieldset, footer, form, h1, h2, h3, h4, h5, h6, header, hr, menu, nav, ol, p, pre, section, table, or ul element, or if there is no more content in the parent element and the parent element is not an a element.

So, for example, the following is invalid:

<a href="http://example.com><p>This paragraph is unclosed</a>

But this is valid:

<div class="news"><p>Something important happened!</div>

HTML never required the <p> tag to be closed—it was always optional. You may close your HTML tags so it looks like well-formed XHTML, but that isn't necessary. XHTML is more strict than HTML.



来源:https://stackoverflow.com/questions/7369989/is-it-not-necessary-to-close-the-tag-in-html-5-like-html

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