This chapter is from the book

This chapter covers the most basic (X)HTML elements—the ones you need to create the structure of your document. You'll learn how to create new paragraphs, headers, page breaks, comments, and more.

Creating a clear and consistent structure makes it that much easier to apply styles to your document.

Starting Your Web Page

Begin your page by using a DOCTYPE(see page 38) to declare what type of HTML or XHTML you're using. The DOCTYPE lets browsers know what to expect and tells validators how to judge your code in order to check its syntax. Then, signal the beginning of the actual code with the opening html tag.

Type <html xmlns="http://www.w3.org/1999/xhtml"> to begin the XHTML portion of your page and declare its namespace.

Leave a few spaces for creating the rest of your page (using the rest of this book).

Type </html>.

Example 3.1. Here's the DOCTYPE for a transitional HTML document as well as the opening and closing html tags. It's a gruesome bit of text. I recommend just copying it from one document to the next instead of trying to type all that gobbledy-gook.

Both the DOCTYPE and the html element are optional in HTML (even strict HTML). XHTML requires both (with the namespace declaration in the opening html tag). Note that there is no xhtml element.

I've only shown how to write the DOCTYPE for transitional HTML and XHTML. You can find a list of common DOCTYPE declarations on my Web site (see page 24) or at http://www.w3.org. For help choosing an appropriate DOCTYPE, see page 38.

Create a template with the appropriate DOCTYPE declaration and html tag as a starting point for all your pages.

Declaring a DOCTYPE with a URL at the top of your Web page generally puts current browsers in standards mode—letting you use standards-compliant code in order to have more control over the display of your Web page (see page 39).

If you start an XHTML page with the xml declaration, IE 6 for Windows goes into quirks mode. That's a huge bug. The workaround? Skip the (optional) declaration and declare the encoding with the meta tag instead (see page 63).

If you use non-standard HTML tags, there's no point in specifying a DOCTYPE. Just enclose your page in opening and closing html tags. Current browsers will use quirks mode when displaying your pages, letting you take advantage of old, soon-to-be-obsolete bugs (see page 39).