51

I am confused. A co-worker turned me on to the possibility that tags ending in />, such as <br /> can still be used in HTML5. I thought that only <br>-style could be used. All of the "talk" across the Internet is about using the latter.

Could someone please explain this to me? This seems very confusing and poorly documented.

And this brings up another question: Is HTML 5 considered to be well-formed XML?

Maroun
  • 94,125
  • 30
  • 188
  • 241
Jack B.
  • 937
  • 2
  • 8
  • 10

5 Answers5

51

No. Counter-examples:

These are valid HTML5 but invalid XHTML5:

  1. Some closing tags can be omitted:

    <p>First
    <p>Second
    

    See: P-end-tag (</p>) is not needed in HTML

  2. script escape magic:

    <script><a></script>
    

    See: What is CDATA in HTML?

  3. Attributes without values (boolean attributes):

    <input type="text" disabled />
    

    See: What is the correct value for the disabled attribute?

  4. Attributes without quotes, e.g.:

    <div data-a=b></div>
    

    See: In XHTML 1.0 Strict do attribute values need to be surrounded with quotes?

  5. Implicit open elements and multiple top level tags.

    Some HTML elements are created implicitly. E.g. html. This allows the HTML to have "multiple top level elements":

    <!doctype html><title>a</title><p>a</p>
    

    See: Is it necessary to write HEAD, BODY and HTML tags?

Valid XHTML that is invalid HTML:

  1. CDATA constructs with invalid tags inside

  2. ENTITY and other exclamation mark constructs, which allow for billion laughs: How does the billion laughs XML DoS attack work?

Valid HTML and XHTML but with different meanings:

  1. HTML has hundreds of named character references (e.g. &pound;, &copy;), XML has only 5 (quot, amp, apos, lt, gt).
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
  • 2
    As a nitpick, HTML5 does not allow multiple top-level elements, only multiple "top-level tags". For example, the tag can be omitted, but the resulting document still has a html element which will be the only top level element. – Remember Monica Oct 28 '22 at 04:53
11

You can markup your page as valid HTML5 and XHTML5: http://www.w3.org/TR/html-polyglot/

Polyglot markup that meets a well defined set of constraints is interpreted as compatible, regardless of whether they are processed as HTML or as XHTML, per the HTML5 specification.

The basic document could look like:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
  <head>
    <title></title>
  </head>
  <body>
  </body>
</html>

Of course you'd have to follow some additional rules (like not to use the noscript element, for example), outlined in the linked working draft.

unor
  • 92,415
  • 26
  • 211
  • 360
11

There is an XML serialization of it, called XHTML5. Basically, you're free to use either HTML5 (HTML serialization) or XHTML5 (XML serialization). The draft spec says HTML5 "is the format suggested for most authors," mainly for the same reasons people recommend text/html for XHTML 1.1.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
3

HTML5 can be written with or without self-closing slashes; it is meant to be backwards-compatible with both HTML 4.01 and XHTML 1.0 code, so that it is easy to convert code into valid HTML5. There is an XML serialization called XHTML5, but for backwards-compatibility purposes with IE browsers, it is not recommended to be used. So technically, HTML5 is not considered to be well-formed XML.

Kevin Ji
  • 10,479
  • 4
  • 40
  • 63
  • 1
    Polyglot is no longer maintained and not good standard (Beware. This specification is no longer in active maintenance and the HTML Working Group does not intend to maintain it further.) – theking2 Oct 22 '19 at 15:15
  • 1
    @theking2 I think you're replying to the wrong answer. – Kevin Ji Oct 22 '19 at 17:58
  • @kevinji no, he is not. polyglot is the only extantr "standard" that specifies valid (as both xml and html5) documents. HTML5 . Furthermore, HTML5 does not, in general, support self-closing tags: i.e. will parse correctly, but will keep the element open in HTML5, and will close it in XML. – Remember Monica Oct 28 '22 at 04:58
3

Under no condition should you expect any html document (regardless of version) to be "well-formed xml"

html != xml.

It is a different spec with different suggestions (I'm purposely avoiding the word "rules" here) on how it should be interpreted.

The HTML 5 spec has enough "do it this way, but it's okay if you don't" wiggle statements that it's a wonder that any of the browsers show the same thing at all.

NotMe
  • 87,343
  • 27
  • 171
  • 245
  • 9
    XHTML compliant pages are supposed to be well-formed by definition. Well-formed XHTML pages were intended to make parsing routines in the Web browsers less complicated. However, HTML and HTML5 do not have to be well-formed although they can be if desired. Of course even pages with an XHTML DOCTYPE might not be well-formed due to sloppy coders so this is probably why the HTML5 spec is recommended instead of XHTML5 since good browsers probably support rendering badly coded pages anyway. – Kmeixner Jan 04 '13 at 20:15