user3164101
user3164101

Reputation: 21

Empty xml element confusion - HTML

Here are two html snippets:

<html>
  <head>
    <title>foo</title>
    <style type="text/css"></style>
  </head>
  <body>
    bar
  </body>
</html>

<html>
  <head>
    <title>foo</title>
    <style type="text/css"/>
  </head>
  <body>
    bar
  </body>
</html>

Try rendering in Firefox, Chrome or IE - the two snippets render differently! But I thought both versions of an empty element are the same? (The style element)

Upvotes: 2

Views: 68

Answers (3)

Jukka K. Korpela
Jukka K. Korpela

Reputation: 201568

When served as text/html, they are parsed with an HTML parser, which treats <style type="text/css"/> just as a typo for <style type="text/css"> (i.e., ignores the slash before the tag close). This makes the rest of the document part of the style element and thus ignored. – This is the reason why XHTML 1.0, appendix C, recommends that the “self-closing” syntax (aka. “minimized syntax”) be used only for elements with EMPTY declared content.

When served as genuine XHTML, with an XML content type, they are processed identically, by XML rules. However, in the absence of an xmlns attribute, they are treated just as generic XML without a style, so in practice the browsers just display the XML code as-is.

Upvotes: 0

Robert Siemer
Robert Siemer

Reputation: 34697

In XML it would be valid, but you call your snippets HTML, where it is not. – In HTML5 for example (serialized as HTML, not as XML), you didn’t close the style element yet (slash is ignored).

Upvotes: 1

MarcinJuraszek
MarcinJuraszek

Reputation: 125620

According to HTML Specification end tag is required for STYLE element.

14.2.3 Header style information: the STYLE element

Start tag: required, End tag: required

So self-closed version of style is not correct HTML document part.

Upvotes: 1

Related Questions