Declarations To Consider

Here you can find out more about defining a valid XHTML document.

A document declaration should be at the very top of your web pages. Without specifying a doctype, your HTML is not valid and most browsers will switch to ‘quirks mode’. In other words, the browsers will think that you just don’t know that you are doing and decide what to do with your code on their own. Even perfect CSS is nothing without a document declaration as well as with a wrong document declaration.

So, the document declaration for XHTML 1.0 Strict looks like this:Doctype Declarations to remember

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”

The following is the document declaration for XHTML 1.1:


You can use XHTML 1.0 Transitional in case you prefer HTML 4:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”

If you use frames, the XHTML 1.0 Frameset document type declaration looks like this:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Frameset//EN”

Note that the DOCTYPE tag should be written in upper case and adorned with an exclamation mark. It is the only tag that doesn’t need closing.

Language Declarations

To identify the primary language of a document have nothing to do with a valid HTML document. It is all about accessibility. Thus, you can indentify it through an HTTP header or with the xml:lang attribute inside the opening html tag. The value is an abbreviation, for example, ‘en’ (English), ‘fr’ (French) or ‘de’ (German).

Here is the sample of a document with primarily English content:

<html xmlns=”” xml:lang=”en”>

In case you use other languages than in your content, use use the xml:lang attribute in-line (such as <span xml:lang=”fr”>HTML règles</span>).

Content Types

Declarations to know

The following HTTP header specifies the media type and character set of an HTML document:

Content-Type: text/html; charset=UTF-8

Let’s describe this example in detail.

text/html – is the MIME type of the file, that lets the browser know what media type a file is and what to do with it. All files have some kind of MIME type. A CSS file is text/css, a JPEG image is image/jpeg and the type generally used for HTML is text/html.

UTF-8 – the second part of the HTTP header is the character set.

You can use an ‘HTTP-equivalent’ meta tag in the HTML to set an HTTP header:

<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″ />

You also can use a server-side scripting language that with PHP may look like this:

<? header(“Content-Type: text/html; charset= UTF-8”); ?>

In case using a server-side scripting language is problematic for you, you can go straight to the server with an ‘.htaccess’ file. Most servers (Apache compatible) can have a small text file with the file name ‘.htaccess’ that is in the root directory. Using the following line in it, you can associate all files with the extension ‘.html’ with a MIME type and character set:

AddType text/html;charset=UTF-8 html

Character sets include ‘ISO-8859-1’ for many Western, Latin based languages, ‘SHIFT_JIS’ for Japanese and ‘UTF-8’, a version of Unicode Transformation Format, which provides a wide range of unique characters used in most languages.

Of course, you can use a character set that will be recognized by the users of your site. For Latin-based language (including English) you can use ISO-8859-1, while UTF-8 can display most characters from most languages. What is more, the latter code is the safest one because it will work on most computers.

XHTML should be served by the MIME type application/xhtml+xml.

With most browsers having no clue what this is, you can use the MIME type text/html. According to W3C XHTML 1.0 may be served as text/html, but XHTML 1.1 should not.

Here is the sample of the script that placed at the top of every page of the site that uses PHP to serve XHTML 1.1 with an application/xhtml+xml MIME type to those browsers that understand the type (Mozilla) and XHTML1.0 Strict with the text/html type to other browsers (IE):

header(“Content-Type: application/xhtml+xml; charset=UTF-8”);
echo(‘<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN” “”>’);
} else {
header(“Content-Type: text/html; charset=UTF-8”);
echo (‘<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “”>’);

If the browser accepts the application/xhtml+xml MIME, that MIME type is sent and the XHTML1.1 document type is written to the HTML. In case the MIME type isn’t recognized then the text/html MIME type is sent and the XHTML1.0 Strict document type is written in the HTML.

Comments are closed.