Writing Less Markup with HTML5

A lot of proponents of XHTML markup (served as text/html) complain about the fact that, in HTML5, you don't have to quote attribute values and you don't have to explicitly close elements like <p>. They also complain that you don't have to end void elements with /> and you can just use >.

Because of that, they say that HTML5 is harder to author than XHTML and that HTML5 encourages sloppy markup. However, this isn't true at all.

Even if it's valid to leave attribute values unquoted, it's not recommended. Even if it's valid to let the parser implicitly close some elements, it's not recommended. These things are only for authors that prefer to do these things *and* understand how attribute values are parsed and how and exactly when open elements are implicitly closed. Normal authors should quote their attribute values and explicitly close their elements.

As far as ending void elements with /> in HTML, that doesn't make authoring any easier. Since the / isn't needed and you have to know what elements are void elements either way (you can't just close any empty element with "/>" in HTML), the / is useless. Same thing goes for xmlns="http://www.w3.org/1999/xhtml" and xml:lang on the HTML element, and "//<![CDATA[" and "]]>" inside <script>. They don't have any effect in HTML, so you don't need them and they therefore don't make authoring any easier. You should only use these things (when allowed) when authoring polyglot documents where it is required that the markup be properly parsed and handled as both HTML and XML. If that's not the case, you should get out of the habit of doing these XML-ish things as you're just wasting your time.

In short, HTML5 isn't harder to author. It's potentially easier to author as you don't have to write as much.

  • For <script>, <style>, and <link>, you don't need to specify the type attribute. You don't need to specify the language attribute for <script> either. Although you can include them in HTML5, authors are expected to omit them. Why write them when their default values are just what you want?
  • For the doctype, all you need to write is "<!DOCTYPE html>". Nothing more is required to put browsers in full standards mode (and IE in HTML5 mode). Nothing more is required for validation purposes. <http://validator.nu/> can check the document just fine with just "<!DOCTYPE html>". With W3C transitional doctypes for example, they might only trigger almost-standards mode. If you just use "<!DOCTYPE html>", you don't have to worry about this stuff.
  • For the charset, you just use "<meta charset="utf-8">. That's all that's needed to tell the browser to treat the page as utf-8 (make sure the source is saved as utf-8 of course). No need to write the longer form if it doesn't help.
  • For symbols like ©, ™, and non-ascii characters like √, you don't have to use HTML entities. You just put the raw character in the markup. It will work just fine.
  • For quoted attribute values that contain ", you can *still* escape " as &quot; just like in XHTML. But, you can also still quote the attribute value with ' instead so you don't have to escape the each ".

So, as a normal author of HTML5 markup, if something is not needed *and* specifying it doesn't help with authoring, you should omit it. For stuff like implicit closing of elements and implicit creating of elements (like head, body and title etc.), leave that to those that know what they're doing and like to toy around with the markup.

Leave a Reply

Your email address will not be published. Required fields are marked *