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 Style | XHTML Style |
---|---|
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" |
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
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.
Element | Prohibition |
---|---|
Must not contain other elements. | |
Must not contain the , | |
Must not contain the , | |
Must not contain other | |
Must not contain other |
A minimal XHTML document
Following example shows you a minimum content of an XHTML 1.0 document.
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Every document must have a title
...your content goes here...