The document declaration


The student who completes this chapter should be able to:

The Doc Type Tag

Many Web pages don’t use the <DOCTYPE> statement. Depending on the complexity of your code, leaving it out might not make a lot of difference. But to ensure that the browser renders your website correctly, put it in. You must have it if you want to validate your Web pages.

The Modes

To deal with Web pages written for older browsers, the newer browser versions (Mozilla-based browsers, Netscape 7, Windows Internet Explorer 6, Mac Internet Explorer 5, Opera 7, and Safari) have two types of presentation modes: standard and quirks. Using the standard mode, the browser tries to follow W3C recommendations. When the browser goes into quirks mode, it tries to emulate an older version of the browser. This could cause the browser to ignore your page’s style sheets and render the page unusable.

Mozilla and Safari also have almost standards modes, affecting mostly the vertical size of tables.

What Is It?

The <DOCTYPE> declaration tells the browser what version of (X)HTML is being used so it will know how to display the page. The declaration also can provide a link to a text file (i.e., the loose DTD) that the browser uses to get more information. It’s similar to having a tune-up done on your car: The mechanic must know the make, model and year, the number of cylinders in the engine and its horsepower. Once he knows this, he can tune the engine using the right specifications and parts.

The Specs

The specifications for HTML 4.01 and HTML5 1.0 set by the World-Wide Web Consortium requires the <DOCTYPE> in all documents.

The W3C sets the specifications for common protocols used on the Web to ensure that each website is accessible to browsers, readers such as Acrobat and plug-ins such as Flash. These are not “standards,” of coding, only “specifications,” that the W3C refers to as “recommendations.” Most Web developers consider these specifications to be standards, and browsers such as Firefox, Opera, Safari follow the specifications.

The benefits of following the recommendations begin with having your Web pages work correctly. When errors arise, help is easier to come by if you follow the W3C specs. Otherwise, you are doomed to a trial-and-error nightmare of getting things to work, with no guarantee that what works on your browser will work on others.

Using the DOCTYPE tag

The <DOCTYPE> declaration must be the first thing in your document, before the <html> tag. When the browser loads the page, it knows how to handle the code.

HTML5 1.0 and HTML 4.01 both use three document types: Strict, Transitional and Frameset. Because we want to build our Web pages to be functional into the forseeable future, we will use an HTML5 1.0 document declaration. We will use the Transitional type to accommodate older browsers that might not support Cascading Style Sheets:

Here is the doctype declaration we will use:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML5 1.0 Strict//EN"

The other two HTML5 1.0 specifications look like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML5 1.0 Transitional//EN"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML5 1.0 Frameset//EN"

Here are the doctype specs for HTML 4.01:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"

Because the <DOCTYPE> declaration is a comment tag, it won’t confuse older browsers that don’t understand the statement.

Additional Information