XHTML syntax
There are certain syntax rules in XHTML that must be followed.
This lesson focuses on:
- XHTML syntax rules
XHTML syntax rules
These syntax rules are not the same as the tag rules discussed in the differences between HTML and XHTML lesson, though some of the information from that lesson is repeated here.
The syntax rules presented in this lesson mostly revolve around tag attributes.
Attribute names must be in lower-case
Not just attributes, but tags as well must all be in lowercase in XHTML.
Example with uppercase attributes and tags:
The above example fixed to conform to proper XHTML syntax:
Attribute values must be quoted
Attributes should always be quoted. Even though a page may appear as you intend it to even if you don't quote attributes, the page will not validate with an HTML validator. Also, if the value given to the attribute that is not quoted has at least one space, the page may actually not appear as you intend it to.
Example of not quoting attributes:
Output:
In the above example, we omitted quotes from the value attribute of the <input> tag which in this case displays a button. The text that is supposed to be displayed on the button is "I am a button", but only "I" is displayed since it cuts off after the space. For the href attribute in the <a> tag, we omitted quotes as well, but it still works as intended.
The above example fixed to conform to proper XHTML syntax:
Output:
Attributes cannot be minimized
There are some attributes in HTML that in the past have been minimized. With XHTML, this cannot be done.
A list of attributes that are minimized in HTML, and how they should be coded 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" |
Example with minimized attributes:
The above example fixed to conform to proper XHTML syntax:
The name attribute is replaced by the id attribute
In HTML 4.01, there are certain tags which contain a name attribute including <a>, <applet>, <frame>, <iframe>, <img>, and <map>. The name attribute is deprecated in XHTML, and instead the id attribute should be used.
NOTE: Even though the name attribute is deprecated in favor of the id attribute, use both attributes for backwards compatibility -- for older browsers.
Example using only the name attribute:
The above example fixed to conform to proper XHTML syntax:
Some elements are mandatory
XHTML documents must contain a doctype declaration. Mandatory XHTML elements include the html, head, body, and title elements. The title element must be located inside the head element.
A basic XHTML document with the mandatory elements (including the doctype declaration):
NOTE: The doctype declaration is NOT an XHTML element, and not part of the XHTML document. The doctype declaration is used to declare which document type the XHTML document is.
The doctype declaration as well as the different document types you can have in XHTML are covered in the XHTML document types lesson.




