Hacker News new | past | comments | ask | show | jobs | submit login
The minimal code required in a HTML5 page (chetankjain.net)
57 points by cjain on Feb 19, 2012 | hide | past | favorite | 17 comments



The fact that it works is completely irrelevant. It has always worked, in about all versions of HTML/XHTML, in that type of simple cases.

The important fact is that omitting the html, body, head tags is valid markup. Whether browsers conform to this is another matter. [1] suggests there are some issues with that.

[1] http://stackoverflow.com/questions/5641997/is-it-necessary-t...


The only browser to still have an issue with this should be MSIE, and that should be resolved with IE10 which will feature an HTML5 parser[0]: http://blogs.msdn.com/b/ie/archive/2011/07/06/html5-parsing-...

All other major browsers have already switched to an HTML5 parser

* Firefox since Firefox 4: http://hacks.mozilla.org/2010/05/firefox-4-the-html5-parser-...

* Webkit since ~early 2011: http://www.webkit.org/blog/1273/the-html5-parsing-algorithm/ I don't have the exact dates for stable versions of Chrome and Safari

* Opera since December 2011: http://www.opera.com/docs/changelogs/windows/1160/

[0] "html5 parser" is used as a shortcut for "parser implementing the HTML5 parsing algorithm"


Exactly. Combined with the `Link` HTTP header, it’s even possible to apply CSS to a completely empty HTML document! http://mathiasbynens.be/notes/css-without-html


Note that `<title>` is only optional in a few edge cases. This post has a more in-depth explanation: http://mathiasbynens.be/notes/minimal-html


Just so we are clear: Never actually do this.

The point of the post is that the syntax for writing html is getting better (just using 'html' as a doctype is great). But in terms of application, there is none.

If you want to write a web site/service and you want to support more users than just your friends, you have to do things right to support older browsers (and IE) and this means things like including all of the tags that FF/Chrome are automatically adding.


I would like to point out that the % of older browser usage is going down significantly. We are talking < 35% of web population and shrinking by the day ... http://gs.statcounter.com/#browser-ww-monthly-201111-201201


Here's a list of smallest possible syntactically valid files of different types https://github.com/mathiasbynens/small


Also see http://mathiasbynens.be/notes/minimal-html for a list of the smallest possible (X)HTML files per version.


Before you ask what the minimal code for an HTML5 page is, you have to decide what constraints you are putting. Are you talking about the minimal valid, independent web page? The minimal code that your browser will parse as HTML and generate a DOM for? The minimal code that is allowed in an <iframe srcdoc>?

If you're talking about the minimal valid code for an independent web page, it is:

    <!DOCTYPE html><title></title>
The title element is required, and is not automatically inserted if the tags are missing the way <html>, <head>, and <body> are.

If you're talking about the minimal thing the browser will parse, you can provide an empty document (with Content-type: text/html), and the browser will treat it as if it's an HTML document, generating the appropriate elements automatically. It will parse it in quirks mode, but if you don't care about validity, and are talking about an empty document, that doesn't matter much.

If you're talking about what you can embed in <iframe srcdoc>, again, an empty document is acceptable. In that context, the doctype and title are both optional.


Note that `<title>` is optional in a few edge cases. This post has a more in-depth explanation: http://mathiasbynens.be/notes/minimal-html


The author mentions that you can leave the type attribute off of <script> tags. You can do the same with <style> tags as well, since it defaults to CSS.


Wouldn't <html/> be the smallest?


The shortest HTML4 (and earlier) document is

  <title//x
This uses the never-implemented but legal shorttag syntax for the required title element, and the "x" character serves as the body content.


I would have thought that

<!DOCTYPE html>0

would be the smallest.


Smallest what? There's no DOCTYPE.


It's worth adding that the DOCTYPE is what specifies what HTML rendering engine IE 7 and 8 use. A document without the DOCTYPE directive will render in quirks mode, and nobody wants that.


I think I recall Paul Irish once saying even closing tags of certain types (<li>, <p> etc) were also unnecessary. The start of the next tag should also signal the close of the previous one (unless you want to nest stuff) -- so whether the </p> or </li> tag or whatever is there or not makes no difference.

HTML parsers cope quite well with that (and have done for a while) but I don't do it. Although I am guilty of not bothering with the type declaration in scripts that are JavaScript <script src="..." />

I know I'm just plain lazy and wrong.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: