Monday, July 19, 2010

(X)HTML Minimum Document Structure

Every (X)HTML document must have a Document Type Declaration, html as the root element, head and body as the child elements, and title as the child element of head. In XHTML documents, the html element must have the attribute xmlns and xml:lang and lang.

Example 1: <!DOCTYPE HTML "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>some title</title>
</head>
<body>
body content
</body>
</html>

Example 2: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>some title</title>
</head>
<body>
body content
</body>
</html>

The lang attribute's value specifies the language of the content of the body tag. This attribute can also be used on other elements. The xml:lang attribute has the same function as lang, but it is used only in XHTML documents. The value of these attributes is a two-letter code specified in ISO 639-1 .

A webpage can also include a meta tag that specifies the character encoding of the page.

Example: <meta http-equiv="content-type" content="text/html; charset=utf-8">

The http-equiv attribute is used by HTTP servers to gather information for HTTP response headers. Therefore, in the above example, the HTTP response header will have content-type: text/html; charset=utf-8

The example below includes a meta tag that specifies the HTTP response header.

Example 3: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title><i>some title</i></title>
</head>
<body>
<i>body content</i>
</body>
</html>

No comments: