Starting Your Web Page






Starting Your Web Page

Begin your page by using a DOCTYPE (see page 40) 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.

Figure. 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.


To start a transitional HTML 4 page:

1.
Type <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> to declare that you're using transitional HTML 4.01 in your Web page.

2.
Type <html> to begin the actual HTML portion of your document.

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

4.
Type </html>.

To begin a transitional XHTML page:

1.
Type <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> to declare that you're using transitional XHTML in your Web page.

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

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

4.
Type </html>.

HTML vs. XHTML

For a thorough discussion on whether you should use HTML or XHTML to write your Web pages, see XHTML vs. HTML: What Should You Use? on page 21 and HTML vs. XHTML on page 38. The short answer is that right now, it doesn't matter much as long as you are consistent and don't mix. If you're writing HTML, write HTML; if you choose XHTML, follow the XHTML rules. In this book, the examples are written in XHTML because XHTML encourages consistency and standards, and consistent, standards-conscious Web pages have a better chance of displaying properly and consistently across Web browsers and platforms. HTML, because it is less picky, sometimes engenders laziness. That laziness can translate to erratic or unexpected Web page display when viewed on a variety of browsers and platforms.


Tips

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

  • Both the DOCTYPE and the html element are optional in HTML (even strict HTML). XHTML requires both (with the name-space 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 26) or at http://www.w3.org. For help choosing an appropriate DOCTYPE, see page 40.

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

  • 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 (see page 41).

  • Declaring the appropriate DOCTYPE tells validators which specifications to compare your code against (see page 345).

  • Note that the word DOCTYPE (since it actually originated from another language called SGML) is typed in all uppercase letters, both in HTML and in XHTML.

  • XHTML pages, because they are XML, should technically begin with an XML declaration like <?xml version="1.0" encoding="ISO-8859-1"?>. However, such a declaration throws IE 6 for Windows unaccountably into quirks mode (see page 41). It's a huge bug. Luckily, since most XHTML files are not served or otherwise treated as XML, the declaration can and should be omitted (and the encoding declared as on page 59).


Figure. Here's the DOCTYPE for a transitional XHTML document, the opening html tag and required namespace declaration, and the closing html tag.




 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows