6

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?

Jitendra Vyas
  • 148,487
  • 229
  • 573
  • 852

3 Answers3

9

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.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • I just thougt it would be invalid in HTML5. but if it is a part of specification in HTML 4.01 and HTML 5. why they didn't changed in HTML5. I don't think if is there any benefit of not closing the tag. I just think some of good things of XHTML should have adopted in HTML 5 – Jitendra Vyas Sep 10 '11 at 06:39
  • 5
    Then what's the benefit of requiring a closing `` tag? – BoltClock Sep 10 '11 at 06:41
  • Then is it also ok to not to close html, head, and body? – Jitendra Vyas Sep 10 '11 at 06:44
  • But this code is also valid in w3c validator http://chopapp.com/#42j7l4wt I removed closing tags for head, body and html – Jitendra Vyas Sep 10 '11 at 06:51
  • Oh then that's my mistake. If you need to know whether a tag needs to be closed, refer to the spec. http://www.w3.org/TR/html-markup – BoltClock Sep 10 '11 at 06:54
  • @BoltClock The benefit would be for organization within your text editor (collapsible elements) – Jeremy Dec 31 '13 at 18:07
7

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.

Alohci
  • 78,296
  • 16
  • 112
  • 156
0

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.

ErikE
  • 48,881
  • 23
  • 151
  • 196