Let's start right at the beginning of an HTML5 document. Screw this part up and you could spend a long time wondering why your page doesn't behave as it should. The first few lines should look something like this:
<!DOCTYPE html> <html lang="en"> <head> <meta charset=utf-8>
Let's go through these tags one by one. Generally, they will be the same every time you create a web page but trust me, it's worth understanding what they do.
The doctype is a means of communicating to the browser the type of document we have. Otherwise, it wouldn't necessarily know how to use the content within it.
We opened our document with the HTML5 doctype declaration:
<!DOCTYPE html>
If you're a fan of lowercase, then <!doctype html> is just as good. It makes no difference.
This is a welcome change from HTML 4.01 pages. They used to start something like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
What an enormous pain in the pimply rear! No wonder I used to copy and paste it!
The HTML5 doctype on the other hand is nice and short, just <!DOCTYPE html>. Interesting fact (to me anyway): it actually ended up this way as it was determined that this was the shortest method of telling a browser to render the page in "standards mode".
Want a history lesson in what 'quirks' and 'standards' mode were? Wikipedia has you covered: http://en.wikipedia.org/wiki/Quirks_mode
After the doctype declaration, we open the html tag; the root tag for our document. We also use the lang attribute to specify the language for the document, and then we open the <head> section:
<html lang="en"> <head>
According to the W3C specifications (http://www.w3.org/TR/html5/dom.html#the-lang-and-xml:lang-attributes), the lang attribute specifies the primary language for the element's contents and for any of the element's attributes that contain text. If you're not writing pages in English, you'd best specify the correct language code. For example, for Japanese, the HTML tag would be <html lang="ja">. For a full list of languages take a look at http://www.iana.org/assignments/language-subtag-registry.
Finally, we specify the character encoding. As it's a void element (cannot contain anything) it doesn't require a closing tag:
<meta charset="utf-8">
Unless you have a good reason to specify otherwise, the value for the charset is almost always utf-8. For the curious, more information on the subject can be found at http://www.w3.org/International/questions/qa-html-encoding-declarations#html5charset.