The construction of a simple web page requires a minimum number of elements as shown below.
<html> <head> <title>....</title> </head> <body> ....... </body> </html>
However, to conform with XHTML standards, further declarations and attributes are required as shown in the highlighted text below. This is also the case with previous HTML standards although the actual components are a little different.
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>....</title>
</head>
<body>
.......
</body>
</html>
XHTML is actually an XML document, hence the presence of the XML declaration (<?xml...>).
This is not required in all XML documents although its use is strongly recommended by the W3C.
However be warned, IE6 for Windows has a problem with this declaration.
If a 'strict' DOCTYPE is specified and the <?xml ...> declaration is present, then strict mode is not followed in all respects.
This may, and probably will, cause your style sheets to behave a little differently from expected.
If the declaration is present, the version number is always required but the encoding information is optional.
If the character encoding is left out though, the document may not validate correctly.
If you choose to omit the declaration, recommended if you're using the 'strict' DOCTYPE, the important character encoding may be specified by the use of a <meta> element in the HEAD section, see below.
The DOCTYPE declaration must be present, validation cannot be made without it.
This states what Document Type Definition (DTD) you are using.
A DTD is basically a definition of the markup language.
For XHTML 1.0, there are three possible DOCTYPE declarations; Transitional, Strict and Frameset.
Strict is the ideal definition to use, as it is free of any markup associated with layout. Cascading Style Sheets (CSS) should be used to specify the font, color, and layout effects you want. A minor downside is that it is more than likely not compatible with older browsers where support of CSS was poor. No excuse for an old browser now with the new generation of browsers like Firefox.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
Transitional allows use of deprecated elements and attributes from HTML 4.0.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Frameset is for when the document is using frames.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
The xmlns attribute of the <html> tag must be used to set the location of the XHTML namespace.
A namespace provides a simple method for qualifying names used in XML documents by associating them with namespaces identified by a URI.
But don't worry about this just, put it in!
Although optional, it is common to specify the language attribute, lang="en", after the namespace, as in this example.
The only required element within the HEAD section is the <title> element.
The text within the <title> and </title> tags is the title of the browser window.
Everything within the BODY section defines the contents and format of the page. This may include text, HTML or scripting code. The scripting code would be in another language such as JavaScript.
There are a number of optional elements available for use in the HEAD section.
They are the <meta>, <link>, <style> and <script> elements.
<meta> element you can describe many details about the document, including author, default document information and keywords.
<meta name="author" content="John Doe" /> <meta http-equiv="Expires" content="Wed, 24 Dec 2003 11:39:26" /> <meta name="keywords" content="HTML, XHTML, Harlequins, Stalefish1" /> <meta name="description" content="XHTML Reference guide - Basic structure" /> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
Because of the problem mentioned above with the <?xml ... ?> declaration, this is the suggested method for specifying the character encoding as seen in the last example above.
<link> element is to link an external style sheet to the document.
<link rel="stylesheet" type="text/css" href="css/XHTMLRefGuide.css" />
The rel and type attributes specify that the link is to a CSS type style sheet with href specifying the location of the style sheet.
See the chapter on Links for further details on how to specify values for the href attribute.
<style type="text/css"> <!-- .... --> </style>
Note the use of the Comment tags within this element. This is to prevent older browsers which do not support style sheets rendering the contents of this element as text. See the chapter on Cascading Style Sheets for details on style sheet syntax.
<script> element is used to contain scripting language.
It may be placed in the HEAD or BODY sections.
There may also be multiple occurrences of this element.
<script type="text/javascript"> <!-- .... //--> </style>
Note that in this example there are comment tags.
This is so older browsers which do not understand script will ignore the code.
The // is a JavaScript comment which prevents the XHTML comment from being detected as a script error.
After the script is functioning correctly it is often more convenient to place the code in an external file. This also removes the necessity of including comment tags.
<script type="text/javascript" src="myscript.js"></script>
Elements in the BODY section are divided into two groups. They are either block-level elements or inline elements.
A block-level element such as <p> or <div> is used to define the structure of the document.
They can contain other block-level elements or inline elements.
A inline element such as <span> or <img> is used at a text level defining the content of the document rather than the structure.
Consequently an inline element should not contain a block-level element.
Strict XHTML requires that inline elements be contained in a block-level element although this is not necessary for transitional XHTML.
Browsers ignore white space e.g. blanks, tabs, carriage returns etc. Multiple spaces between words will be displayed as one space. Therefore XHTML elements must be used to control breaks in the text. It is recommended though that your XHTML is well laid out with appropriate line spaces and indentations so that viewing the source and any subsequent maintenance is easier.
To define a paragraph, the <p> and </p> tags are used.
This causes a blank line to be inserted at the start and end of any enclosed text.
Remember that although the closing tag is optional in HTML, XHTML standards require that all elements are closed.
Style characteristics of the paragraph can easily be changed by applying style sheets to the paragraph.
Sometimes however, styles need to applied to a block of data which may comprise more than one paragraph and other elements.
This is where the <div> element is useful.
All the required elements can be grouped together within the <div> and </div> tags and a style applied to the whole block.
Combining the <div> element with style sheets is a powerful way to design the layout of a web page without using tables.
A <div> does not cause any presentational effect other than causing a line break. This can be overridden by the application of a style sheet.
A line break can also be forced within a paragraph by use of the <br /> tag.
As with <div>, a blank line is not inserted after the break.
A horizontal line (i.e the separator between these sections) can be displayed across the page using the <hr /> element.
This also causes a line break.
The separators used in this document display using their default properties.
All presentation attributes for this element (align, width, size and noshade) have been deprecated but its presentation can be changed by the use of style sheets.
In the following example the line is displayed as ½ the display width, in red and aligned to the right.
<hr style="width:25%; color:red; text-align:right; " />
displays as:
Note that in some browsers the colour and alignment and ignored.
Use <hn> and </hn> tags (where n is an integer) to create headings in the page.
<h1> displays the largest font size (see 1st title line of this page).
<h2>, <h3> etc. give progressively smaller headings.
(2nd title line and this section heading respectively).
These elements cause a line break and insert a blank line.
Again style sheets should be used to change the presentation.
<h3 style="color: red; text-align: right" />This is a heading 3</h3>
displays as:
Comments may be included in a page by enclosing the text between <!-- and --> delimiters.
This text will not be displayed in the page, it is only there for reference by the author or anyone viewing the source code.
Note that scripts and style sheets have their own delimiter convention for comments.