The Doctype
<!DOCTYPE html>
This is known as the doctype, which is short for Document Type Definition. It must be the first item on a web page, appearing even before any spacing or carriage returns. In earlier versions of this book, the doctype we used was to declare the page as XHTML 1.1 strict. It is, to be honest, quite nasty to glance at, given that this is your first exposure to the world of HTML. How nasty? Well, this is what we used to specify:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
Try memorizing that if your life depended on it!
Thankfully, we can use the much simpler HTML5 doctype, and everything works smoothly. But what is this doctype?
Have you ever taken a document you wrote in Microsoft Word 2007 on one computer, and tried to open it on another that only had Word 2000 on it? Frustratingly, without some preemptive massaging when the file is first saved, this fails to work as expected. It fails because Word 2007 includes features that Bill Gates and his team had yet to dream up in 2000, so Microsoft needed to create a new version of its file format to cater for these new features.
Just as Microsoft has many different versions of Word, there have also been different versions of HTML over time, for example HTML 3.2, HTML 4, XHTML 1.1 and now HTML5. Mercifully, the different versions of HTML were designed so that there’s no suffering the same kind of incompatibility gremlins as Word. If you throw some HTML5 at an older browser that fails to understand what it’s been given, it will generally render it as plain text, which may be absolutely fine. Conversely, newer browsers will cope with old markup defined in earlier versions of HTML, even HTML elements that have since been dropped (or deprecated, to use the official language). The doctype’s job is to specify which version of HTML the browser is about to be given. The browser then uses this information to decide how it should render items on the screen. There are a lot of doctypes that you could use but, trust me, you’re best just sticking with the simple one that HTML5 gives us. And yes, eagle-eyes, you’re right—this doctype doesn’t actually state an HTML version at all, unlike previous doctypes. We’ll avoid opening that particular can of worms, as it’s a big can; just take pleasure in the fact that you can easily remember this one, even without your life depending on it.
0 comments:
Post a Comment