XHTML Syntax

Writing XHTML code requires pure HTML syntax.

More XHTML syntax rules:

  • Attribute names must be lowercase
  • Attribute values must be enclosed in quotes
  • Attributes cannot be abbreviated
  • Use the Id attribute instead of the name attribute
  • The XHTML DTD defines the mandatory HTML elements

Attribute names must be lowercase

This is incorrect:

<table WIDTH="100%">

This is correct:

<table width="100%">

Attribute values must be enclosed in quotes

This is incorrect:

<table width=100%>

This is correct:

<table width="100%">

Attributes cannot be abbreviated

This is incorrect:

<input checked>
<input readonly>
<input disabled>
<option selected>
<frame noresize>

This is correct:

<input checked="checked" />
<input readonly="readonly" />
<input disabled="disabled" />
<option selected="selected" />
<frame noresize="noresize" />

The following is a list of abbreviated HTML attributes and their equivalents in XHTML:

HTML XHTML
compact compact="compact"
checked checked="checked"
declare declare="declare"
readonly readonly="readonly"
disabled disabled="disabled"
selected selected="selected"
defer defer="defer"
ismap ismap="ismap"
nohref nohref="nohref"
noshade noshade="noshade"
nowrap nowrap="nowrap"
multiple multiple="multiple"
noresize noresize="noresize"

Replace name attribute with id attribute

HTML 4.01 defines the name attribute for the following elements: a, applet, frame, iframe, img, and map.

The use of the name attribute is not encouraged in XHTML; it should be replaced with id.

This is incorrect:

<img src="picture.gif" name="picture1" />

This is correct:

<img src="picture.gif" id="picture1" />

Important compatibility tip:

You should add an extra space before the "/" symbol to make your XHTML compatible with today's browsers.

Language attribute (lang)

The lang attribute is applied to almost all XHTML elements. It defines the type of language used for the content within the element.

If the lang attribute is used in an element, an additional xml:lang must be added, like this:

<div lang="no" xml:lang="no">Heia Norge!</div>

Mandatory XHTML elements

All XHTML documents must have a file type declaration (DOCTYPE declaration). In an XHTML document, the html, head, and body elements must be present, and the title element must be located within the head element.

Below is a minimized XHTML file template:

<!DOCTYPE Doctype goes here>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Title goes here</title>
</head>
<body>
</body>
</html>

Tip:The file type declaration is not a part of the XHTML document itself. It is not an XHTML element and does not have a closing tag.

Tip:In XHTML, the xmlns attribute within the <html> tag is required. However, even if this attribute is not present in the XHTML document, the w3.org validation tool will not prompt an error. This is because, "xmlns=http://www.w3.org/1999/xhtml" is a fixed value, and this value will be added to the <html> tag even if you do not include it in the code.

You will learn more about XHTML document type declarations in the next chapter.