Good Practices and Page Validation

HTML has developed over time, and many of the coding practices of years gone by do not work well for modern browsers. It is always best to make your coding comply with current standards to ensure that it displays as consistently as possible in different browsers and to ensure that future browsers are able to interpret it.

The Document Type Declaration

How do you comply with standards? First you have to choose one, and there are several out there. For our purposes, they fall into two categories: versions of HTML and versions of XHTML. At the beginning of every web page, you should "declare" which you are using. This is called the Document Type Declaration (DTD) . It looks like one of the following:

For HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

For XHTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

There are other possibilites, but these will suffice for exemplary purposes. The information in the DTD is essentially a reference to the web address where the description of the standards for the (X)HTML version can be found.

The DTD should be the first element on your web page (before <html>).

The Namespace Declaration

The DTD is a file, but there is also a web-based reference you can consult, and it is good practice to include a notation of this reference as an attribute of the <html> element. If you are coding in XHTML (which you should), the <html> element will look like this:

<html xmlns="http://www.w3.org/1999/xhtml">

The attribute xmlns (which stands for xml namespace) just contains the URL for reference. If you type it into your browser, you can consult it. The namespace has an important function in cases where your coding is ambiguous, but you will probably not encounter such problems. Still, it is good practice to include it.

The Character Set Declaration

For good measure, we cna also provide information for the browser about what character set standard we are using. This is accomplished by a <meta> tag inside the <head> element.

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

If you do not do this, your browser will default to the one above, unless you are in a country like, say, Japan, and your browser is set to default to a Japanese character set. The Latin character set in the code above is an aging standard, which is now being replaced by utf-8. Hence it is probably best to include the following code instead:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

Summary

This is a lot to remember to type, and the easiest thing to do is just cut and paste the declarations into every web page you make. To make this easier, here is the beginning of an XHTML document:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Page Title</title>
</head>
<body>
</body>
</html>

Page Validation

A web page which conforms to the standard in its DTD is considered to be valid. That means that, in order to be valid, the page must declare its document type. In the case of an XHTML page, it must also follow XML coding practices as described in Differences between HTML and XHTML. "Validating" a web page means checking that it is valid. It is good practice to validate your web page before you publish them. There are many reasons to validate your web page. For a good discussion, see the W3C's Why Validate? page. The W3C in fact provides an online Markup Validation Service, which you can use to check your page. All you have to do is enter your URL. Just remember to select the correct DTD and character set in the options.

Return to Top