Document Structure Elements
Every XHTML document has three major components: the XHTML declaration, the head, and the body. The document structure elements are those that define each component.
<html>
Type:
Container
Function:
Declares the document to be an XHTML document. All document content and supporting XHTML code goes between the <html> and </html> elements.
Syntax:
<html> ... </html>
Attributes:
The <html> element can take the xmlns attribute, which specifies the URL that describes the XML namespace for the document (for example, xmlns="http://www.w3.org/1999/xhtml").
Example:
<html> ... all content and XHTML code goes here ... </html>
Related Elements:
Although the <html> element is typically the first element in a document, it is sometimes preceded by a <!DOCTYPE> element that specifies which level of HTML conformance the document displays. A document conforming to the XHTML 1.0 standard might have a <!DOCTYPE> element that reads
<!DOCTYPE XML PUBLIC "-//W3C//DTD XHTML 1.0//EN">
Technically, <!DOCTYPE> is an SGML element, not an HTML element, so it is acceptable for it to be outside the <html> and </html> elements.
<head>
Type:
Container
Function:
Contains the elements that compose the document head.
Syntax:
<head> ... </head>
Attributes:
<head> can take the profile attribute, which gets set equal to a space-separated list of URLs that point to meta-data profiles for the document. The profiles could describe the nature of the content, who wrote it, how it was published, and so on.
Example:
<html> <head profile="http://www.server.com/profiles/"> ... elements making up the document head go here ... </head> ... all other content and XHTML code goes here ... </html>
Related Elements:
Several elements can be placed between the <head> and </head> elements, including <base/>, <link/>, <meta/>, <script>, <style>, and <title>. Each of these is described next.
NOTE
The <ISINDEX> tag, which has been deprecated from HTML, does not have an equivalent in XHTML.
<base />
Type:
Standalone
Function:
Declares a global reference value for href and target attributes. The reference or base href value is used as a basis for computing all relative URL references. The base target value indicates which frame in a frameset is targeted by default.
Syntax:
<base href="base_url" />
or
<base target="base_target" />
Attributes:
A given <base /> element takes either the href or the target attribute, but not both at the same time. The usage of each attribute follows:
hrefSpecifies the reference URL that is used to help compute relative URLs. If the base href URL is http://www.myserver.com/sports/hockey/skates.html and you use the relative URL pucks.html elsewhere in the document, for example, the relative URL will really point to http://www.myserver.com/sports/hockey/pucks.html.
-
targetUsed to direct the results of a hyperlink click or a form submission to a particular frame in the frameset. A common use of this attribute is to set target="main" in a frame that contains navigation links. This results in the linked documents being loaded into the frame called "main" rather than being loaded into the navigation frame itself.
NOTE
When used in a <base> element, href is typically set to the URL of the document.
Example:
<head> <base href="http://www.myserver.com/index.html/" /> <base target="main" /> ... </head>
This code sets the document's base URL to http://www.myserver.com/index.html.
<meta />
Type:
Standalone
Function:
Defines document meta-information, such as keywords, expiration date, author, page-generation software used, and many other document-specific items. It also supports the notion of client pulla dynamic document technique in which the browser loads a new document after a specified delay.
Syntax:
<meta http-equiv="header" content="value" />
or
<meta name="name" content="value" />
Attributes:
The <meta /> element takes the following attributes:
-
http-equivSpecifies a type of HTTP header to be sent with the document. The value of the header is given by the content attribute. The two most commonly used values of http-equiv are refresh, which refreshes the page after a specified delay, and expires, which gives the date after which content in the document is not considered to be reliable.
-
nameSet equal to the name of the document meta-variable you want to specify. The value of the variable is given in the content attribute. Typical values for name include author, keywords, generator, and description. The keywords value is particularly useful for specifying words you want a search engine's indexing program to associate with the page.
-
schemeProvides information on how to interpret the meta-variable. For example, with the following <meta/> element:
<meta scheme="9-digit-ZipCode" name="zip" content="02134-1078"/>
a browser might not know how to interpret "02134-1078" without information from the scheme attribute.
-
contentSpecifies either the HTTP header or the value of the meta-variable.
Example:
<head> <!-- The first <meta/> element instructs the browser to load a new page after 5 seconds. --> <!-- This is useful for creating a splash screen effect. --> <meta http-equiv="refresh" content="5; URL=http://www.myserver.com/index2.html" /> <!-- The remaining <meta /> elements specify author and keyword information. --> <meta name="author" content="Eric Ladd" /> <meta name="keywords" content="Main page, welcome, neat stuff" /> ... </head>
<link />
Type:
Standalone
Function:
Denotes the linking relationship between two files.
Syntax:
<link href="url_of_linked_file" title="title" rel="forward_relationship" rev="reverse_relationship" />
Attributes:
The <link /> element takes the following attributes:
charsetDenotes which character encoding scheme to use.
hrefSet equal to the URL of the file to which you're making the linking reference.
hreflangSpecifies the language code for the linked file.
mediaProvides the intended display destination for the linked document. The default value of media is screen.
relSpecifies the relationship of the linked file to the current file.
revSpecifies how the current file relates to the linked file.
targetSpecifies which frame to target.
titleGives the link a descriptive title.
typeSpecifies the MIME type of the linked file.
Table 3.3 shows some possible values for rel and rev and what these values mean.
Table 3.3 Possible Values for the rel and rev Attributes
Value |
Meaning |
copyright |
Web site's copyright page |
glossary |
Glossary of terms for a site |
help |
Site help page |
home |
Site home page |
index |
Site index page |
made |
Mail to URL pointing to the email address of the page author |
next |
Page that logically follows the current page |
previous |
Page that precedes the current page |
stylesheet |
File containing style information for the page |
toc |
Site table of contents |
up |
Page that is above the current page in a site's hierarchy |
NOTE
Because so many types of linked files exist, it is permissible to have more than one <link /> element in a document.
Example:
<head> <link href="/style/styles.css" rel="stylesheet" /> <link href="/index.html" rel="home" /> <link href="/help.html" rel="help" /> <link href="back_one.html" rev="previous" /> ... </head>
<script>
Type:
Container
Function:
Contains script code referenced in the body of the document.
Syntax:
<script type="scripting_language MIME type"> ... script code goes here ... </script>
Attributes:
The <script> element can take the following attributes:
charsetDenotes which character encoding scheme to use.
deferSpecifying the defer attribute tells the browser that the script does not generate any document content. This enables the browser to continue parsing and rendering the document without having to execute the script.
srcSpecifies the URL of a file containing the script code, if not contained between the <script> and </script> elements.
typeSet equal to the MIME type of the script code, usually text/javascript or text/vbscript. When specifying a specific version of a scripting language, you can set type equal to a value that includes version information as well (for example, type="text/javascript1.1"). type is a required attribute under XHTML 1.0.
NOTE
The <script> element also takes the xml:space attribute, which tells processing programs to preserve whitespace within the element.
Example:
<script type="text/vbscript"> Sub ScriptEx document.write("<hr/>") document.write("<h1>Thank you for your submission!</h1>") document.write("<hr/>") </script>
TIP
Traditionally, script code is often placed between <!-- and --> elements so that browsers that can't process scripts will treat the code as a comment. However, under XHTML, a parser can remove what's contained inside a comment. Therefore, hiding your scripts with comment delimiters will probably not serve you well when writing for XHTML browsers.
Related Elements:
You can use the <noscript> element to specify what a browser should do if it's unable to execute a script contained in the <script> and </script> elements.
<noscript>
Type:
Container
Function:
Provides alternative content to use if a script cannot be executed. A browser might not be able to execute a script because the user has turned scripting off or because it does not know the scripting language used to write the script.
Syntax:
<noscript> ... alternative to script code goes here ... </noscript>
Attributes:
None.
Example:
<script type="text/javascript"> document.write("Hello, World!"); </script> <noscript> You either have scripting turned off or your browser does not understand JavaScript. </noscript>
<style>
Type:
Container
Function:
Specifies style information for the document.
Syntax:
<style type="mime_type" media="media_type" title="title"> ... style information goes here ... </style>
Attributes:
The <style> element takes the following three attributes:
mediaSpecifies what media types the styles are to be used for (visual browser, speech-based browser, Braille browser, and so on).
titleGives the style information a descriptive title.
typeSet equal to the Internet content type for the style language. You will most likely use type="text/css2" to denote the use of the style language put forward in the Cascading Style Sheets, Level 1 specification. type is a required attribute of the <style> element.
NOTE
The <style> element also takes the xml:space attribute, which tells processing programs to preserve whitespace within the element.
Example:
<style type="text/css2"> <!-- body {font: 10 pt Palatino; color: silver margin-left: 0.25 in} h1 {font: 18 pt Palatino; font-weight: bold} h2 {font: 16 pt Palatino; font-weight: bold} p {font: 12 pt Arial; line-height: 14 pt; text-indent: 0.25 in} --> </style>
NOTE
Style information has traditionally been placed between <!-- and --> elements so browsers that cannot process it will treat the style information as a comment. However, this is not recommended for XHTML documents since an XHTML parser is free to remove contents inside of comments.
<title>
Type:
Container
Function:
Gives a descriptive title to a document. Use of the <title> element is required by the XHTML 1.0 DTD for many good reasons. Titles show up in browser window title bars and in bookmark and history listings. In each of these cases, you provide an important reader service when you specify a title because otherwise the browser displays just the document's URL. Additionally, Web search engines, such as Yahoo! and AltaVista, frequently look for title information when they index a document.
Syntax:
<title> ... document title goes here ... </title>
Attributes:
None.
Example:
<title> XHTML Tutorial </title>
TIP
Try to keep titles to 40 characters or fewer so that browsers can display them completely.
<body>
Type:
Container
Function:
Contains all content and elements that compose the document body.
Syntax:
<body> ... document body goes here ... </body>
Attributes:
None.
NOTE
The popular <BODY> tag attributes used in HTML--BGCOLOR, BACKGROUND, TEXT, LINK, VLINK, and ALINKdo not have equivalents under the Strict XHTML DTD. You should use style sheets to specify these display characteristics or use the Transitional DTD instead.
NOTE
The <body> element is not allowed under the XHTML Frameset DTD.
Example:
<body> ... all document body content and XHTML code goes here ... </body>
Related Elements:
Dozens of elements are allowed inside the <body> element. In fact, with the exception of some of the frame-related elements, any element in the rest of the chapter can be placed between <body> and </body>.
By putting together what you've learned in this section, you can come up with a generic XHTML document template such as the following:
<html> <head> <title>Document Template</title> ... <meta />, <base />, <link />, <script>, <style> elements ... </head> <body> ... document body content and elements ... </body> </html>
When creating a new document, you can use this code to get started, and then fill in elements and other information according to your needs.