Skip to main content

Minimal required code in HTML5

I've encountered this question repeatedly of late.

"What are the tags required at bare minimum for a html file?"

Earlier there were a bunch of mandatory tags that were required for any html file. At bare minimum, the recommended structure was: (ref: http://www.w3.org/TR/html4/struct/global.html)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
  <HEAD>
    <TITLE>A small HTML</TITLE>
  </HEAD>
  <BODY>
    <P>Small HTML file!</P>
  </BODY>
</HTML>

Yes, using capitals for the tags was the way to go! Those were the days of the purists and strict was the way to be. Now open your notepad and copy the above code, save the file as old.html and launch it in Chrome or Firefox. You will see only one line "Small HTML file!" shown. Now launch the developer tools in Chrome or Inspect Element in Firefox. This can be done by pressing CTRL+SHIFT+I in both the browsers. You will see the html code rendered by the browser now shown in the inspector. The code matches with the above code.

Now lets look at how things stand today. Welcome HTML5!

<!DOCTYPE html>
Smallest HTML file!

Type the above code in notepad and save it as new.html. Yes just two lines, actually the first line is recommended for HTML5, but for basic stuff even that is not necessary. You can even skip that line for this particular exercise. Now save and open the file in your browser. Voila, the text is there, now open the code inspector. You will see the missing tags have been automatically added by the browser. This is what the code inspector should show:

<!DOCTYPE html>
<html>
  <head></head>
  <body>Smallest HTML file!</body>
</html>

Great isn't it. Things have been vastly simplified and made easier for the developer. DOCTYPE's earlier used to cause allergic reactions in me. Now it requires just one word "html". 

There is one more thing that I wish to highlight. I see so many HTML5 pages still showing this code while using JavaScript,

<SCRIPT type="text/javascript" src="....">

This can simply be

<script src="....">

There is no need to specify the type attribute. JavaScript is the default. And yes please use small case where possible. Easier on the eyes and the brain!

There are many more wonderful simplifications done in the HTML5 semantics. And there are entire chapters dedicated to this topic in various HTML5 books. But somehow, I keep encountering the above quite often off late and so the post.

Comments

  1. I tried in Chrome / FF on OS X but the missing tags haven't been automatically added by the browser.

    ReplyDelete
    Replies
    1. pax: If you see the plain text in the browser, then the tags are added. You have to open the Code Inspector to see it, press CTRL+I in your browser to open it. In Chrome there is an Elements tab, you will see the full HTML there.

      Delete
    2. right, I see that now - I sometimes need extra aid in reading black on white text :)

      Delete
    3. Don't worry, I also misinterpreted. I expected the browser to save the file with the minimum tags added. Somehow this black and white text radiates confusion.

      Delete
  2. I also omit quotes in attribute values wherever possible, e.g.

    < img id=myimg width=200 height=100 src=/img/1.png >

    - makes the entire file so much nicer looking. I see some raised brows now, but it works, it's fine with me, and my visitors don't care.

    ReplyDelete
    Replies
    1. See http://mathiasbynens.be/notes/unquoted-attribute-values for more information on when attribute values can remain unquoted. There’s a tool, too: http://mothereff.in/unquoted-attributes#foo%7Cbar

      Delete
    2. I love how people ignore xml standards and then wonder why there are so many broken pieces of crap software out there. Because of people doing things that 'look nice' in a document that is meant to be mechanically processed.

      Delete
    3. Well, html5 is not xml, though it has an xml representation. It's not even SGML-compliant, in spite of how it looks.

      Delete
  3. Note that <title> is only optional in a few edge cases. This post explains it: http://mathiasbynens.be/notes/minimal-html

    ReplyDelete
  4. Thanks for the comments Mathias, excellent set of posts on your website and at github.
    Found this quite informative: https://github.com/mathiasbynens/small

    ReplyDelete
  5. I take advantage of this behavior when throwing together a quick demo page. In that situation I don't bother with a doctype. I think a fair amount of developers think that if you don't have one you won't be able to use html5 stuff like the canvas tag in chrome for example, but that is not true. The only purpose it serves is to trigger standards vs quirks mode in old IE (maybe even still IE9?) and other browsers (old FF I think?). If anyone knows a list of browsers that it matters for, please chime in!

    ReplyDelete
    Replies
    1. Basically all of widely used browsers require the DOCTYPE (Chrome/Firefox/IE/Opera/Safari) to avoid switching to quirks mode. The only exception I can think of is Lynx.

      Delete
    2. Mike: see http://www.quirksmode.org/css/quirksmode.html
      also http://hsivonen.iki.fi/doctype/

      Delete
  6. I would recommend keeping an opening html element, in order to specify a 'lang' attribute.

    ReplyDelete
  7. There isn't any minimum requirement for an HTML file. Create a file with "Hello World", save it as .HTML it gets opened in a browser

    ReplyDelete
    Replies
    1. Is the DOCTYPE required, this is what the spec says.. for legacy reasons -- yes. http://www.whatwg.org/specs/web-apps/current-work/multipage/syntax.html#the-doctype

      Delete
  8. An employee at Opera once said to me:

    "For javascript omit the type='text/javascript' tags because what else is it going to be? And for stylesheets omit the type='text/css' because, again, what else is it going to be?"

    ReplyDelete
  9. Great post, saw it some where in a video and could not remember what the minimum structure required was.

    ReplyDelete

Post a Comment

Popular posts from this blog

Using duplicate IDs in HTML

Well today I'm being a bit controversial. Let us see what the HTML5 spec says about unique IDs in a HTML file. The  id  attribute specifies its element's  unique identifier (ID) . The value must be unique amongst all the IDs in the element's  home subtree  and must contain at least one character. The value must not contain any  space characters . An element's  unique identifier  can be used for a variety of purposes, most notably as a way to link to specific parts of a document using fragment identifiers, as a way to target an element when scripting, and as a way to style a specific element from CSS. Yes its been mentioned almost everywhere on the planet that ID must be unique. Now let us look at the below code, Launch dup.css #p2 {   background-color: yellow;  } dup-id.html <!DOCTYPE html> <html>   <head>     <title>Duplicate ID Tester</title> <link rel="stylesheet" href="dup.css" />  

Fixing Date, Time and Zone on RHEL 6 command line

Had to fix all time related issues on a remote RHEL 6 server which runs without any windowing system. Plain ol' command line. Documenting steps here for future reference: Check to see if your date and timezone settings are accurate: # date # cat /etc/sysconfig/clock The server I accessed had wrong settings for both the commands. Here are the steps I used to correct: Find out your timezone from the folder /usr/share/zoneinfo # ls /usr/share/zoneinfo Mine was pointing to America/EDT instead of  Asia/Calcutta Update and save the /etc/sysconfig/clock file to # sudo vi /etc/sysconfig/clock ZONE="Asia/Calcutta" UTC=true ARC=false Remove the /etc/localtime # sudo rm /etc/localtime Create a new soft link to your time zone # cd /etc # sudo ln -s /usr/share/zoneinfo/Asia/Calcutta /etc/localtime # ls -al localtime Now it should show the link to your time zone Set your hardware clock to UTC # sudo hwclock --systohc --utc # hwclock --show Update your t