Why Validate?
Why Should I Care?
If you've ever worked with any computer languages, you may have noticed something unusual about HTML. Or more accurately, it's what you haven't noticed – you never get any error messages. Why not? It's not like there aren't any rules on how to write HTML properly – in fact, there are quite a few – it's just that your web browser never tells you when you make a mistake. This is understandable. After all, if web browsers refused to render any web pages containing syntax errors, the World-Wide-Web would be an endless mess of error messages, and a large portion of the web would be unavailable. Needless to say, most of us would rather read a badly written web page than nothing at all.
With this in mind, you might ask why you should even bother making sure that your web pages are constructed correctly, if web browsers will display them anyway. In order to understand the importance of writing HTML correctly, you'll first have to understand how it is that web browsers display incorrect HTML in the first place.
Quirks Mode
If you try this website out in different browsers, you'll notice that they all display it almost exactly the same. In order to achieve this, I had to make sure that the browsers all know I used a current version of HTML – they don't know this automatically. When your web browser encounters a malformed web page, or one written in an outdated version of HTML, it doesn't magically know how it was meant to be displayed. Instead, it has to make some educated guesses as to what the developer meant, and interpret the page according to some legacy rules. This is known as quirks mode. Unfortunately, the guesswork is often inaccurate, and since the rules are not standards, the way the page is displayed varies slightly from one browser to the next. If you want your website to be displayed the way you intend, you can't have this. So how do you avoid quirks mode?
The Document Type Definition (DTD)
To let the browser know which version of HTML you have used, you need to add something called a Document Type Definition, or DOCTYPE (the part at the top of the page that reads something like <!DOCTYPE html PUBLIC ... >). There are three main versions that are widely used today:
- HTML 4.01
- XHTML 1.0
- HTML 5 (the current standard)
Note that HTML 4.01 and XHTML 1.0 come in three distinct types:
- Transitional:
-
Allows some deprecated presentational tags and attributes
like
<center>andbgcolor, which are no longer supported in more recent versions.
Useful for an older website being upgraded to a more current version of HTML. - Frameset:
-
Allows the use of HTML frames (the
<FRAMESET>and<FRAME>tags).
Useful for keeping an old frame-based website standards compliant. - Strict:
- These are the only versions that fully comply with standards.
If your web pages are fairly simple, adding a DOCTYPE shouldn't make much of a difference in how they are displayed by the web browser. But is it actually the right one? How do you know which version of HTML you are using? How do you know if your web page even complies with this particular version? This brings us to the purpose of the HTML validator.