Summary: Document validation is the single most important tool an author has available. Validating not only makes sure your documents are well-formed, but makes them more robust and ready for the future. Get the details on common validation errors and how to fix your documents so you can avoid them. Like a nation or a house, a page divided against itself cannot stand-- not in standards-compliant browsers, anyway. Every page has a structure, and it turns out that if you aren't careful with your construction methods, the structure will be weakened, flawed, and potentially dangerous. If you've ever loaded up a page in Opera or Netscape 6 or Internet Explorer and had it look totally mangled, odds are that you've inadvertently built a shaky structure.
Imagine building a house on a foundation of sand, or with rubber support beams. Most people wouldn't even bother, and anyone who did shouldn't be surprised by huge cracks in the walls, wildly uneven flooring, or even total collapse of the structure. Yet many authors are shocked to discover that their pages fall apart in recent browsers. The usual reaction is that "the page was fine before!" which is exactly like saying "my rubber-column house didn't collapse on the same day it was built!" Perhaps not, but it was always in danger of falling over.
So how does one ensure a good, solid Web house? Well-structured markup. A clean document structure is absolutely essential to ensuring that your pages will behave in browsers both present and future. Fortunately, fixing up a page's structure after it's been built is a lot easier and less expensive than trying to correct structural flaws in a house! In fact, there are HTML validators out there that can help you identify the problems and quickly correct them. We highly recommend the World Wide Web Consortium's HTML Validator-- not only because it's provided by the same people who are responsible for the HTML and XHTML specifications, but also because most of its error messages provide a link to an explanation of what the error means. Eventually, of course, you'll recognize what each error message means without having to look up the explanation, but when you're starting out these help files are invaluable.
Your goal is simple: to bring your page to a state where it doesn't generate any errors at all. For bonus points, you could try to eliminate any warnings as well, but the important thing is to avoid having errors. There are, practically speaking, two general kinds of errors:
- Warnings about elements, which are the most serious and can really mangle a page if left uncorrected. For example, an error like "element '
TD' not allowed here", which implies that you either have a
TDoutside of a table element, or else the validator thinks you do. Either way it's a major problem, and finding out why should be a top priority. An element error is equivalent to a contractor telling you that he left some critical support beams out of your house.
- Warnings about attributes, which are less serious since most browsers will ignore any attribute they don't understand. This is not to say that attribute errors can be ignored, but they are generally less of a concern than element errors.
As you fix your markup to remove one error, you may find that you generate more-- or that suddenly several other errors go away. For example, if you add a missing end-table tag (</table>) to a document, you might fix every "element not allowed here" error that followed. In any case, the goal of every author should be to have no errors at all of either kind.
DOCTYPE and Validation
When you validate your document, you have the option to pick which Document Type Definition (DTD) you want to use as the standard. There are many options available, from HTML 2.0 up to the most recent standard available (it was XHTML 1.1 when we wrote this article). If you want your pages to work in today's browsers, then the best choice is a recent DTD. Given the generally backwards-compatible nature of HTML and XHTML, validating against a recent DTD should mean you'll be all right if older browsers drop by.
Rather than picking a DTD from the provided list, you can also place a
DOCTYPE element at the top of your document, thus marking it as using a specific DTD. Let's say you wanted to use the HTML 4.0 Strict DTD. In that case, the very first line of your document (even before the
<html> tag) should be:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
Once you've added this element to the top of your document, then you can use the Document Type option "(specified inline)" and the W3C validator will use the DTD you've declared in your document to validate the markup.
It is also the case that recent browsers (Netscape 6, Explorer 5 for Macintosh, Explorer 6 for Windows) will make use of the
DOCTYPE element to determine the "rendering mode" you want browser to use when displaying your document. Generally speaking, any "transitional" or "loose" DTD, or even a lack of a
DOCTYPE, will cause the browsers to use a rendering mode that emulates legacy browser behavior. "Strict" DTDs, on the other hand, will switch browsers into a standards-compliant rendering mode. This is an easy way for authors to decide how they want browsers to handle their markup. The Apple Developer Connection has an article called "DOCTYPE Explained" that covers this territory in more detail; note that Internet Explorer 6 for Windows also supports the "DOCTYPE switching" described in the article.
There are a few errors that authors will likely see many, many times as they validate pages. There are also a few things that a validator might not catch (software is generally as perfect as the humans who write it). Here are a few of the most common errors and pitfalls to avoid.
Forgetting Important Attributes
If you get an attribute-related error, it's very likely going to tell you that you forgot to include a required attribute. These include:
typeattribute for the elements
altattribute for the elements
summaryattribute for the element
The latter two attribute are important for accessibility reasons, as their inclusion assists users who are using text-only or audio browsers. The first attribute we mention,
type, is critical for forward compatibility. As an example, many browsers (including Netscape 6) will ignore any
STYLE element that has no
type attribute, which has the usually unwanted effect of disabling the entire stylesheet.
A related situation is that the strict DOCTYPE for HTML and XHTML does not permit the attribute
type is the only way to mark what kind of script is being used. Thus, if you have a script that starts like this:
...then the validator is quite likely to throw an error. You can fix this by modifying the element to read:
Original Document Information
- Author(s): Eric A. Meyer, Netscape Communications
- Last Updated Date: Published 05 Mar 2001
- Copyright Information: Copyright © 2001-2003 Netscape. All rights reserved.
- Note: This reprinted article was originally part of the DevEdge site.