XHTML - Syntax - Xhtml Tutorials


XHTML syntax is very similar to HTML syntax and almost all the valid HTML elements are valid in XHTML as well. But when you write an XHTML document you need to pay a bit extra attention to make your HTML document compliant to XHTML.
Here are the important points to remember while writing a new XHTML document or converting existing HTML document into XHTML document:
  • You need to write a DOCTYPE declaration at the start of the XHTML document.
  • You must write all XHTML tags and attributes in lower case only.
  • You need to close all XHTML tags properly.
  • You must quote all the attribute values.
  • Attribute minimization is forbidden.
  • The id attribute replaces the name attribute.
  • The language attribute of the script tag is deprecated.
  • You need to nest all the tags properly.
  • Here is the detail explanation of the above XHTML rules:

DOCTYPE Declaration

All XHTML documents must have a DOCTYPE declaration at the start. There are three types of DOCTYPE declarations, Here is an example of using DOCTYPE:

Case sensitivity

XHTML is case sensitive markup language. All the XHTML tags and attributes need to be written in lower case only.

XHTML Tutorial


XHTML Tutorial
In the example, Href and anchor tag A are having characters which are not in lower case, so it is incorrect.

Closing tags

Each and every XHTML tag should have an equivalent closing tag, even empty elements should also have closing tags. Here is an example showing valid and invalid ways of using tags:

This paragraph is not written according to XHTML syntax.



Here is the correct way of writing above tags in XHTML. Difference is that, here we have closed both the tags properly.

This paragraph is not written according to XHTML syntax.





Attribute quotes

All the values of XHTML attributes must be quoted. Otherwise, your XHTML document is assumed as an invalid document. Here is the example showing syntax:




Attribute minimization

XHTML does not allow attribute minimization. It means you need to explicitly state the attribute and its value. The following example shows the difference:

Here is a list of the minimized attributes in HTML and the way you need to write them in XHTML:
HTML StyleXHTML Style
compactcompact="compact"
checkedchecked="checked"
declaredeclare="declare"
readonlyreadonly="readonly"
disableddisabled="disabled"
selectedselected="selected"
deferdefer="defer"
ismapismap="ismap"
nohrefnohref="nohref"
noshadenoshade="noshade"
nowrapnowrap="nowrap"
multiplemultiple="multiple"
noresizenoresize="noresize"

The id attribute


The id attribute replaces the name attribute. Instead of using name="name", XHTML prefers to use id="id". The following example shows this difference:





The language attribute

The language attribute of the script tag is deprecated. The following example shows this difference:





Nested tags

All the XHTML tags must be nested properly otherwise your document will be assumed as an incorrect XHTML document. The following example shows the syntax:

This text is bold and italic
This text is bold and italic

Element prohibitions

The following elements are not allowed to have any other element inside them. This prohibition applies to all depths of nesting, i.e. it includes all the descendant elements.
ElementProhibition
Must not contain other elements.
Must not contain the , , , , , or elements.
Must not contain the ,